⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bufferedconnectionadapter.java

📁 J2me唆哈的代码
💻 JAVA
字号:
/*
 * Created on 2005-8-19 by pcy
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package a.a.a.midp.io;

import java.io.IOException;

/**
 * @author pcy
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public abstract class BufferedConnectionAdapter extends ConnectionBaseAdapter {

    protected boolean eof;

    protected byte buf[];

    protected int count;

    protected int pos;
    
    protected BufferedConnectionAdapter(int sizeOfBuffer) {
        if (sizeOfBuffer > 0) {
            buf = new byte[sizeOfBuffer];
        }
    }
    
    public int readBytes(byte b[], int off, int len) throws IOException {
        //int bytesRead;

        if (count == 0) {
            if (eof) {
                return -1;
            }

            if (buf == null || len >= buf.length) {
                return nonBufferedRead(b, off, len);
            } else {
                int res = nonBufferedRead(buf, 0, buf.length);

                pos = 0;

                if (res <= 0) {
                    return res;
                } else {
                    count = res;
                }
            }
        }

        if (len > count) {
            len = count;
        }

        System.arraycopy(buf, pos, b, off, len);
        count -= len;
        pos   += len;
        return len;
    }
    
    public int available() throws IOException {
        int bytesRead;

        if (buf == null) {
            return 0;
        }

        if (count > 0) {
            return count;
        }

        bytesRead = readBytesNonBlocking(buf, 0, buf.length);

        if (bytesRead == -1) {
            return 0;
        }
    
        /*
         * Reset the current buffer position and count of bytes
         * available. These variables must be reset to match
         * the processing in readBytes.
         */
        pos = 0;
        count = bytesRead;

        return count;
    }
    
    protected int readBytesNonBlocking(byte b[], int off, int len)
    throws IOException {
        return 0;
    }
    
    protected abstract int nonBufferedRead(byte b[], int off, int len)
    throws IOException;

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -