本文整理了Java中java.nio.Buffer.clear()
方法的一些代码示例,展示了Buffer.clear()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Buffer.clear()
方法的具体详情如下:
包路径:java.nio.Buffer
类名称:Buffer
方法名:clear
[英]Clears this buffer.
While the content of this buffer is not changed, the following internal changes take place: the current position is reset back to the start of the buffer, the value of the buffer limit is made equal to the capacity and mark is cleared.
[中]清除此缓冲区。
当此缓冲区的内容未更改时,会发生以下内部更改:将当前位置重置回缓冲区的开始位置,使缓冲区限制值等于容量,并清除标记。
代码示例来源:origin: stackoverflow.com
ByteBuffer b = new ByteBuffer(1024);
for(int i=0; i<N; i++) {
b.clear();
b.put(header[i]);
b.put(data[i]);
b.flip();
out.write(b);
}
代码示例来源:origin: stackoverflow.com
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;
}
}
代码示例来源:origin: stackoverflow.com
ByteBuffer buffer = ByteBuffer.allocate(20);
buffer.clear();
test.putUnsignedByte(buffer, 255);
test.putUnsignedByte(buffer, 128);
代码示例来源:origin: stackoverflow.com
md = MessageDigest.getInstance("MD5");
FileChannel channel = inputStream.getChannel();
ByteBuffer buff = ByteBuffer.allocate(2048);
while(channel.read(buff) != -1)
buff.flip();
md.update(buff);
buff.clear();
代码示例来源:origin: stackoverflow.com
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);
代码示例来源:origin: DV8FromTheWorld/JDA
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);
}
代码示例来源:origin: stackoverflow.com
output = Channels.newChannel(externalContext.getResponseOutputStream());
for (ByteBuffer buffer = ByteBuffer.allocateDirect(10240); input.read(buffer) != -1; buffer.clear()) {
output.write((ByteBuffer) buffer.flip());
代码示例来源:origin: glowroot/glowroot
@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));
}
});
代码示例来源:origin: stackoverflow.com
class ReadWorker {
private ByteBuffer buffer = ByteBuffer.allocate(4096);
public void work() {
fillBufferWithData();
buffer.flip();
doSomethingWithData();
buffer.clear();
}
}
代码示例来源:origin: stackoverflow.com
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();
}
}
代码示例来源:origin: stackoverflow.com
buffer.flip();
size += outputChannel.write(buffer);
buffer.clear();
代码示例来源:origin: eclipse/californium
/**
* 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;
}
代码示例来源:origin: stackoverflow.com
private static ByteBuffer buffer = ByteBuffer.allocate(8);
public static byte[] encodeDouble(double x) {
buffer.clear();
buffer.putDouble(0, x);
return buffer.array();
}
public static double decodeDouble(byte[] bytes) {
buffer.clear();
buffer.put(bytes);
buffer.flip();
return buffer.getDouble();
}
代码示例来源:origin: DV8FromTheWorld/JDA
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;
}
代码示例来源:origin: stackoverflow.com
int bufsize = 8192;
ByteBuffer buffer = ByteBuffer.allocateDirect(bufsize);
byte[] temp = new byte[bufsize];
int b = channel.read(buffer);
while (b > 0) {
buffer.flip();
buffer.get(temp, 0, b);
md.update(temp, 0, b);
buffer.clear();
b = channel.read(buffer);
}
代码示例来源:origin: stackoverflow.com
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++;
}
代码示例来源:origin: stackoverflow.com
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;
}
}
代码示例来源:origin: stackoverflow.com
ByteBuffer buf = ByteBuffer.allocate(100);
while(!stopped)
{
// do something
buf.clear();
}
代码示例来源:origin: stackoverflow.com
// 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
}
代码示例来源:origin: stackoverflow.com
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();
}
}
}
内容来源于网络,如有侵权,请联系作者删除!