Refine search
public class ByteUtils { private static ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES); public static byte[] longToBytes(long x) { buffer.putLong(0, x); return buffer.array(); } public static long bytesToLong(byte[] bytes) { buffer.put(bytes, 0, bytes.length); buffer.flip();//need flip return buffer.getLong(); } }
public static long stream(InputStream input, OutputStream output) throws IOException { try ( ReadableByteChannel inputChannel = Channels.newChannel(input); WritableByteChannel outputChannel = Channels.newChannel(output); ) { ByteBuffer buffer = ByteBuffer.allocateDirect(10240); long size = 0; while (inputChannel.read(buffer) != -1) { buffer.flip(); size += outputChannel.write(buffer); buffer.clear(); } return size; } }
public static ByteBuffer cloneByteBuffer(final ByteBuffer original) { // Create clone with same capacity as original. final ByteBuffer clone = (original.isDirect()) ? ByteBuffer.allocateDirect(original.capacity()) : ByteBuffer.allocate(original.capacity()); // Create a read-only copy of the original. // This allows reading from the original without modifying it. final ByteBuffer readOnlyCopy = original.asReadOnlyBuffer(); // Flip and read from the original. readOnlyCopy.flip(); clone.put(readOnlyCopy); return clone; }
ByteBuffer buffer = ByteBuffer.allocate(1024); Selector selector = Selector.open(); case OP_READ: client = (SocketChannel) key.channel(); buffer.clear(); if (client.read(buffer) != -1) { buffer.flip(); String line = new String(buffer.array(), buffer.position(), buffer.remaining()); System.out.println(line);
@Override public void call(ByteBuffer buffer) { // this cast is needed in order to avoid // java.lang.NoSuchMethodError: java.nio.ByteBuffer.clear()Ljava/nio/ByteBuffer; // when this code is compiled with Java 9 and run with Java 8 or earlier ((Buffer) buffer).clear(); histogram.encodeIntoByteBuffer(buffer); int size = buffer.position(); // this cast is needed in order to avoid // java.lang.NoSuchMethodError: java.nio.ByteBuffer.flip()Ljava/nio/ByteBuffer; // when this code is compiled with Java 9 and run with Java 8 or earlier ((Buffer) buffer).flip(); builder.setEncodedBytes(ByteString.copyFrom(buffer, size)); } });
/** * Gets the buffer's content. * <p> * The buffer will be cleared as part of this method, thus this method should * only be invoked once there are no more blocks to add. * * @return The bytes contained in the buffer. */ public byte[] getBody() { ((Buffer)buf).flip(); byte[] body = new byte[buf.remaining()]; ((Buffer)buf.get(body)).clear(); return body; }
private long stream(InputStream input, OutputStream output) throws IOException { try (ReadableByteChannel inputChannel = Channels.newChannel(input); WritableByteChannel outputChannel = Channels.newChannel(output)) { ByteBuffer buffer = ByteBuffer.allocate(10240); long size = 0; while (inputChannel.read(buffer) != -1) { buffer.flip(); size += outputChannel.write(buffer); buffer.clear(); } return size; } }
int readCount = 0; int BUFFER_SIZE = 256; StringBuilder sb = new StringBuilder(); CharBuffer cBuffer = CharBuffer.allocate(BUFFER_SIZE); Reader reader = new InputStreamReader(is, StandardCharsets.UTF_8); while(reader.read(cBuffer) > 0 && readCount < 68) { cBuffer.flip(); sb.append(cBuffer); cBuffer.clear(); readCount++; }
private long stream(InputStream input, OutputStream output) throws IOException { try (ReadableByteChannel inputChannel = Channels.newChannel(input); WritableByteChannel outputChannel = Channels.newChannel(output)) { ByteBuffer buffer = ByteBuffer.allocate(10240); long size = 0; while (inputChannel.read(buffer) != -1) { buffer.flip(); size += outputChannel.write(buffer); buffer.clear(); } return size; }
private final ByteBuffer writeBuffer = ByteBuffer.allocateDirect(1024*1024); private void writeData(AbstractMsg msg) { writeBuffer.clear(); writeBuffer.putInt(0); // set later msg.writeHeader(writeBuffer); msg.writeData(writeBuffer); writeBuffer.putInt(0, writeBuffer.position()); writeBuffer.flip(); while(writeBuffer.hasRemaining()) commChannel.write(writeBuffer); }