List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
From source file:org.apache.mnemonic.Utils.java
/** * resize a bytebuffer with a new instance * * @param buf/*from w w w . j a va 2 s.c o m*/ * specify a buf to resize * * @param size * specify the size for resizing * * @return the resized bytebuffer instance */ public static ByteBuffer resizeByteBuffer(ByteBuffer buf, long size) { ByteBuffer ret = ByteBuffer.allocateDirect((int) size); if (ret != null) { if (null != buf) { ret.put(buf); ret.flip(); } } return ret; }
From source file:org.apache.cassandra.index.sasi.disk.TokenTreeTest.java
private static DecoratedKey dk(Long token) { ByteBuffer buf = ByteBuffer.allocate(8); buf.putLong(token);/* w w w .j a va 2 s.c o m*/ buf.flip(); Long hashed = MurmurHash.hash2_64(buf, buf.position(), buf.remaining(), 0); return new BufferDecoratedKey(new Murmur3Partitioner.LongToken(hashed), buf); }
From source file:io.blobkeeper.file.util.FileUtils.java
public static ByteBuffer readFile(@NotNull File file, long offset, long length) { ByteBuffer byteBuffer = ByteBuffer.allocate((int) length); int bytesRead = 0; try {/*w ww .j a v a 2 s . c om*/ // TODO: for exclusively read of file channel by single thread we can use read w/o offset (avoid additional seeks?) while ((bytesRead = file.getFileChannel().read(byteBuffer, offset)) != -1) { if (!byteBuffer.hasRemaining()) { byteBuffer.flip(); return byteBuffer; } offset += bytesRead; } } catch (Exception e) { log.error("Can't read file", e); } if (bytesRead < byteBuffer.capacity()) { String error = String.format("File read error for file %s", file); log.error(error); throw new IllegalArgumentException(error); } byteBuffer.flip(); return byteBuffer; }
From source file:Main.java
private static ByteBuffer encode(CharBuffer in, CharsetEncoder encoder) { int length = (int) (in.remaining() * (double) encoder.averageBytesPerChar()); ByteBuffer out = ByteBuffer.allocate(length); encoder.reset();/* w w w.j a v a 2s .co m*/ CoderResult flushResult = null; while (flushResult != CoderResult.UNDERFLOW) { CoderResult encodeResult = encoder.encode(in, out, true); if (encodeResult == CoderResult.OVERFLOW) { out = allocateMore(out); continue; } flushResult = encoder.flush(out); if (flushResult == CoderResult.OVERFLOW) { out = allocateMore(out); } } out.flip(); return out; }
From source file:de.metalcon.imageServer.protocol.CreateRequestTest.java
/** * compare two input streams/*w w w. j a va 2 s.c o m*/ * * @param stream1 * first input stream * @param stream2 * second input stream * @return true - if the two streams does contain the same content<br> * false - otherwise * @throws IOException * if IO errors occurred */ private static boolean compareInputStreams(final InputStream stream1, final InputStream stream2) throws IOException { final ReadableByteChannel channel1 = Channels.newChannel(stream1); final ReadableByteChannel channel2 = Channels.newChannel(stream2); final ByteBuffer buffer1 = ByteBuffer.allocateDirect(4096); final ByteBuffer buffer2 = ByteBuffer.allocateDirect(4096); try { while (true) { int n1 = channel1.read(buffer1); int n2 = channel2.read(buffer2); if ((n1 == -1) || (n2 == -1)) { return n1 == n2; } buffer1.flip(); buffer2.flip(); for (int i = 0; i < Math.min(n1, n2); i++) { if (buffer1.get() != buffer2.get()) { return false; } } buffer1.compact(); buffer2.compact(); } } finally { if (stream1 != null) { stream1.close(); } if (stream2 != null) { stream2.close(); } } }
From source file:com.mgmtp.perfload.perfalyzer.util.IoUtilities.java
public static void writeToChannel(final WritableByteChannel destChannel, final ByteBuffer buffer) { try {//w ww . j a va 2 s .co m // write to the destination channel destChannel.write(buffer); // If partial transfer, shift remainder down so it does not get lost // If buffer is empty, this is the same as calling clear() buffer.compact(); // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained while (buffer.hasRemaining()) { destChannel.write(buffer); } } catch (IOException ex) { throw new UncheckedIOException(ex); } }
From source file:com.pavlospt.rxfile.RxFile.java
private static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { buffer.flip(); dest.write(buffer);//from w w w . j a v a 2 s. c o m buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } }
From source file:com.log4ic.compressor.utils.FileUtils.java
/** * ?/*from w w w . ja va 2s.co m*/ * * @param fileInputStream * @return */ public static String readFile(FileInputStream fileInputStream) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(1024); StringBuffer contentBuffer = new StringBuffer(); Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; try { FileChannel channel = fileInputStream.getChannel(); while (true) { buffer.clear(); int pos = channel.read(buffer); if (pos == -1) { break; } buffer.flip(); charset = Charset.forName("UTF-8"); decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer); contentBuffer.append(charBuffer.toString()); } } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return contentBuffer.toString(); }
From source file:io.mycat.util.ByteBufferUtil.java
/** * @return a new copy of the data in @param buffer * USUALLY YOU SHOULD USE ByteBuffer.duplicate() INSTEAD, which creates a new Buffer * (so you can mutate its position without affecting the original) without copying the underlying array. *//*from ww w. j a va2 s . c o m*/ public static ByteBuffer clone(ByteBuffer buffer) { assert buffer != null; if (buffer.remaining() == 0) { return EMPTY_BYTE_BUFFER; } ByteBuffer clone = ByteBuffer.allocate(buffer.remaining()); if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.arrayOffset() + buffer.position(), clone.array(), 0, buffer.remaining()); } else { clone.put(buffer.duplicate()); clone.flip(); } return clone; }
From source file:gobblin.tunnel.TestTunnelWithArbitraryTCPTraffic.java
private static String readFromSocket(SocketChannel client) throws IOException { ByteBuffer readBuf = ByteBuffer.allocate(256); LOG.info("Reading from client"); client.read(readBuf);/*from w w w . ja v a2 s . com*/ readBuf.flip(); return StandardCharsets.US_ASCII.decode(readBuf).toString(); }