List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
From source file:Main.java
public static void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException { ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY); while (src.read(buffer) != -1) { buffer.flip(); dest.write(buffer);/*from w ww . jav a 2s . c o m*/ buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } }
From source file:IOUtilities.java
/** * Copy ALL available data from one stream into another * @param in/*from ww w . ja v a 2 s . c om*/ * @param out * @throws IOException */ public static void copy(InputStream in, OutputStream out) throws IOException { ReadableByteChannel source = Channels.newChannel(in); WritableByteChannel target = Channels.newChannel(out); ByteBuffer buffer = ByteBuffer.allocate(16 * 1024); while (source.read(buffer) != -1) { buffer.flip(); // Prepare the buffer to be drained while (buffer.hasRemaining()) { target.write(buffer); } buffer.clear(); // Empty buffer to get ready for filling } source.close(); target.close(); }
From source file:Main.java
public static void transfer(ReadableByteChannel in, WritableByteChannel out) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(4096); while (in.read(buffer) != -1) { buffer.flip(); while (buffer.hasRemaining()) { out.write(buffer);// www .java 2 s.c o m } buffer.clear(); } }
From source file:com.arrow.acn.client.utils.MD5Util.java
public static byte[] calcMD5Checksum(Path path) throws NoSuchAlgorithmException, IOException { MessageDigest md = MessageDigest.getInstance("MD5"); try (SeekableByteChannel sbc = Files.newByteChannel(path)) { ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE); while (sbc.read(buf) > 0) { buf.flip(); md.update(buf);/*from www. j ava 2 s. c o m*/ buf.clear(); } } return md.digest(); }
From source file:Main.java
public static ByteBuffer clone(ByteBuffer original) { ByteBuffer clone = ByteBuffer.allocate(original.capacity()); original.rewind();//copy from the beginning clone.put(original);//from w ww . j a v a 2s. c o m original.rewind(); clone.flip(); return clone; }
From source file:com.alibaba.otter.shared.common.utils.ByteUtils.java
public static int bytes2int(byte[] b) { ByteBuffer buf = ByteBuffer.allocate(4); buf.put(b);/*from ww w. jav a 2 s .c om*/ buf.flip(); return buf.getInt(); }
From source file:com.alibaba.otter.shared.common.utils.ByteUtils.java
public static long bytes2long(byte[] b) { ByteBuffer buf = ByteBuffer.allocate(8); buf.put(b);/*from w w w. j ava 2 s. c o m*/ buf.flip(); return buf.getLong(); }
From source file:Main.java
public static void fastChannelCopy(final ReadableByteChannel src, final WritableByteChannel dest) throws IOException { final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (src.read(buffer) != -1) { // prepare the buffer to be drained buffer.flip(); // write to the channel, may block dest.write(buffer);/*from w w w.j a va2 s .c o m*/ // If partial transfer, shift remainder down // If buffer is empty, same as doing recycle() buffer.compact(); } // EOF will leave buffer in fill state buffer.flip(); // make sure the buffer is fully drained. while (buffer.hasRemaining()) { dest.write(buffer); } }
From source file:com.thinkberg.webdav.Util.java
public static long copyStream(final InputStream is, final OutputStream os) throws IOException { ReadableByteChannel rbc = Channels.newChannel(is); WritableByteChannel wbc = Channels.newChannel(os); int bytesWritten = 0; final ByteBuffer buffer = ByteBuffer.allocateDirect(16 * 1024); while (rbc.read(buffer) != -1) { buffer.flip(); bytesWritten += wbc.write(buffer); buffer.compact();/*from w w w .ja va2s . c om*/ } buffer.flip(); while (buffer.hasRemaining()) { bytesWritten += wbc.write(buffer); } rbc.close(); wbc.close(); return bytesWritten; }
From source file:Main.java
/** * @param columnarKeyBlockData//from w ww . j a va2s . co m * @param columnarKeyStoreMetadata * @return * @author s71955 The high cardinality dimensions rows will be send in byte * array with its data length appended in the * ColumnarKeyStoreDataHolder byte array since high cardinality dim * data will not be part of MDKey/Surrogate keys. In this method the * byte array will be scanned and the length which is stored in * short will be removed. */ public static List<byte[]> readColumnarKeyBlockDataForNoDictionaryCols(byte[] columnarKeyBlockData) { List<byte[]> columnarKeyBlockDataList = new ArrayList<byte[]>(50); ByteBuffer noDictionaryValKeyStoreDataHolder = ByteBuffer.allocate(columnarKeyBlockData.length); noDictionaryValKeyStoreDataHolder.put(columnarKeyBlockData); noDictionaryValKeyStoreDataHolder.flip(); while (noDictionaryValKeyStoreDataHolder.hasRemaining()) { short dataLength = noDictionaryValKeyStoreDataHolder.getShort(); byte[] noDictionaryValKeyData = new byte[dataLength]; noDictionaryValKeyStoreDataHolder.get(noDictionaryValKeyData); columnarKeyBlockDataList.add(noDictionaryValKeyData); } return columnarKeyBlockDataList; }