📄 abstractiobuffer.java
字号:
public boolean equals(Object o) { if (!(o instanceof IoBuffer)) { return false; } IoBuffer that = (IoBuffer) o; if (this.remaining() != that.remaining()) { return false; } int p = this.position(); for (int i = this.limit() - 1, j = that.limit() - 1; i >= p; i--, j--) { byte v1 = this.get(i); byte v2 = that.get(j); if (v1 != v2) { return false; } } return true; } /** * {@inheritDoc} */ public int compareTo(IoBuffer that) { int n = this.position() + Math.min(this.remaining(), that.remaining()); for (int i = this.position(), j = that.position(); i < n; i++, j++) { byte v1 = this.get(i); byte v2 = that.get(j); if (v1 == v2) { continue; } if (v1 < v2) { return -1; } return +1; } return this.remaining() - that.remaining(); } /** * {@inheritDoc} */ @Override public String toString() { StringBuilder buf = new StringBuilder(); if (isDirect()) { buf.append("DirectBuffer"); } else { buf.append("HeapBuffer"); } buf.append("[pos="); buf.append(position()); buf.append(" lim="); buf.append(limit()); buf.append(" cap="); buf.append(capacity()); buf.append(": "); buf.append(getHexDump(16)); buf.append(']'); return buf.toString(); } /** * {@inheritDoc} */ @Override public IoBuffer get(byte[] dst) { return get(dst, 0, dst.length); } /** * {@inheritDoc} */ @Override public IoBuffer put(IoBuffer src) { return put(src.buf()); } /** * {@inheritDoc} */ @Override public IoBuffer put(byte[] src) { return put(src, 0, src.length); } /** * {@inheritDoc} */ @Override public int getUnsignedShort() { return getShort() & 0xffff; } /** * {@inheritDoc} */ @Override public int getUnsignedShort(int index) { return getShort(index) & 0xffff; } /** * {@inheritDoc} */ @Override public long getUnsignedInt() { return getInt() & 0xffffffffL; } /** * {@inheritDoc} */ @Override public int getMediumInt() { byte b1 = get(); byte b2 = get(); byte b3 = get(); if (ByteOrder.BIG_ENDIAN.equals(order())) { return getMediumInt(b1, b2, b3); } else { return getMediumInt(b3, b2, b1); } } /** * {@inheritDoc} */ @Override public int getUnsignedMediumInt() { int b1 = getUnsigned(); int b2 = getUnsigned(); int b3 = getUnsigned(); if (ByteOrder.BIG_ENDIAN.equals(order())) { return b1 << 16 | b2 << 8 | b3; } else { return b3 << 16 | b2 << 8 | b1; } } /** * {@inheritDoc} */ @Override public int getMediumInt(int index) { byte b1 = get(index); byte b2 = get(index + 1); byte b3 = get(index + 2); if (ByteOrder.BIG_ENDIAN.equals(order())) { return getMediumInt(b1, b2, b3); } else { return getMediumInt(b3, b2, b1); } } /** * {@inheritDoc} */ @Override public int getUnsignedMediumInt(int index) { int b1 = getUnsigned(index); int b2 = getUnsigned(index + 1); int b3 = getUnsigned(index + 2); if (ByteOrder.BIG_ENDIAN.equals(order())) { return b1 << 16 | b2 << 8 | b3; } else { return b3 << 16 | b2 << 8 | b1; } } /** * {@inheritDoc} */ private int getMediumInt(byte b1, byte b2, byte b3) { int ret = b1 << 16 & 0xff0000 | b2 << 8 & 0xff00 | b3 & 0xff; // Check to see if the medium int is negative (high bit in b1 set) if ((b1 & 0x80) == 0x80) { // Make the the whole int negative ret |= 0xff000000; } return ret; } /** * {@inheritDoc} */ @Override public IoBuffer putMediumInt(int value) { byte b1 = (byte) (value >> 16); byte b2 = (byte) (value >> 8); byte b3 = (byte) value; if (ByteOrder.BIG_ENDIAN.equals(order())) { put(b1).put(b2).put(b3); } else { put(b3).put(b2).put(b1); } return this; } /** * {@inheritDoc} */ @Override public IoBuffer putMediumInt(int index, int value) { byte b1 = (byte) (value >> 16); byte b2 = (byte) (value >> 8); byte b3 = (byte) value; if (ByteOrder.BIG_ENDIAN.equals(order())) { put(index, b1).put(index + 1, b2).put(index + 2, b3); } else { put(index, b3).put(index + 1, b2).put(index + 2, b1); } return this; } /** * {@inheritDoc} */ @Override public long getUnsignedInt(int index) { return getInt(index) & 0xffffffffL; } /** * {@inheritDoc} */ @Override public InputStream asInputStream() { return new InputStream() { @Override public int available() { return AbstractIoBuffer.this.remaining(); } @Override public synchronized void mark(int readlimit) { AbstractIoBuffer.this.mark(); } @Override public boolean markSupported() { return true; } @Override public int read() { if (AbstractIoBuffer.this.hasRemaining()) { return AbstractIoBuffer.this.get() & 0xff; } else { return -1; } } @Override public int read(byte[] b, int off, int len) { int remaining = AbstractIoBuffer.this.remaining(); if (remaining > 0) { int readBytes = Math.min(remaining, len); AbstractIoBuffer.this.get(b, off, readBytes); return readBytes; } else { return -1; } } @Override public synchronized void reset() { AbstractIoBuffer.this.reset(); } @Override public long skip(long n) { int bytes; if (n > Integer.MAX_VALUE) { bytes = AbstractIoBuffer.this.remaining(); } else { bytes = Math .min(AbstractIoBuffer.this.remaining(), (int) n); } AbstractIoBuffer.this.skip(bytes); return bytes; } }; } /** * {@inheritDoc} */ @Override public OutputStream asOutputStream() { return new OutputStream() { @Override public void write(byte[] b, int off, int len) { AbstractIoBuffer.this.put(b, off, len); } @Override public void write(int b) { AbstractIoBuffer.this.put((byte) b); } }; } /** * {@inheritDoc} */ @Override public String getHexDump() { return this.getHexDump(Integer.MAX_VALUE); } /** * {@inheritDoc} */ @Override public String getHexDump(int lengthLimit) { return IoBufferHexDumper.getHexdump(this, lengthLimit); } /** * {@inheritDoc} */ @Override public String getString(CharsetDecoder decoder) throws CharacterCodingException { if (!hasRemaining()) { return ""; } boolean utf16 = decoder.charset().name().startsWith("UTF-16"); int oldPos = position(); int oldLimit = limit(); int end = -1; int newPos; if (!utf16) { end = indexOf((byte) 0x00); if (end < 0) { newPos = end = oldLimit; } else { newPos = end + 1; } } else { int i = oldPos; for (;;) { boolean wasZero = get(i) == 0; i++; if (i >= oldLimit) { break; } if (get(i) != 0) { i++; if (i >= oldLimit) { break; } else { continue; } } if (wasZero) { end = i - 1; break; } } if (end < 0) { newPos = end = oldPos + (oldLimit - oldPos & 0xFFFFFFFE); } else { if (end + 2 <= oldLimit) { newPos = end + 2; } else { newPos = end; } } } if (oldPos == end) { position(newPos); return ""; } limit(end); decoder.reset(); int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1; CharBuffer out = CharBuffer.allocate(expectedLength); for (;;) { CoderResult cr; if (hasRemaining()) { cr = decoder.decode(buf(), out, true); } else { cr = decoder.flush(out); } if (cr.isUnderflow()) { break; } if (cr.isOverflow()) { CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength); out.flip(); o.put(out); out = o; continue; } if (cr.isError()) { // Revert the buffer back to the previous state. limit(oldLimit); position(oldPos); cr.throwException(); } } limit(oldLimit); position(newPos); return out.flip().toString(); } /** * {@inheritDoc} */ @Override public String getString(int fieldSize, CharsetDecoder decoder) throws CharacterCodingException { checkFieldSize(fieldSize); if (fieldSize == 0) { return ""; } if (!hasRemaining()) { return ""; } boolean utf16 = decoder.charset().name().startsWith("UTF-16"); if (utf16 && (fieldSize & 1) != 0) { throw new IllegalArgumentException("fieldSize is not even."); } int oldPos = position(); int oldLimit = limit(); int end = oldPos + fieldSize; if (oldLimit < end) { throw new BufferUnderflowException(); } int i; if (!utf16) { for (i = oldPos; i < end; i++) { if (get(i) == 0) { break; } } if (i == end) { limit(end); } else { limit(i); } } else { for (i = oldPos; i < end; i += 2) { if (get(i) == 0 && get(i + 1) == 0) { break; } } if (i == end) { limit(end); } else { limit(i); } } if (!hasRemaining()) { limit(oldLimit); position(end); return ""; } decoder.reset(); int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1; CharBuffer out = CharBuffer.allocate(expectedLength); for (;;) { CoderResult cr; if (hasRemaining()) { cr = decoder.decode(buf(), out, true); } else { cr = decoder.flush(out); } if (cr.isUnderflow()) { break; } if (cr.isOverflow()) { CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -