📄 abstractiobuffer.java
字号:
case 1: dataLength = getUnsigned(position()); break; case 2: dataLength = getUnsignedShort(position()); break; case 4: dataLength = getInt(position()); break; default: throw new IllegalArgumentException("prefixLength: " + prefixLength); } if (dataLength < 0 || dataLength > maxDataLength) { throw new BufferDataException("dataLength: " + dataLength); } return remaining() - prefixLength >= dataLength; } /** * {@inheritDoc} */ @Override public int indexOf(byte b) { if (hasArray()) { int arrayOffset = arrayOffset(); int beginPos = arrayOffset + position(); int limit = arrayOffset + limit(); byte[] array = array(); for (int i = beginPos; i < limit; i++) { if (array[i] == b) { return i - arrayOffset; } } } else { int beginPos = position(); int limit = limit(); for (int i = beginPos; i < limit; i++) { if (get(i) == b) { return i; } } } return -1; } /** * {@inheritDoc} */ @Override public IoBuffer skip(int size) { autoExpand(size); return position(position() + size); } /** * {@inheritDoc} */ @Override public IoBuffer fill(byte value, int size) { autoExpand(size); int q = size >>> 3; int r = size & 7; if (q > 0) { int intValue = value | value << 8 | value << 16 | value << 24; long longValue = intValue; longValue <<= 32; longValue |= intValue; for (int i = q; i > 0; i--) { putLong(longValue); } } q = r >>> 2; r = r & 3; if (q > 0) { int intValue = value | value << 8 | value << 16 | value << 24; putInt(intValue); } q = r >> 1; r = r & 1; if (q > 0) { short shortValue = (short) (value | value << 8); putShort(shortValue); } if (r > 0) { put(value); } return this; } /** * {@inheritDoc} */ @Override public IoBuffer fillAndReset(byte value, int size) { autoExpand(size); int pos = position(); try { fill(value, size); } finally { position(pos); } return this; } /** * {@inheritDoc} */ @Override public IoBuffer fill(int size) { autoExpand(size); int q = size >>> 3; int r = size & 7; for (int i = q; i > 0; i--) { putLong(0L); } q = r >>> 2; r = r & 3; if (q > 0) { putInt(0); } q = r >> 1; r = r & 1; if (q > 0) { putShort((short) 0); } if (r > 0) { put((byte) 0); } return this; } /** * {@inheritDoc} */ @Override public IoBuffer fillAndReset(int size) { autoExpand(size); int pos = position(); try { fill(size); } finally { position(pos); } return this; } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> E getEnum(Class<E> enumClass) { return toEnum(enumClass, getUnsigned()); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> E getEnum(int index, Class<E> enumClass) { return toEnum(enumClass, getUnsigned(index)); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> E getEnumShort(Class<E> enumClass) { return toEnum(enumClass, getUnsignedShort()); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> E getEnumShort(int index, Class<E> enumClass) { return toEnum(enumClass, getUnsignedShort(index)); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> E getEnumInt(Class<E> enumClass) { return toEnum(enumClass, getInt()); } /** * {@inheritDoc} */ public <E extends Enum<E>> E getEnumInt(int index, Class<E> enumClass) { return toEnum(enumClass, getInt(index)); } /** * {@inheritDoc} */ @Override public IoBuffer putEnum(Enum<?> e) { if (e.ordinal() > BYTE_MASK) { throw new IllegalArgumentException(enumConversionErrorMessage(e, "byte")); } return put((byte) e.ordinal()); } /** * {@inheritDoc} */ @Override public IoBuffer putEnum(int index, Enum<?> e) { if (e.ordinal() > BYTE_MASK) { throw new IllegalArgumentException(enumConversionErrorMessage(e, "byte")); } return put(index, (byte) e.ordinal()); } /** * {@inheritDoc} */ @Override public IoBuffer putEnumShort(Enum<?> e) { if (e.ordinal() > SHORT_MASK) { throw new IllegalArgumentException(enumConversionErrorMessage(e, "short")); } return putShort((short) e.ordinal()); } /** * {@inheritDoc} */ @Override public IoBuffer putEnumShort(int index, Enum<?> e) { if (e.ordinal() > SHORT_MASK) { throw new IllegalArgumentException(enumConversionErrorMessage(e, "short")); } return putShort(index, (short) e.ordinal()); } /** * {@inheritDoc} */ @Override public IoBuffer putEnumInt(Enum<?> e) { return putInt(e.ordinal()); } /** * {@inheritDoc} */ @Override public IoBuffer putEnumInt(int index, Enum<?> e) { return putInt(index, e.ordinal()); } private <E> E toEnum(Class<E> enumClass, int i) { E[] enumConstants = enumClass.getEnumConstants(); if (i > enumConstants.length) { throw new IndexOutOfBoundsException(String.format( "%d is too large of an ordinal to convert to the enum %s", i, enumClass.getName())); } return enumConstants[i]; } private String enumConversionErrorMessage(Enum<?> e, String type) { return String.format("%s.%s has an ordinal value too large for a %s", e .getClass().getName(), e.name(), type); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> EnumSet<E> getEnumSet(Class<E> enumClass) { return toEnumSet(enumClass, get() & BYTE_MASK); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> EnumSet<E> getEnumSet(int index, Class<E> enumClass) { return toEnumSet(enumClass, get(index) & BYTE_MASK); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> EnumSet<E> getEnumSetShort(Class<E> enumClass) { return toEnumSet(enumClass, getShort() & SHORT_MASK); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> EnumSet<E> getEnumSetShort(int index, Class<E> enumClass) { return toEnumSet(enumClass, getShort(index) & SHORT_MASK); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> EnumSet<E> getEnumSetInt(Class<E> enumClass) { return toEnumSet(enumClass, getInt() & INT_MASK); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> EnumSet<E> getEnumSetInt(int index, Class<E> enumClass) { return toEnumSet(enumClass, getInt(index) & INT_MASK); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> EnumSet<E> getEnumSetLong(Class<E> enumClass) { return toEnumSet(enumClass, getLong()); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> EnumSet<E> getEnumSetLong(int index, Class<E> enumClass) { return toEnumSet(enumClass, getLong(index)); } private <E extends Enum<E>> EnumSet<E> toEnumSet(Class<E> clazz, long vector) { EnumSet<E> set = EnumSet.noneOf(clazz); long mask = 1; for (E e : clazz.getEnumConstants()) { if ((mask & vector) == mask) { set.add(e); } mask <<= 1; } return set; } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> IoBuffer putEnumSet(Set<E> set) { long vector = toLong(set); if ((vector & ~BYTE_MASK) != 0) { throw new IllegalArgumentException( "The enum set is too large to fit in a byte: " + set); } return put((byte) vector); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> IoBuffer putEnumSet(int index, Set<E> set) { long vector = toLong(set); if ((vector & ~BYTE_MASK) != 0) { throw new IllegalArgumentException( "The enum set is too large to fit in a byte: " + set); } return put(index, (byte) vector); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> IoBuffer putEnumSetShort(Set<E> set) { long vector = toLong(set); if ((vector & ~SHORT_MASK) != 0) { throw new IllegalArgumentException( "The enum set is too large to fit in a short: " + set); } return putShort((short) vector); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> IoBuffer putEnumSetShort(int index, Set<E> set) { long vector = toLong(set); if ((vector & ~SHORT_MASK) != 0) { throw new IllegalArgumentException( "The enum set is too large to fit in a short: " + set); } return putShort(index, (short) vector); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> IoBuffer putEnumSetInt(Set<E> set) { long vector = toLong(set); if ((vector & ~INT_MASK) != 0) { throw new IllegalArgumentException( "The enum set is too large to fit in an int: " + set); } return putInt((int) vector); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> IoBuffer putEnumSetInt(int index, Set<E> set) { long vector = toLong(set); if ((vector & ~INT_MASK) != 0) { throw new IllegalArgumentException( "The enum set is too large to fit in an int: " + set); } return putInt(index, (int) vector); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> IoBuffer putEnumSetLong(Set<E> set) { return putLong(toLong(set)); } /** * {@inheritDoc} */ @Override public <E extends Enum<E>> IoBuffer putEnumSetLong(int index, Set<E> set) { return putLong(index, toLong(set)); } private <E extends Enum<E>> long toLong(Set<E> set) { long vector = 0; for (E e : set) { if (e.ordinal() >= Long.SIZE) { throw new IllegalArgumentException( "The enum set is too large to fit in a bit vector: " + set); } vector |= 1L << e.ordinal(); } return vector; } /** * This method forwards the call to {@link #expand(int)} only when * <tt>autoExpand</tt> property is <tt>true</tt>. */ private IoBuffer autoExpand(int expectedRemaining) { if (isAutoExpand()) { expand(expectedRemaining, true); } return this; } /** * This method forwards the call to {@link #expand(int)} only when * <tt>autoExpand</tt> property is <tt>true</tt>. */ private IoBuffer autoExpand(int pos, int expectedRemaining) { if (isAutoExpand()) { expand(pos, expectedRemaining, true); } return this; } private static void checkFieldSize(int fieldSize) { if (fieldSize < 0) { throw new IllegalArgumentException("fieldSize cannot be negative: " + fieldSize); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -