Refine search
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; } }
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);
private void ensureEncryptionBuffer(byte[] data) { ((Buffer) encryptionBuffer).clear(); int currentCapacity = encryptionBuffer.remaining(); int requiredCapacity = AudioPacket.RTP_HEADER_BYTE_LENGTH + data.length; if (currentCapacity < requiredCapacity) encryptionBuffer = ByteBuffer.allocate(requiredCapacity); }
@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)); } });
Charset utf8 = Charset.forName("UTF-8"); CharsetEncoder encoder = utf8.newEncoder(); char[] array = new char[1]; CharBuffer input = CharBuffer.wrap(array); ByteBuffer output = ByteBuffer.allocate(10); for (int reps = 0; reps < 10000; reps++) { for (array[0] = 0; array[0] < 10000; array[0]++) { output.clear(); input.clear(); encoder.encode(input, output, false); int len = output.position(); } }
/** * 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; }
public ByteBuffer asEncryptedPacket(ByteBuffer buffer, byte[] secretKey, byte[] nonce, int nlen) { //Xsalsa20's Nonce is 24 bytes long, however RTP (and consequently Discord)'s nonce is a different length // so we need to create a 24 byte array, and copy the nonce into it. // we will leave the extra bytes as nulls. (Java sets non-populated bytes as 0). byte[] extendedNonce = nonce; if (nonce == null) extendedNonce = getNoncePadded(); //Create our SecretBox encoder with the secretKey provided by Discord. TweetNaclFast.SecretBox boxer = new TweetNaclFast.SecretBox(secretKey); byte[] encryptedAudio = boxer.box(encodedAudio, extendedNonce); ((Buffer) buffer).clear(); int capacity = RTP_HEADER_BYTE_LENGTH + encryptedAudio.length + nlen; if (capacity > buffer.remaining()) buffer = ByteBuffer.allocate(capacity); populateBuffer(seq, timestamp, ssrc, encryptedAudio, buffer); if (nonce != null) buffer.put(nonce, 0, nlen); return buffer; }
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; } }
// Create a direct buffer to get bytes from socket. // Direct buffers should be long-lived and be reused as much as possible. ByteBuffer buf = ByteBuffer.allocateDirect(1024); try { // Clear the buffer and read bytes from socket buf.clear(); int numBytesRead = socketChannel.read(buf); if (numBytesRead == -1) { // No more bytes can be read from the channel socketChannel.close(); } else { // To read the bytes, flip the buffer buf.flip(); // Read the bytes from the buffer ...; // see Getting Bytes from a ByteBuffer } } catch (IOException e) { // Connection may have been closed }
public static void readUtf8(URLConnection connection, Appendable out) throws IOException { CharBuffer buffer = CharBuffer.allocate(1024); try (InputStream in = connection.getInputStream(); Reader reader = new InputStreamReader(in, StandardCharsets.UTF_8)) { while (reader.read(buffer) != -1) { buffer.flip(); out.append(buffer); buffer.clear(); } } }