List of usage examples for java.nio ByteBuffer clear
public final Buffer clear()
From source file:Main.java
private static byte[] getByteArrayFromBuffer(ByteBuffer byteBuf) { byteBuf.flip();//from w ww . ja va2 s. c om byte[] row = new byte[byteBuf.limit()]; byteBuf.get(row); byteBuf.clear(); return row; }
From source file:Main.java
static ByteBuffer createMsg(int state, long leader, long zxid, long epoch) { byte requestBytes[] = new byte[28]; ByteBuffer requestBuffer = ByteBuffer.wrap(requestBytes); /*// w w w.j ava2 s . co m * Building notification packet to send */ requestBuffer.clear(); requestBuffer.putInt(state); requestBuffer.putLong(leader); requestBuffer.putLong(zxid); requestBuffer.putLong(epoch); return requestBuffer; }
From source file:pro.foundev.GzipCompressionOfColumnExample.java
private static String fromByteBuffer(ByteBuffer byteBuffer) { if (byteBuffer == null) { return null; }/* w w w .ja va2 s. c o m*/ GZIPInputStream gzipInputStream = null; ByteArrayOutputStream baos = null; ByteBuffer dup = ByteBuffer.allocate(byteBuffer.limit()); dup.clear(); dup.put(byteBuffer); dup.flip(); try { gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(dup.array(), 0, dup.limit())); baos = new ByteArrayOutputStream(); for (int value = 0; value != -1;) { value = gzipInputStream.read(); if (value != -1) { baos.write(value); } } gzipInputStream.close(); baos.flush(); baos.close(); return new String(baos.toByteArray(), charset); } catch (IOException e) { throw new RuntimeException("Error decompressing column data", e); } finally { if (gzipInputStream != null) { try { gzipInputStream.close(); } catch (IOException e) { } } if (baos != null) { try { baos.flush(); baos.close(); } catch (IOException e) { } } } }
From source file:Main.java
public static ByteBuffer setupByteBuffer(ByteBuffer preBuffer, byte[] array) { if (preBuffer == null || preBuffer.capacity() < array.length) { preBuffer = createByteBuffer(array.length * 2); } else {/* w ww . ja v a2 s . co m*/ preBuffer.clear(); } preBuffer.put(array); preBuffer.position(0); return preBuffer; }
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();// w w w .j a va2s . c o m md.update(buf); buf.clear(); } } return md.digest(); }
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();/* w w w. ja v a 2 s . c o m*/ while (buffer.hasRemaining()) { out.write(buffer); } buffer.clear(); } }
From source file:xbird.server.services.RemotePagingService.java
private static boolean doRead(final SocketChannel channel, final ByteBuffer cmdBuffer) throws IOException { cmdBuffer.clear(); int n, count = 0; while (channel.isOpen() && (n = channel.read(cmdBuffer)) > 0) { count += n;/*from www . j a v a 2 s .c o m*/ } return count > 0; }
From source file:Main.java
public static Object getObject(ByteBuffer byteBuffer) throws ClassNotFoundException, IOException { InputStream input = new ByteArrayInputStream(byteBuffer.array()); ObjectInputStream oi = new ObjectInputStream(input); Object obj = oi.readObject(); input.close();//from w ww .j a v a 2 s . c o m oi.close(); byteBuffer.clear(); return obj; }
From source file:IOUtilities.java
/** * Copy ALL available data from one stream into another * @param in/*from w ww . jav a2 s . com*/ * @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:com.google.cloud.public_datasets.nexrad2.GcsUntar.java
private static File downloadFromGcs(String bucketName, String blobName, File tmpDir) throws IOException { Storage storage = StorageOptions.getDefaultInstance().getService(); File fileName = File.createTempFile("download", "bytes", tmpDir); try (ReadChannel reader = storage.reader(bucketName, blobName); FileOutputStream writer = new FileOutputStream(fileName)) { ByteBuffer bytes = ByteBuffer.allocate(64 * 1024); while (reader.read(bytes) > 0) { bytes.flip();/*from www. j a v a2 s .c o m*/ writer.getChannel().write(bytes); bytes.clear(); } } return fileName; }