List of usage examples for java.nio ByteBuffer limit
public final int limit()
From source file:io.alicorn.server.http.LoginEndpoint.java
public static String hash(char[] chars) { //Parse chars into bytes for hashing. CharBuffer charBuffer = CharBuffer.wrap(chars); ByteBuffer byteBuffer = charset.encode(charBuffer); byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); //Clear temporary arrays of any data. Arrays.fill(charBuffer.array(), '\u0000'); Arrays.fill(byteBuffer.array(), (byte) 0); //Generate the SHA-256 hash. String hash = hash(bytes);/*from w w w. j a v a 2 s.c om*/ //Clear remaining arrays of any data. Arrays.fill(bytes, (byte) 0); return hash; }
From source file:com.icloud.framework.core.nio.ByteBufferUtil.java
public static ByteBuffer clone(ByteBuffer o) { assert o != null; if (o.remaining() == 0) return ByteBuffer.wrap(ArrayUtils.EMPTY_BYTE_ARRAY); ByteBuffer clone = ByteBuffer.allocate(o.remaining()); if (o.isDirect()) { for (int i = o.position(); i < o.limit(); i++) { clone.put(o.get(i));//from w w w. ja v a 2 s . c o m } clone.flip(); } else { System.arraycopy(o.array(), o.arrayOffset() + o.position(), clone.array(), 0, o.remaining()); } return clone; }
From source file:de.csdev.ebus.utils.EBusUtils.java
/** * Convert a ByteBuffer to a byte array/*from w w w . j a v a 2 s . c o m*/ * * @param buffer * @return */ public static byte[] toByteArray(ByteBuffer buffer) { int size = 0; if (buffer.position() == 0) { size = buffer.limit(); } else { size = buffer.position(); } byte[] data = new byte[size]; ((ByteBuffer) buffer.duplicate().clear()).get(data); return data; }
From source file:Main.java
public static ReadableByteChannel asReadableByteChannel(final ByteBuffer buffer) { return new ReadableByteChannel() { private boolean open = true; public int read(ByteBuffer dst) throws IOException { if (open == false) { throw new ClosedChannelException(); }/* w ww. j a v a 2 s . co m*/ final int p = buffer.position(); final int l = buffer.limit(); final int r = dst.remaining(); buffer.limit(p + r); dst.put(buffer); buffer.limit(l); return r; } public void close() throws IOException { open = false; } public boolean isOpen() { return open; } }; }
From source file:de.csdev.ebus.utils.EBusUtils.java
/** * Generates a string hex dump from a ByteBuffer * * @param data The source//from w w w. ja v a 2 s . c om * @return The StringBuilder with hex dump */ static public StringBuilder toHexDumpString(ByteBuffer data) { int size = 0; if (data.position() == 0) { size = data.limit(); } else { size = data.position(); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < size; i++) { byte c = data.get(i); if (i > 0) { sb.append(' '); } sb.append(toHexDumpString(c)); } return sb; }
From source file:Main.java
/** * @return//w w w .ja v a2s .c om */ public static WritableByteChannel asWritableByteChannel(final ByteBuffer buffer) { return new WritableByteChannel() { private boolean open = true; public int write(ByteBuffer src) throws IOException { if (open == false) { throw new ClosedChannelException(); } final int p = buffer.position(); final int l = buffer.limit(); final int r = src.remaining(); buffer.limit(l + r); buffer.position(l); buffer.put(src); buffer.position(p); return r; } public void close() throws IOException { open = false; } public boolean isOpen() { return open; } }; }
From source file:com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.java
public static byte[] getBytesFromByteBuffer(ByteBuffer buffer) { // Be careful *NOT* to perform anything that will modify the buffer's position or limit byte[] bytes = new byte[buffer.limit() - buffer.position()]; if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.position(), bytes, 0, bytes.length); } else {// w w w . j av a 2 s.c om buffer.duplicate().get(bytes, buffer.position(), bytes.length); } return bytes; }
From source file:org.commoncrawl.service.queryserver.master.S3Helper.java
static int scanForGZIPHeader(ByteBuffer byteBuffer) throws IOException { LOG.info("*** SCANNING FOR GZIP MAGIC Bytes:" + Byte.toString((byte) StreamingArcFileReader.GZIP_MAGIC) + " " + Byte.toString((byte) (StreamingArcFileReader.GZIP_MAGIC >> 8)) + " BufferSize is:" + byteBuffer.limit() + " Remaining:" + byteBuffer.remaining()); int limit = byteBuffer.limit(); while (byteBuffer.position() + 2 < limit) { //LOG.info("Reading Byte At:"+ byteBuffer.position()); int b = byteBuffer.get(); //LOG.info("First Byte is:"+ b); if (b == (byte) (StreamingArcFileReader.GZIP_MAGIC)) { byteBuffer.mark();/*w w w . ja v a2 s . c om*/ byte b2 = byteBuffer.get(); //LOG.info("Second Byte is:"+ b2); if (b2 == (byte) (StreamingArcFileReader.GZIP_MAGIC >> 8)) { byte b3 = byteBuffer.get(); if (b3 == Deflater.DEFLATED) { LOG.info("Found GZip Magic at:" + (byteBuffer.position() - 3)); return byteBuffer.position() - 3; } } byteBuffer.reset(); } } LOG.error("Failed to Find GZIP Magic!!"); //LOG.error(Arrays.toString(byteBuffer.array())); return -1; }
From source file:com.icloud.framework.core.util.FBUtilities.java
public static String bytesToHex(ByteBuffer bytes) { StringBuilder sb = new StringBuilder(); for (int i = bytes.position() + bytes.arrayOffset(); i < bytes.limit() + bytes.arrayOffset(); i++) { int bint = bytes.array()[i] & 0xff; if (bint <= 0xF) // toHexString does not 0 pad its results. sb.append("0"); sb.append(Integer.toHexString(bint)); }/* ww w . j av a 2 s . c o m*/ return sb.toString(); }
From source file:Main.java
public static ByteBuffer clone(final ByteBuffer buf) { if (buf == null) { return null; }//from w w w. j a va 2s. c o m buf.rewind(); final ByteBuffer copy; if (buf.isDirect()) { copy = createByteBuffer(buf.limit()); } else { copy = createByteBufferOnHeap(buf.limit()); } copy.put(buf); return copy; }