List of usage examples for java.nio ByteBuffer wrap
public static ByteBuffer wrap(byte[] array, int start, int len)
From source file:org.ojai.beans.jackson.DocumentGenerator.java
@Override public void writeBinary(Base64Variant bv, byte[] data, int offset, int len) throws IOException { ByteBuffer buff = ByteBuffer.wrap(data, offset, len); if (inMap()) { b.put(currFieldName, buff);/*w ww . j ava2 s.c o m*/ } else { b.add(buff); } }
From source file:com.bigdata.rdf.sail.webapp.client.EntityContentProvider.java
@Override public Iterator<ByteBuffer> iterator() { try {// ww w . j a v a 2s.c o m final InputStream instr; if (m_entity.isStreaming()) { instr = m_entity.getContent(); } else { // If Apache Entity does not stream then we need to double buffer to an output stream - not ideal final ByteArrayOutputStream streambuf = new ByteArrayOutputStream(); m_entity.writeTo(streambuf); instr = new ByteArrayInputStream(streambuf.toByteArray()); } return new Iterator<ByteBuffer>() { boolean eof = false; int bufindex = 0; @Override public boolean hasNext() { return !eof; } @Override public ByteBuffer next() { final byte[] buf = new byte[1024]; try { final int rdlen = instr.read(buf); // System.out.println("Returning ByteBuffer[" + bufindex++ + "] length: " + rdlen); if (rdlen == -1) { eof = true; return ByteBuffer.wrap(buf, 0, 0); } else { return ByteBuffer.wrap(buf, 0, rdlen); } } catch (IOException e) { throw new RuntimeException(e); } } @Override public void remove() { throw new UnsupportedOperationException(); } }; } catch (Exception e) { throw new RuntimeException("Unexpected", e); } }
From source file:com.alibaba.otter.shared.common.utils.ByteUtils.java
public static long bytes2long(byte[] data, int offset) { ByteBuffer buffer = ByteBuffer.wrap(data, offset, 8); return buffer.getLong(); }
From source file:com.l2jfree.security.NewCipher.java
/** * Verifies a packet's checksum./* ww w.java 2 s. c o m*/ * * @deprecated Legacy method * @param raw data * @param offset offset to the packet's body * @param size packet's body size * @return whether packet integrity is OK or not * @see #verifyChecksum(ByteBuffer, int, int, boolean) */ @Deprecated public static boolean verifyChecksum(byte[] raw, final int offset, final int size) { return verifyChecksum(ByteBuffer.wrap(raw, offset, size), size); }
From source file:net.pms.util.OpenSubtitle.java
public static String computeHash(InputStream stream, long length) throws IOException { int chunkSizeForFile = (int) Math.min(HASH_CHUNK_SIZE, length); // Buffer that will contain the head and the tail chunk, chunks will overlap if length is smaller than two chunks byte[] chunkBytes = new byte[(int) Math.min(2 * HASH_CHUNK_SIZE, length)]; long head;/*from w ww .j a va 2 s .co m*/ long tail; try (DataInputStream in = new DataInputStream(stream)) { // First chunk in.readFully(chunkBytes, 0, chunkSizeForFile); long position = chunkSizeForFile; long tailChunkPosition = length - chunkSizeForFile; // Seek to position of the tail chunk, or not at all if length is smaller than two chunks while (position < tailChunkPosition && (position += in.skip(tailChunkPosition - position)) >= 0) ; // Second chunk, or the rest of the data if length is smaller than two chunks in.readFully(chunkBytes, chunkSizeForFile, chunkBytes.length - chunkSizeForFile); head = computeHashForChunk(ByteBuffer.wrap(chunkBytes, 0, chunkSizeForFile)); tail = computeHashForChunk( ByteBuffer.wrap(chunkBytes, chunkBytes.length - chunkSizeForFile, chunkSizeForFile)); } return String.format("%016x", length + head + tail); }
From source file:com.joyent.manta.http.entity.ExposedByteArrayEntity.java
@Override public ByteBuffer getBackingBuffer() { return ByteBuffer.wrap(buffer, offset, length); }
From source file:net.lightbody.bmp.proxy.jetty.http.nio.SocketChannelOutputStream.java
public void write(byte[] buf, int offset, int length) throws IOException { if (length > _buffer.capacity()) _flush = ByteBuffer.wrap(buf, offset, length); else {//from w ww .j av a 2 s . com _buffer.clear(); _buffer.put(buf, offset, length); _buffer.flip(); _flush = _buffer; } flushBuffer(); }
From source file:eu.stratosphere.arraymodel.io.StringInputFormat.java
public boolean readRecord(Value[] target, byte[] bytes, int offset, int numBytes) { StringValue str = this.theString; if (this.ascii) { str.setValueAscii(bytes, offset, numBytes); } else {//ww w . j ava 2s . co m ByteBuffer byteWrapper = this.byteWrapper; if (bytes != byteWrapper.array()) { byteWrapper = ByteBuffer.wrap(bytes, 0, bytes.length); this.byteWrapper = byteWrapper; } byteWrapper.clear(); byteWrapper.position(offset); byteWrapper.limit(offset + numBytes); try { CharBuffer result = this.decoder.decode(byteWrapper); str.setValue(result); } catch (CharacterCodingException e) { byte[] copy = new byte[numBytes]; System.arraycopy(bytes, offset, copy, 0, numBytes); LOG.warn("Line could not be encoded: " + Arrays.toString(copy), e); return false; } } target[0] = str; return true; }
From source file:cn.ac.ncic.mastiff.io.coding.RunLengthEncodingByteWriter.java
@Override public byte[] getPage() throws IOException { if (dataOffset == 3 * Bytes.SIZEOF_INT) // no data appended return null; /** We do not compressed the page of data */ if (compressAlgo == null) { bb = ByteBuffer.wrap(page, 0, page.length); bb.putInt(dataOffset);//from w w w . j a v a 2 s . c om bb.putInt(numPairs); bb.putInt(startPos); return page; } else { // compress a page using the specified <i>Algorithm</i> outputBuffer.reset(); outputBuffer.writeInt(dataOffset); outputBuffer.writeInt(numPairs); outputBuffer.writeInt(startPos); outputBuffer.writeInt(page.length - 12); compressor = compressAlgo.getCompressor(); // create the compression stream OutputStream os = this.compressAlgo.createCompressionStream(outputBuffer, compressor, 0); os.write(page, 3 * Bytes.SIZEOF_INT, dataOffset - 3 * Bytes.SIZEOF_INT); os.flush(); this.compressAlgo.returnCompressor(compressor); this.compressor = null; return outputBuffer.getData(); } }
From source file:com.github.neoio.nio.util.NIOUtils.java
public static int writeToChannel(WritableByteChannel channel, byte array[], int offset, int length) throws NetIOException { try {/* ww w .j ava2 s . c o m*/ return channel.write(ByteBuffer.wrap(array, offset, length)); } catch (IndexOutOfBoundsException e) { throw new NetIOException(e); } catch (IOException e) { throw new NetIOException(e); } }