List of usage examples for java.nio ByteBuffer flip
public final Buffer flip()
From source file:org.sglover.nlp.EntityExtracter.java
private String getContent(ReadableByteChannel channel) throws IOException { StringBuilder sb = new StringBuilder(); ByteBuffer bb = ByteBuffer.allocate(2048); int c = -1;/*from w w w.ja v a 2 s. c o m*/ do { c = channel.read(bb); bb.flip(); bb.clear(); sb.append(new String(bb.array(), "UTF-8")); } while (c != -1); String content = sb.toString(); return content; }
From source file:com.bennavetta.appsite.file.ResourceService.java
private void copy(ReadableByteChannel src, WritableByteChannel dest) throws IOException { ByteBuffer buffer = ByteBuffer.allocateDirect(bufferSize.get()); while (src.read(buffer) != -1 || buffer.position() != 0) { buffer.flip(); dest.write(buffer);//from w ww . j av a 2 s. c o m buffer.compact(); } }
From source file:com.joemelsha.crypto.hash.KeccakTest.java
@Test public void testValid() { T hash = create();// w w w .ja v a 2 s. c om for (ByteBuffer[] tv : testVectors) { hash.reset(); hash.update(tv[0].duplicate()); ByteBuffer out = ByteBuffer.allocate(tv[1].remaining()); hash.digest(out); out.flip(); Assert.assertEquals("mismatch", out, tv[1]); } }
From source file:org.apache.hadoop.hdfs.hoss.db.PathStore.java
/** * /* w ww . ja v a 2 s. c o m*/ * @param pathId * @param objId:the index-th block in fileblockstore * @param offset * @return */ public PathPosition put(long objId, long pathId, long offset) { String path = getFixedPath(pathId); //set the block index in the fileblockstore final WriteBuffer wbuf = fbs.set((int) objId); final ByteBuffer buf = wbuf.buf(); StringSerializer.fromStringToBuffer(buf, path, PATHWIDTH); buf.putLong(offset); buf.flip(); wbuf.save(); PathPosition pp = new PathPosition(path, offset); return pp; }
From source file:jnative.io.AIO.java
private ByteBuffer getDirect(ByteBuffer buf) { if (buf instanceof DirectBuffer) return buf; int pos = buf.position(); int lim = buf.limit(); assert (pos <= lim); int rem = (pos <= lim ? lim - pos : 0); ByteBuffer bb = ByteBuffer.allocate(rem); bb.put(buf);//from ww w . j a va2 s. com bb.flip(); return bb; }
From source file:com.jivesoftware.os.rcvs.api.keys.SymetricalHashableKeyTest.java
License:asdf
public long bytesLong(byte[] bytes) { ByteBuffer buffer = ByteBuffer.allocate(8); buffer.put(bytes);/*from w w w . j a va 2s .com*/ buffer.flip(); //need flip return buffer.getLong(); }
From source file:gobblin.util.io.StreamUtilsTest.java
private void verifyBuffer(byte[] srcBytes, ByteBuffer buffer) throws IOException { buffer.put(srcBytes);/*from w w w . j a v a 2 s .c om*/ buffer.flip(); ByteArrayOutputStream bOs = new ByteArrayOutputStream(); StreamUtils.byteBufferToOutputStream(buffer, bOs); Assert.assertEquals(bOs.toByteArray(), srcBytes); bOs = new ByteArrayOutputStream(); buffer.rewind(); // consume one character from the buf; make sure it is not included in the output by // byteBufferToOutputStream buffer.getChar(); StreamUtils.byteBufferToOutputStream(buffer, bOs); byte[] offByTwo = bOs.toByteArray(); Assert.assertEquals(offByTwo.length, srcBytes.length - 2); for (int i = 0; i < offByTwo.length; i++) { Assert.assertEquals(offByTwo[i], srcBytes[i + 2]); } }
From source file:com.github.matthesrieke.jprox.JProxViaParameterServlet.java
private void copyChannel(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);/* ww w .j ava2 s. c om*/ buffer.compact(); } buffer.flip(); while (buffer.hasRemaining()) { dest.write(buffer); } }
From source file:com.bennavetta.appsite.file.ResourceService.java
public Resource create(String path, MediaType mime, ReadableByteChannel src) throws IOException { String normalized = PathUtils.normalize(path); log.debug("Creating resource {}", normalized); AppEngineFile file = fs.createNewBlobFile(mime.toString(), normalized); FileWriteChannel channel = fs.openWriteChannel(file, true); MessageDigest digest = null;//from w w w . j a v a2 s . co m try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 not available", e); } ByteBuffer buf = ByteBuffer.allocateDirect(bufferSize.get()); while (src.read(buf) != -1 || buf.position() != 0) { buf.flip(); int read = channel.write(buf); if (read != 0) { int origPos = buf.position(); int origLimit = buf.limit(); buf.position(origPos - read); buf.limit(origPos); digest.update(buf); buf.limit(origLimit); buf.position(origPos); } buf.compact(); } channel.closeFinally(); Resource resource = new Resource(normalized, fs.getBlobKey(file).getKeyString(), digest.digest(), mime); resource.save(); return resource; }
From source file:com.buaa.cfs.nfs3.FileHandle.java
private long bytesToLong(byte[] data) { ByteBuffer buffer = ByteBuffer.allocate(8); for (int i = 0; i < 8; i++) { buffer.put(data[i]);/*from w w w.j av a 2 s .co m*/ } buffer.flip();// need flip return buffer.getLong(); }