/** * Read a buffer into a Byte array for the given offset and length */ public static byte[] readBytes(ByteBuffer buffer, int offset, int length) { byte[] dest = new byte[length]; if (buffer.hasArray()) { System.arraycopy(buffer.array(), buffer.arrayOffset() + offset, dest, 0, length); } else { buffer.mark(); buffer.position(offset); buffer.get(dest, 0, length); buffer.reset(); } return dest; }
public static String stringUtf8( ByteBuffer bytes ) throws InvalidDataException { CharsetDecoder decode = Charset.forName( "UTF8" ).newDecoder(); decode.onMalformedInput( codingErrorAction ); decode.onUnmappableCharacter( codingErrorAction ); String s; try { bytes.mark(); s = decode.decode( bytes ).toString(); bytes.reset(); } catch ( CharacterCodingException e ) { throw new InvalidDataException( CloseFrame.NO_UTF8, e ); } return s; }
@Override public ByteBuffer readBinaryValue(long chain) { // first get total element size and allocate buffer long element = chain + this.totalChainHeaderSize; int totalLength = DETACHED_CONTIGUOUS_CHAIN_HEADER_SIZE; do { totalLength += ELEMENT_HEADER_SIZE + readElementLength(element); element = storage.readLong(element + ELEMENT_HEADER_NEXT_OFFSET); } while (element != chain); final ByteBuffer detachedContiguousBuffer = ByteBuffer.allocate(totalLength); // one way for layers above to extract encoding is to put the encoding of the chain address in the value detachedContiguousBuffer.putLong(chain); // now add the elements to the buffer element = chain + this.totalChainHeaderSize; do { final int startPosition = detachedContiguousBuffer.position(); detachedContiguousBuffer.put(storage.readBuffer(element, ELEMENT_HEADER_SIZE + readElementLength(element))); detachedContiguousBuffer.mark(); detachedContiguousBuffer.putLong(startPosition + ELEMENT_HEADER_NEXT_OFFSET, -1L); detachedContiguousBuffer.reset(); element = storage.readLong(element + ELEMENT_HEADER_NEXT_OFFSET); } while (element != chain); return (ByteBuffer)detachedContiguousBuffer.flip(); }
/** * Returns the next code point at the current position in * the buffer. The buffer's position will be incremented. * Any mark set on this buffer will be changed by this method! */ public static int bytesToCodePoint(ByteBuffer bytes) { bytes.mark(); byte b = bytes.get(); bytes.reset(); int extraBytesToRead = bytesFromUTF8[(b & 0xFF)]; if (extraBytesToRead < 0) return -1; // trailing byte! int ch = 0; switch (extraBytesToRead) { case 5: ch += (bytes.get() & 0xFF); ch <<= 6; /* remember, illegal UTF-8 */ case 4: ch += (bytes.get() & 0xFF); ch <<= 6; /* remember, illegal UTF-8 */ case 3: ch += (bytes.get() & 0xFF); ch <<= 6; case 2: ch += (bytes.get() & 0xFF); ch <<= 6; case 1: ch += (bytes.get() & 0xFF); ch <<= 6; case 0: ch += (bytes.get() & 0xFF); } ch -= offsetsFromUTF8[extraBytesToRead]; return ch; }
static boolean match(SegmentBuffer result, List<byte[]> requestTags, int tagTypeCode) { ByteBuffer message = result.getBuffer(); message.mark(); byte flag = message.get(); if (!Flags.hasTags(flag)) { message.reset(); return false; final byte tagsSize = message.get(); if (tagsSize == 1) { final short len = message.getShort(); final byte[] tag = new byte[len]; message.get(tag); message.reset(); return matchOneTag(tag, requestTags, tagTypeCode); tags.add(bs); message.reset(); return matchTags(tags, requestTags, tagTypeCode);
public static void main(String[] args) throws IOException { Movie movie = MovieCreator.build(DumpAmf0TrackToPropertyFile.class.getProtectionDomain().getCodeSource().getLocation().getFile() + "/example.f4v"); for (Track track : movie.getTracks()) { if (track.getHandler().equals("data") ) { long time = 0; Iterator<Sample> samples = track.getSamples().iterator(); Properties properties = new Properties(); File f = File.createTempFile(DumpAmf0TrackToPropertyFile.class.getSimpleName(), "" + track.getTrackMetaData().getTrackId()); for (long decodingTime : track.getSampleDurations()) { ByteBuffer sample = samples.next().asByteBuffer(); byte[] sampleBytes = new byte[sample.limit()]; sample.reset(); sample.get(sampleBytes); properties.put("" + time, new String(Base64.encodeBase64(sampleBytes, false, false))); time += decodingTime; } FileOutputStream fos = new FileOutputStream(f); System.err.println(properties); properties.store(fos, ""); } } }
/** * Similar to {@link ByteBuffer}.reset(), ensures that this MBB * is reset back to last marked position. * @return This MBB */ @Override public MultiByteBuff reset() { // when the buffer is moved to the next one.. the reset should happen on the previous marked // item and the new one should be taken as the base if (this.markedItemIndex < 0) throw new InvalidMarkException(); ByteBuffer markedItem = this.items[this.markedItemIndex]; markedItem.reset(); this.curItem = markedItem; // All items after the marked position upto the current item should be reset to 0 for (int i = this.curItemIndex; i > this.markedItemIndex; i--) { this.items[i].position(0); } this.curItemIndex = this.markedItemIndex; return this; }
private ByteBuffer allocateEmptyHLLBuffer(boolean direct, boolean aligned, int offset) { final int size = HyperLogLogCollector.getLatestNumBytesForDenseStorage(); final byte[] EMPTY_BYTES = HyperLogLogCollector.makeEmptyVersionedByteArray(); final ByteBuffer buf; if (direct) { if (aligned) { buf = ByteBuffers.allocateAlignedByteBuffer(size + offset, CACHE_LINE); buf.position(offset); buf.mark(); buf.limit(size + offset); } else { buf = ByteBuffer.allocateDirect(size); buf.mark(); buf.limit(size); } buf.put(EMPTY_BYTES); buf.reset(); } else { buf = ByteBuffer.allocate(size); buf.limit(size); buf.put(EMPTY_BYTES); buf.rewind(); } return buf; }
/** * Write all records to the given channel (including partial records). * @param channel The channel to write to * @return The number of bytes written * @throws IOException For any IO errors writing to the channel */ public int writeFullyTo(GatheringByteChannel channel) throws IOException { buffer.mark(); int written = 0; while (written < sizeInBytes()) written += channel.write(buffer); buffer.reset(); return written; }
public synchronized boolean unwrapInput() { boolean success = false; boolean shouldUnwrap = true; while (!isClosed() && shouldUnwrap && netIn.position() > 0) { debug("- UNWRAP"); netIn.flip(); // prepare for reading netIn.mark(); // backup, to rollback in case of underflow SSLEngineResult result = unwrap(netIn, appIn); boolean underflow = result.getStatus().equals(SSLEngineResult.Status.BUFFER_UNDERFLOW); if (!underflow) { // prepare for writing netIn.compact(); } else { // rollback netIn.reset(); netIn.compact(); } appIn.flip(); conn.input.append(appIn); appIn.clear(); reactToResult(result); shouldUnwrap = !result.getStatus().equals(SSLEngineResult.Status.BUFFER_UNDERFLOW) && !result.getHandshakeStatus().equals(SSLEngineResult.HandshakeStatus.NEED_TASK) && !result.getHandshakeStatus().equals(SSLEngineResult.HandshakeStatus.NEED_WRAP); success = true; } return success; }