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

📄 abstractbuffer.java

📁 nio的framework,不需要关注NIO编程的细节,只需要根据几个简单的API就可以使用NIO了.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            for (int j = 0; j < s.length(); j++) {
                char c = s.charAt(j);
                sb.append(Character.isISOControl(c) ? '.' : c);
            }
        }
        return sb.toString();
    }

    /**
     * Put byte without check.
     * 
     * @param index
     *            index
     * @param b
     *            byte
     */
    protected abstract void _put(int index, byte b);

    /**
     * Get byte without check.
     * 
     * @param index
     *            index
     * @return byte
     */
    protected abstract byte _get(int index);

    public byte get() {
        return _get(getIndex(1));
    }

    public byte get(int index) {
        return _get(getIndex(index, 1));
    }

    public Buffer get(byte[] dst) {
        return get(dst, 0, dst.length);
    }

    public Buffer get(int index, byte[] dst) {
        return get(index, dst, 0, dst.length);
    }

    public Buffer get(byte[] dst, int offset, int length) {
        return get(ByteBuffer.wrap(dst, offset, length));
    }

    public Buffer get(int index, byte[] dst, int offset, int length) {
        return get(index, ByteBuffer.wrap(dst, offset, length));
    }

    public Buffer get(ByteBuffer dst) {
        return get(dst, dst.remaining());
    }

    public Buffer get(int index, ByteBuffer dst) {
        return get(index, dst, dst.remaining());
    }

    public Buffer get(ByteBuffer dst, int length) {
        checkBounds(0, length, dst.remaining());
        getIndex(length);
        ByteBuffer buffer = asByteBuffer();
        buffer.limit(buffer.position());
        buffer.position(buffer.position() - length);
        dst.put(buffer);
        return this;
    }

    public Buffer get(int index, ByteBuffer dst, int length) {
        checkBounds(0, length, dst.remaining());
        getIndex(index, length);
        ByteBuffer buffer = asByteBuffer();
        buffer.position(index);
        buffer.limit(index + length);
        dst.put(buffer);
        return this;
    }

    public Buffer get(Buffer dst) {
        return get(dst, dst.remaining());
    }

    public Buffer get(int index, Buffer dst) {
        return get(index, dst, dst.remaining());
    }

    public Buffer get(Buffer dst, int length) {
        checkBounds(0, length, dst.remaining());
        getIndex(length);
        ByteBuffer buffer = asByteBuffer();
        buffer.limit(buffer.position());
        buffer.position(buffer.position() - length);
        dst.put(buffer);
        return this;
    }

    public Buffer get(int index, Buffer dst, int length) {
        checkBounds(0, length, dst.remaining());
        getIndex(index, length);
        ByteBuffer buffer = asByteBuffer();
        buffer.position(index);
        buffer.limit(index + length);
        dst.put(buffer);
        return this;
    }

    public Buffer put(byte b) {
        _put(putIndex(1), b);
        return this;
    }

    public Buffer put(int index, byte b) {
        _put(putIndex(index, 1), b);
        return this;
    }

    public Buffer put(byte[] src) {
        return put(src, 0, src.length);
    }

    public Buffer put(int index, byte[] src) {
        return put(index, src, 0, src.length);
    }

    public Buffer put(byte[] src, int offset, int length) {
        return put(ByteBuffer.wrap(src, offset, length));
    }

    public Buffer put(int index, byte[] src, int offset, int length) {
        return put(index, ByteBuffer.wrap(src, offset, length));
    }

    public Buffer put(ByteBuffer src) {
        return put(src, src.remaining());
    }

    public Buffer put(int index, ByteBuffer src) {
        return put(index, src, src.remaining());
    }

    public Buffer put(ByteBuffer src, int length) {
        checkBounds(0, length, src.remaining());
        int pos = putIndex(length);
        for (int i = 0; i < length; i++) {
            _put(pos + i, src.get());
        }
        return this;
    }

    public Buffer put(int index, ByteBuffer src, int length) {
        checkBounds(0, length, src.remaining());
        int pos = putIndex(index, length);
        for (int i = 0; i < length; i++) {
            _put(pos + i, src.get());
        }
        return this;
    }

    public Buffer put(Buffer src) {
        return put(src, src.remaining());
    }

    public Buffer put(int index, Buffer src) {
        return put(index, src, src.remaining());
    }

    public Buffer put(Buffer src, int length) {
        checkBounds(0, length, src.remaining());
        int pos = putIndex(length);
        for (int i = 0; i < length; i++) {
            _put(pos + i, src.get());
        }
        return this;
    }

    public Buffer put(int index, Buffer src, int length) {
        checkBounds(0, length, src.remaining());
        int pos = putIndex(index, length);
        for (int i = 0; i < length; i++) {
            _put(pos + i, src.get());
        }
        return this;
    }

    public char getChar() {
        return (char) getShort();
    }

    public char getChar(int index) {
        return (char) getShort(index);
    }

    public Buffer putChar(char c) {
        return putShort((short) c);
    }

    public Buffer putChar(int index, char c) {
        return putShort(index, (short) c);
    }

    public short getShort() {
        return Bits.decodeShort(this, getIndex(2));
    }

    public short getShort(int index) {
        return Bits.decodeShort(this, getIndex(index, 2));
    }

    public Buffer putShort(short s) {
        return Bits.encodeShort(this, putIndex(2), s);
    }

    public Buffer putShort(int index, short s) {
        return Bits.encodeShort(this, putIndex(index, 2), s);
    }

    public int getInt() {
        return Bits.decodeInt(this, getIndex(4));
    }

    public int getInt(int index) {
        return Bits.decodeInt(this, getIndex(index, 4));
    }

    public Buffer putInt(int i) {
        return Bits.encodeInt(this, putIndex(4), i);
    }

    public Buffer putInt(int index, int i) {
        return Bits.encodeInt(this, putIndex(index, 4), i);
    }

    public long getLong() {
        return Bits.decodeLong(this, getIndex(8));
    }

    public long getLong(int index) {
        return Bits.decodeLong(this, getIndex(index, 8));
    }

    public Buffer putLong(long l) {
        return Bits.encodeLong(this, putIndex(8), l);
    }

    public Buffer putLong(int index, long l) {
        return Bits.encodeLong(this, putIndex(index, 8), l);
    }

    public float getFloat() {
        return Float.intBitsToFloat(getInt());
    }

    public float getFloat(int index) {
        return Float.intBitsToFloat(getInt(index));
    }

    public Buffer putFloat(float f) {
        return putInt(Float.floatToRawIntBits(f));
    }

    public Buffer putFloat(int index, float f) {
        return putInt(index, Float.floatToRawIntBits(f));
    }

    public double getDouble() {
        return Double.longBitsToDouble(getLong());
    }

    public double getDouble(int index) {
        return Double.longBitsToDouble(getLong(index));
    }

    public Buffer putDouble(double d) {
        return putLong(Double.doubleToRawLongBits(d));
    }

    public Buffer putDouble(int index, double d) {
        return putLong(index, Double.doubleToRawLongBits(d));
    }

    public short getUnsignedByte() {
        return (short) (get() & 0xff);
    }

    public short getUnsignedByte(int index) {
        return (short) (get(index) & 0xff);
    }

    public Buffer putUnsignedByte(short s) {
        if (s < 0 || s > 0xff)
            throw new IllegalArgumentException();
        return put((byte) s);
    }

    public Buffer putUnsignedByte(int index, short s) {
        if (s < 0 || s > 0xff)
            throw new IllegalArgumentException();
        return put(index, (byte) s);
    }

    public int getUnsignedShort() {
        return getShort() & 0xffff;
    }

    public int getUnsignedShort(int index) {
        return getShort(index) & 0xffff;
    }

    public Buffer putUnsignedShort(int i) {
        if (i < 0 || i > 0xffff)
            throw new IllegalArgumentException();
        return putShort((short) i);
    }

    public Buffer putUnsignedShort(int index, int i) {
        if (i < 0 || i > 0xffff)
            throw new IllegalArgumentException();
        return putShort(index, (short) i);
    }

    public long getUnsignedInt() {
        return getInt() & 0xffffffffL;
    }

    public long getUnsignedInt(int index) {
        return getInt(index) & 0xffffffffL;
    }

    public Buffer putUnsignedInt(long l) {
        if (l < 0 || l > 0xffffffff)
            throw new IllegalArgumentException();
        return putInt((int) l);
    }

    public Buffer putUnsignedInt(int index, long l) {
        if (l < 0 || l > 0xffffffff)
            throw new IllegalArgumentException();
        return putInt(index, (int) l);
    }

    public String getString(Charset charset, int bufferLen) {
        getIndex(bufferLen);
        ByteBuffer buffer = asByteBuffer();
        buffer.limit(buffer.position());
        buffer.position(buffer.limit() - bufferLen);
        return charset.decode(buffer);
    }

    public String getString(int index, Charset charset, int bufferLen) {
        getIndex(index, bufferLen);
        ByteBuffer buffer = asByteBuffer();
        buffer.position(index);
        buffer.limit(index + bufferLen);
        return charset.decode(buffer);
    }

    public Buffer putString(String s, Charset charset) {
        return put(charset.encode(s));
    }

    public Buffer putString(int index, String s, Charset charset) {
        return put(index, charset.encode(s));
    }

}

⌨️ 快捷键说明

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