本文整理了Java中java.nio.Buffer.flip()
方法的一些代码示例,展示了Buffer.flip()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Buffer.flip()
方法的具体详情如下:
包路径:java.nio.Buffer
类名称:Buffer
方法名:flip
[英]Flips this buffer.
The limit is set to the current position, then the position is set to zero, and the mark is cleared.
The content of this buffer is not changed.
[中]翻转此缓冲区。
将限制设置为当前位置,然后将该位置设置为零,并清除标记。
此缓冲区的内容未更改。
代码示例来源:origin: stackoverflow.com
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();
}
}
代码示例来源: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
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
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;
}
代码示例来源:origin: stackoverflow.com
public byte[] longToBytes(long x) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.putLong(x);
return buffer.array();
}
public long bytesToLong(byte[] bytes) {
ByteBuffer buffer = ByteBuffer.allocate(Long.BYTES);
buffer.put(bytes);
buffer.flip();//need flip
return buffer.getLong();
}
代码示例来源: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: 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
ByteBuffer bb = ByteBuffer.allocate(64*1024).order(ByteOrder.LITTLE_ENDIAN );
socket.read(bb);
bb.flip();
double d = bb.getDouble();
代码示例来源:origin: stackoverflow.com
class ReadWorker {
private ByteBuffer buffer = ByteBuffer.allocate(4096);
public void work() {
fillBufferWithData();
buffer.flip();
doSomethingWithData();
buffer.clear();
}
}
代码示例来源: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
public static ByteBuffer clone(ByteBuffer original) {
ByteBuffer clone = ByteBuffer.allocate(original.capacity());
original.rewind();//copy from the beginning
clone.put(original);
original.rewind();
clone.flip();
return clone;
}
代码示例来源: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
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: neo4j/neo4j
private File file( File parent, String name, int size ) throws IOException
{
File file = new File( parent, name );
try ( StoreChannel channel = fs.create( file ) )
{
ByteBuffer buffer = ByteBuffer.allocate( size );
buffer.position( size ).flip();
channel.write( buffer );
}
return file;
}
}
代码示例来源: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
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);
}
内容来源于网络,如有侵权,请联系作者删除!