List of utility methods to do ReadableByteChannel Read
int | readFully(ReadableByteChannel channel, ByteBuffer b) Same as the normal channel.read(b), but tries to ensure that the entire len number of bytes is read. int total = 0; while (true) { int got = channel.read(b); if (got < 0) { return (total == 0) ? -1 : total; total += got; if (total == b.capacity() || b.position() == b.capacity()) { ... |
boolean | readFully(ReadableByteChannel channel, ByteBuffer buffer) read Fully while (buffer.remaining() > 0) { int rd = channel.read(buffer); if (rd == -1) return false; return true; |
String | readLengthAndString(ReadableByteChannel channel, ByteBuffer buffer) read Length And String Integer length = readInt(channel, buffer); if (length != null) { char[] chars = new char[length]; chars = readCharArray(channel, buffer, chars); return chars == null ? null : new String(chars); } else return null; |
Map | readMap(ReadableByteChannel channel, ByteBuffer buffer) read Map int size = readInt(channel, buffer); Map<String, String> map = new HashMap<>(); for (int i = 0; i < size; i++) { String key = readLengthAndString(channel, buffer); String value = readLengthAndString(channel, buffer); if (key == null || value == null) { return null; map.put(key, value); return map; |
Object | readObjFromChannel(ReadableByteChannel chan, ByteBuffer buffer, AtomicBoolean endPointCrashed) Returns the next object to be read from this channel, or null if no data can be read at this moment. buffer.clear(); buffer.limit(BYTE_COUNT_INT); if (chan.read(buffer) > 0) { while (buffer.hasRemaining()) { chan.read(buffer); buffer.flip(); int nbBytes = buffer.getInt(); ... |
int | readToBuffer(ReadableByteChannel src, ByteBuffer dst) Reads available data from the stream until the stream is empty or the java.nio.ByteBuffer is full int totalBytesRead = 0; int bytesRead; do { bytesRead = src.read(dst); totalBytesRead += bytesRead; } while (bytesRead > 0); return totalBytesRead; |
int | retryRead(ReadableByteChannel channel, ByteBuffer buffer) retry Read int attempt = 0; int read = 0; while (attempt < 3) { try { Thread.sleep(100L); } catch (InterruptedException e) { read = read(channel, buffer); ... |
long | transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink) Platform-independent channel-to-channel transfer method. long res; long total = 0L; throughBuffer.limit(0); while (total < count) { throughBuffer.compact(); try { if (count - total < (long) throughBuffer.remaining()) { throughBuffer.limit((int) (count - total)); ... |
long | transfer(final ReadableByteChannel source, final long count, final ByteBuffer throughBuffer, final WritableByteChannel sink) Transfer the data from the source to the sink using the given through buffer to pass data through. long total = 0L; while (total < count) { throughBuffer.clear(); if (count - total < throughBuffer.remaining()) { throughBuffer.limit((int) (count - total)); try { long res = source.read(throughBuffer); ... |
File | writeEntryToTemp(File tempDir, ByteBuffer buffer, ReadableByteChannel channel) Write contents of buffer to a temporary file, followed by the remaining bytes in channel .
final File tempFile = File.createTempFile(TEMP_FILENAME_PREFIX, null, tempDir); final FileChannel out = (new FileOutputStream(tempFile)).getChannel(); try { out.write(buffer); final ByteBuffer buf = ByteBuffer.allocate(WRITE_BUFFER_CAPACITY); buf.clear(); while (channel.read(buf) >= 0 || buf.position() != 0) { buf.flip(); ... |