List of usage examples for java.nio ByteBuffer remaining
public final int remaining()
From source file:org.apache.hadoop.hbase.KeyValueUtil.java
/** * Creates a new KeyValue object positioned in the supplied ByteBuffer and sets the ByteBuffer's * position to the start of the next KeyValue. Does not allocate a new array or copy data. * @param bb/*from w ww . j a va2s . c om*/ * @param includesMvccVersion * @param includesTags */ public static KeyValue nextShallowCopy(final ByteBuffer bb, final boolean includesMvccVersion, boolean includesTags) { if (bb.isDirect()) { throw new IllegalArgumentException("only supports heap buffers"); } if (bb.remaining() < 1) { return null; } KeyValue keyValue = null; int underlyingArrayOffset = bb.arrayOffset() + bb.position(); int keyLength = bb.getInt(); int valueLength = bb.getInt(); ByteBufferUtils.skip(bb, keyLength + valueLength); int tagsLength = 0; if (includesTags) { // Read short as unsigned, high byte first tagsLength = ((bb.get() & 0xff) << 8) ^ (bb.get() & 0xff); ByteBufferUtils.skip(bb, tagsLength); } int kvLength = (int) KeyValue.getKeyValueDataStructureSize(keyLength, valueLength, tagsLength); keyValue = new KeyValue(bb.array(), underlyingArrayOffset, kvLength); if (includesMvccVersion) { long mvccVersion = ByteBufferUtils.readVLong(bb); keyValue.setSequenceId(mvccVersion); } return keyValue; }
From source file:byps.BWire.java
/** * Reads a ByteBuffer from an InputStream * Closes the InputStream.//from ww w .j a v a 2 s . c o m * @param is * @return * @throws IOException */ public static ByteBuffer bufferFromStream(InputStream is, Boolean gzip) throws IOException { if (is == null) return null; try { ByteBuffer ibuf = ByteBuffer.allocate(10 * 1000); if (gzip != null) { if (gzip) { is = new GZIPInputStream(is); } } else { if (!is.markSupported()) is = new BufferedInputStream(is, 2); is.mark(2); int magic = is.read() | (is.read() << 8); is.reset(); if (magic == GZIPInputStream.GZIP_MAGIC) { is = new GZIPInputStream(is); } } ReadableByteChannel rch = Channels.newChannel(is); while (rch.read(ibuf) != -1) { if (ibuf.remaining() == 0) { ByteBuffer nbuf = ByteBuffer.allocate(ibuf.capacity() * 2); ibuf.flip(); nbuf.put(ibuf); ibuf = nbuf; } } ibuf.flip(); return ibuf; } finally { is.close(); } }
From source file:com.glaf.core.util.ByteBufferUtils.java
public static InputStream inputStream(ByteBuffer bytes) { final ByteBuffer copy = bytes.duplicate(); return new InputStream() { public int read() throws IOException { if (!copy.hasRemaining()) return -1; return copy.get() & 0xFF; }/* w w w . ja va2 s . c om*/ @Override public int read(byte[] bytes, int off, int len) throws IOException { if (!copy.hasRemaining()) return -1; len = Math.min(len, copy.remaining()); copy.get(bytes, off, len); return len; } @Override public int available() throws IOException { return copy.remaining(); } }; }
From source file:com.FalcoLabs.Fido.api.datastore.serializers.BinarySerializer.java
@SuppressWarnings("unchecked") public T fromByteBuffer(ByteBuffer byteBuffer) { byte[] bytes = new byte[byteBuffer.remaining()]; byteBuffer.get(bytes);/*from w w w .j a va 2 s . c o m*/ Object obj = SerializationUtils.deserialize(bytes); return (T) obj; }
From source file:org.elasticsoftware.elasticactors.base.serialization.JacksonMessageDeserializer.java
@Override public T deserialize(ByteBuffer serializedObject) throws IOException { byte[] buf = new byte[serializedObject.remaining()]; serializedObject.get(buf);/*from w w w .j a va 2 s . c om*/ return objectMapper.readValue(buf, objectClass); }
From source file:org.apache.cassandra.db.marshal.DateType.java
public int compare(ByteBuffer o1, ByteBuffer o2) { if (o1.remaining() == 0) { return o2.remaining() == 0 ? 0 : -1; }/*from www . j av a2 s . c o m*/ if (o2.remaining() == 0) { return 1; } return ByteBufferUtil.compareUnsigned(o1, o2); }
From source file:org.apache.cassandra.serializers.TimestampSerializer.java
public Date deserialize(ByteBuffer bytes) { return bytes.remaining() == 0 ? null : new Date(ByteBufferUtil.toLong(bytes)); }
From source file:com.offbynull.portmapper.natpmp.NatPmpResponse.java
/** * Constructs a {@link NatPmpResponse} object by parsing a buffer. * @param buffer buffer containing NAT-PMP response data * @throws NullPointerException if any argument is {@code null} * @throws BufferUnderflowException if not enough data is available in {@code buffer} * @throws IllegalArgumentException if the version doesn't match the expected version (must always be {@code 0}), or if the op is * {@code < 128}, or if there's an unsuccessful/unrecognized result code *//*ww w. jav a2 s. co m*/ NatPmpResponse(ByteBuffer buffer) { Validate.notNull(buffer); if (buffer.remaining() < 2) { throw new IllegalArgumentException("Bad packet size: " + buffer.remaining()); } int version = buffer.get() & 0xFF; Validate.isTrue(version == 0, "Unknown version: %d", version); op = buffer.get() & 0xFF; Validate.isTrue(op >= 128, "Op must be >= 128: %d", op); int resultCodeNum = buffer.getShort() & 0xFFFF; NatPmpResultCode[] resultCodes = NatPmpResultCode.values(); Validate.isTrue(resultCodeNum < resultCodes.length, "Unknown result code encountered: %d", resultCodeNum); Validate.isTrue(resultCodeNum == NatPmpResultCode.SUCCESS.ordinal(), "Unsuccessful result code: %s [%s]", resultCodes[resultCodeNum].toString(), resultCodes[resultCodeNum].getMessage()); secondsSinceStartOfEpoch = buffer.getInt() & 0xFFFFFFFFL; }
From source file:com.ntsync.shared.RawContact.java
private static String readRawString(ByteBuffer src) throws UnsupportedEncodingException { if (src != null) { int len = src.remaining(); if (len >= 0) { byte[] output = new byte[len]; src.get(output, 0, len);//from w w w. ja v a 2 s . co m // Android has UTF-8 as default return new String(output, SyncDataHelper.DEFAULT_CHARSET_NAME); } } return null; }
From source file:org.apache.cassandra.serializers.TimestampSerializer.java
public void validate(ByteBuffer bytes) throws MarshalException { if (bytes.remaining() != 8 && bytes.remaining() != 0) throw new MarshalException(String.format("Expected 8 or 0 byte long for date (%d)", bytes.remaining())); }