List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
From source file:hivemall.fm.FactorizationMachineUDTF.java
private static void writeBuffer(@Nonnull ByteBuffer srcBuf, @Nonnull NioStatefullSegment dst) throws HiveException { srcBuf.flip(); try {//from w w w .j a va 2 s. c om dst.write(srcBuf); } catch (IOException e) { throw new HiveException("Exception causes while writing a buffer to file", e); } srcBuf.clear(); }
From source file:io.ecarf.core.utils.Utils.java
/** * Get the likely uncompressed size of a Gziped file * @param filename//from w w w . j a v a 2s . c o m * @return * @throws IOException * @see http://stackoverflow.com/questions/27825927/get-gzipped-file-attributes-like-gzip-l-basically-compression-ratio * @throws FileNotFoundException */ public static long getUncompressedFileSize(String filename) throws FileNotFoundException, IOException { File f = new File(filename); try (RandomAccessFile ra = new RandomAccessFile(f, "r"); FileChannel channel = ra.getChannel()) { MappedByteBuffer fileBuffer = channel.map(MapMode.READ_ONLY, f.length() - 4, 4); fileBuffer.load(); ByteBuffer buf = ByteBuffer.allocate(4); buf.order(ByteOrder.LITTLE_ENDIAN); buf.put(fileBuffer); buf.flip(); //will print the uncompressed size //getInt() reads the 4 bytes as a int // if the file is between 2GB and 4GB // then this will return a negative value //and you'll have to do your own converting to an unsigned int int size = buf.getInt(); if (size < 0) { return FileUtils.ONE_GB + size; } else { return size; } } }
From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java
static ByteBuffer makeCompositeBuffer(byte[] colName, long ts) { assert colName.length <= 1 << 16 : "Cannot use column names larger than 64KiB, was " + colName.length; ByteBuffer buffer = ByteBuffer.allocate(6 /* misc */ + 8 /* timestamp */ + colName.length) .order(ByteOrder.BIG_ENDIAN); buffer.put((byte) ((colName.length >> 8) & 0xFF)); buffer.put((byte) (colName.length & 0xFF)); buffer.put(colName);// www . j a v a2 s .c om buffer.put((byte) 0); buffer.put((byte) 0); buffer.put((byte) (8 & 0xFF)); buffer.putLong(~ts); buffer.put((byte) 0); buffer.flip(); return buffer; }
From source file:hivemall.mf.OnlineMatrixFactorizationUDTF.java
protected static void writeBuffer(@Nonnull final ByteBuffer srcBuf, @Nonnull final NioFixedSegment dst, final long lastWritePos) throws HiveException { // TODO asynchronous write in the background srcBuf.flip(); try {/* w ww .j ava2 s. com*/ dst.writeRecords(lastWritePos, srcBuf); } catch (IOException e) { throw new HiveException("Exception causes while writing records to : " + lastWritePos, e); } srcBuf.clear(); }
From source file:io.blobkeeper.file.util.FileUtils.java
public static long getCrc(@NotNull File file) { CRC32 crc = new CRC32(); while (true) { ByteBuffer buffer = ByteBuffer.allocate(CHUNK_SIZE); while (buffer.hasRemaining()) { int bytes = 0; try { bytes = file.getFileChannel().read(buffer); } catch (IOException e) { log.error("Can't read blob file " + file, e); throw new IllegalArgumentException(e); }//from ww w. j a va 2s. co m if (bytes < 0) { break; } } buffer.flip(); if (buffer.remaining() == 0) { break; } else { crc.update(buffer.array()); } } return crc.getValue(); }
From source file:org.activiti.engine.test.api.repository.diagram.ProcessDiagramRetrievalTest.java
private static boolean isEqual(InputStream stream1, InputStream stream2) throws IOException { ReadableByteChannel channel1 = Channels.newChannel(stream1); ReadableByteChannel channel2 = Channels.newChannel(stream2); ByteBuffer buffer1 = ByteBuffer.allocateDirect(1024); ByteBuffer buffer2 = ByteBuffer.allocateDirect(1024); try {/* ww w .j a v a2s . c o m*/ while (true) { int bytesReadFromStream1 = channel1.read(buffer1); int bytesReadFromStream2 = channel2.read(buffer2); if (bytesReadFromStream1 == -1 || bytesReadFromStream2 == -1) return bytesReadFromStream1 == bytesReadFromStream2; buffer1.flip(); buffer2.flip(); for (int i = 0; i < Math.min(bytesReadFromStream1, bytesReadFromStream2); 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:hivemall.mf.BPRMatrixFactorizationUDTF.java
private static void writeBuffer(@Nonnull final ByteBuffer srcBuf, @Nonnull final NioFixedSegment dst, final long lastWritePos) throws HiveException { // TODO asynchronous write in the background srcBuf.flip(); try {//from w ww . j a v a 2s .c om dst.writeRecords(lastWritePos, srcBuf); } catch (IOException e) { throw new HiveException("Exception causes while writing records to : " + lastWritePos, e); } srcBuf.clear(); }
From source file:com.aerohive.nms.engine.admin.task.licensemgr.license.processor2.PacketUtil.java
public static byte[] join(Header header, byte[] content) { ByteBuffer buf = ByteBuffer.allocate(8192); byte[] outBytes; if (content.length == 0) { outBytes = new byte[0]; } else {//from w w w.j a v a 2 s. c om if (header.isSecretFlag()) { // encrypt data outBytes = encryptData(content); } else { outBytes = new byte[content.length]; System.arraycopy(content, 0, outBytes, 0, content.length); } } buf.put(header.getType()); buf.putInt(outBytes.length); buf.put(header.getProtocolVersion()); buf.put(header.isSecretFlag() ? CommConst.Secret_Flag_Yes : CommConst.Secret_Flag_No); buf.put(outBytes); buf.flip(); byte[] dst = new byte[buf.limit()]; buf.get(dst); return dst; }
From source file:hivemall.GeneralLearnerBaseUDTF.java
private static void writeBuffer(@Nonnull ByteBuffer srcBuf, @Nonnull NioStatefulSegment dst) throws HiveException { srcBuf.flip(); try {/*from w w w .j a v a2 s . c o m*/ dst.write(srcBuf); } catch (IOException e) { throw new HiveException("Exception causes while writing a buffer to file", e); } srcBuf.clear(); }
From source file:org.apache.bookkeeper.bookie.BookieJournalTest.java
private static void updateJournalVersion(JournalChannel jc, int journalVersion) throws IOException { long prevPos = jc.fc.position(); try {/*from w w w .j a v a 2 s . c o m*/ ByteBuffer versionBuffer = ByteBuffer.allocate(4); versionBuffer.putInt(journalVersion); versionBuffer.flip(); jc.fc.position(4); IOUtils.writeFully(jc.fc, versionBuffer); jc.fc.force(true); } finally { jc.fc.position(prevPos); } }