@Test public void shouldReturnMinusOneWhenEmptyByteBufferProvidedAndReadingOneByte() throws Exception { final ByteBuffer empty = ByteBuffer.wrap(EMPTY_BYTE_ARRAY); final InputStream inputStream = IOUtil.newInputStream(empty); final int read = inputStream.read(); assertEquals(-1, read); }
public static InputStream newInputStream(final ByteBuffer buf) { return new InputStream() { public int read() throws IOException { if (!buf.hasRemaining()) { return -1; } return buf.get() & 0xff; } public int read(byte[] bytes, int off, int len) throws IOException { if (!buf.hasRemaining()) { return -1; } len = Math.min(len, buf.remaining()); buf.get(bytes, off, len); return len; } }; }
IOUtil { public static InputStream newInputStream(final ByteBuffer buf) { return new InputStream() { public int read() throws IOException { if (!buf.hasRemaining()) { return -1; } return buf.get() & 0xff; } public int read(byte[] bytes, int off, int len) throws IOException { if (!buf.hasRemaining()) { return -1; } len = Math.min(len, buf.remaining()); buf.get(bytes, off, len); return len; } }; } }
IOUtil { public static InputStream newInputStream(final ByteBuffer buf) { return new InputStream() { public int read() throws IOException { if (!buf.hasRemaining()) { return -1; } return buf.get() & 0xff; } public int read(byte[] bytes, int off, int len) throws IOException { if (!buf.hasRemaining()) { return -1; } len = Math.min(len, buf.remaining()); buf.get(bytes, off, len); return len; } }; }  }
IOUtil { public static InputStream newInputStream(final ByteBuffer buf) { return new InputStream() { public int read() throws IOException { if (!buf.hasRemaining()) { return -1; } return buf.get() & 0xff; } public int read(byte[] bytes, int off, int len) throws IOException { if (!buf.hasRemaining()) { return -1; } len = Math.min(len, buf.remaining()); buf.get(bytes, off, len); return len; } }; }  static OutputStream newOutputStream(final ByteBuffer buf); static InputStream newInputStream(final ByteBuffer buf); static int copyToHeapBuffer(ByteBuffer src, ByteBuffer dest); static int copyToDirectBuffer(ByteBuffer src, ByteBuffer dest); static int copyFromDirectToDirectBuffer(ByteBuffer src, ByteBuffer dest); static void writeLongString(DataOutput dos, String str); static String readLongString(DataInput in); static void putBoolean(ByteBuffer bb, boolean value); static boolean getBoolean(ByteBuffer bb); static Data toData(Object obj); static long getLong(Data longData); static Data addDelta(Data longData, long delta); static Data addDelta(Data intData, int delta); static Object toObject(Data data); static Object toObject(DataHolder dataHolder); static Object serializeToObject(byte[] bytes); static byte[] serializeToBytes(Object object); static byte[] compress(byte[] input); static byte[] decompress(byte[] compressedData); static void closeResource(Closeable closeable); }
IOUtil { public static InputStream newInputStream(final ByteBuffer buf) { return new InputStream() { public int read() throws IOException { if (!buf.hasRemaining()) { return -1; } return buf.get() & 0xff; } public int read(byte[] bytes, int off, int len) throws IOException { if (!buf.hasRemaining()) { return -1; } len = Math.min(len, buf.remaining()); buf.get(bytes, off, len); return len; } }; }  static OutputStream newOutputStream(final ByteBuffer buf); static InputStream newInputStream(final ByteBuffer buf); static int copyToHeapBuffer(ByteBuffer src, ByteBuffer dest); static int copyToDirectBuffer(ByteBuffer src, ByteBuffer dest); static int copyFromDirectToDirectBuffer(ByteBuffer src, ByteBuffer dest); static void writeLongString(DataOutput dos, String str); static String readLongString(DataInput in); static void putBoolean(ByteBuffer bb, boolean value); static boolean getBoolean(ByteBuffer bb); static Data toData(Object obj); static long getLong(Data longData); static Data addDelta(Data longData, long delta); static Data addDelta(Data intData, int delta); static Object toObject(Data data); static Object toObject(DataHolder dataHolder); static Object serializeToObject(byte[] bytes); static byte[] serializeToBytes(Object object); static byte[] compress(byte[] input); static byte[] decompress(byte[] compressedData); static void closeResource(Closeable closeable);  }