@Override public void append(Framedata nextframe) throws InvalidFrameException { ByteBuffer b = nextframe.getPayloadData(); if (unmaskedpayload == null) { unmaskedpayload = ByteBuffer.allocate(b.remaining()); b.mark(); unmaskedpayload.put(b); b.reset(); } else { b.mark(); unmaskedpayload.position(unmaskedpayload.limit()); unmaskedpayload.limit(unmaskedpayload.capacity()); if (b.remaining() > unmaskedpayload.remaining()) { ByteBuffer tmp = ByteBuffer.allocate(b.remaining() + unmaskedpayload.capacity()); unmaskedpayload.flip(); tmp.put(unmaskedpayload); tmp.put(b); unmaskedpayload = tmp; } else { unmaskedpayload.put(b); } unmaskedpayload.rewind(); b.reset(); } fin = nextframe.isFin(); }
public void applyToMapping(AbstractTaggedMapping m) { readLength = m.getLength(); forwardStrand = !m.isOnReverse(); sequence = m.getSequence(); if (!forwardStrand) { // calculate the reversed complement if (revComplement.capacity() < readLength) // ensure space is sufficient { revComplement = ByteBuffer.allocate(readLength); revComplement.mark(); } // now revComplement the read into the buffer revComplement.reset(); int startPos = sequence.position(); for (int pos = startPos + readLength - 1; pos >= startPos; --pos) revComplement.put(complementByte.get(sequence.get(pos))); revComplement.reset(); sequence = revComplement; } // now sequence is what was read by the sequencer // Calculate the dinucleotides. We calculate them starting from the beginning // of 'sequence', but the order in which they are inserted into 'values' must // match the original base order. final byte[] seqArray = sequence.array(); final int sequenceStart = sequence.position(); int valuesPtr; int valuesIncrement; if (forwardStrand) { values[0] = NN; valuesPtr = 1; valuesIncrement = 1; } else { values[readLength - 1] = NN; valuesPtr = readLength - 2; valuesIncrement = -1; } // for each "previous" base, if it's an N insert a value of NN, otherwise the // dinucleotide (previous, previous+1) for (int previous = 0; previous < readLength - 1; ++previous) { if (seqArray[sequenceStart + previous] == 'N') // if previous base is an N values[valuesPtr] = NN; else values[valuesPtr] = new String( seqArray, sequenceStart + previous, 2); // string length 2 starting from seqArray[previous] valuesPtr += valuesIncrement; } }
/** * Read line. * * @param buf the buf * @return the string */ public static String readLine(ByteBuffer buf) { boolean completed = false; buf.mark(); while (buf.hasRemaining() && !completed) { byte b = buf.get(); if (b == '\r') { if (buf.hasRemaining() && buf.get() == '\n') { completed = true; } } } if (!completed) { return null; } int limit = buf.position(); buf.reset(); int length = limit - buf.position(); byte[] tmp = new byte[length]; buf.get(tmp, 0, length); try { String line = new String(tmp, "US-ASCII"); if (log.isLoggable(Level.FINEST)) { log.finest(line.trim()); } return line; } catch (UnsupportedEncodingException e) {; } return null; }
/** * 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; }
protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) { while (true) { if (in.remaining() < 1) return CoderResult.UNDERFLOW; in.mark(); byte b = in.get(); char c = (char) (b & 0x7f); if (CodingErrorAction.REPORT == unmappableCharacterAction() && !PrintableCharset.isPrintableChar(c)) { /* "bug" fix for 359010 return CoderResult.unmappableForLength(1); */ continue; } if (out.remaining() < 1) { in.reset(); return CoderResult.OVERFLOW; } out.put(c); } }
private CommandArgs<K, V> write(byte[] arg) { buffer.mark(); if (buffer.remaining() < arg.length) { int estimate = buffer.remaining() + arg.length + 10; realloc(max(buffer.capacity() * 2, estimate)); } while (true) { try { buffer.put((byte) '$'); write(arg.length); buffer.put(CRLF); buffer.put(arg); buffer.put(CRLF); break; } catch (BufferOverflowException e) { buffer.reset(); realloc(buffer.capacity() * 2); } } count++; return this; }
private void readTagDescriptors() throws IOException { int tagDescriptorOffset = tagDescriptorsOffset; final byte[] bytes = data.array(); final Charset utf8 = Charset.forName("utf-8"); for (int i = 0; i < tagCount; i++) { final int dNameOffset = data.getInt(tagDescriptorOffset); tagDescriptorOffset += 4; final int tagIndex = data.get(tagDescriptorOffset) & 0xff; tagDescriptorOffset += 4; final int nameOffset = tagNamesOffset + dNameOffset; int stringLength = 0; while (bytes[nameOffset + stringLength] != 0) { stringLength++; } data.mark(); data.position(nameOffset); data.limit(nameOffset + stringLength); names[tagIndex] = utf8.decode(data); data.reset(); data.limit(data.capacity()); } }
/** Test verifies that the mark, position and limit are unchanged by the checksum operation. */ public void test_checksum03() { byte[] data = new byte[100]; r.nextBytes(data); ByteBuffer buf = ByteBuffer.wrap(data); // set the limit. buf.limit(20); /* * set the mark (we have to choose a mark less than the position we will * set and test below or the mark will be discarded when we set the * position). */ buf.position(9); buf.mark(); // set the position. buf.position(12); chk.checksum(buf, 0, data.length); // verify limit unchanged. assertEquals(20, buf.limit()); // verify position unchanged. assertEquals(12, buf.position()); // reset the buffer to the mark and verify the mark was not changed. buf.reset(); assertEquals(9, buf.position()); }
private CommandArgs<K, V> write(String arg) { int length = arg.length(); buffer.mark(); if (buffer.remaining() < length) { int estimate = buffer.remaining() + length + 10; realloc(max(buffer.capacity() * 2, estimate)); } while (true) { try { buffer.put((byte) '$'); write(length); buffer.put(CRLF); for (int i = 0; i < length; i++) { buffer.put((byte) arg.charAt(i)); } buffer.put(CRLF); break; } catch (BufferOverflowException e) { buffer.reset(); realloc(buffer.capacity() * 2); } } count++; return this; }
public void readIDField(ByteBuffer buf, GeoEvent geoEvent, int i) throws MessagingException, FieldException { // Check if there is more data int rm = buf.remaining(); if (buf.remaining() > 8) { buf.mark(); readString(buf, 1); // Read out semi-colon ; String idName = readString(buf, 3); // Read out ID= if (idName.equals("ID=") == true) { // read until ';' to get value of the ID String id = ""; while (true) { String data = readString(buf, 1); if (data.equals(";") == false) { id += data; } else { break; } } geoEvent.setField(i++, id); } else // no ID= field { buf.reset(); // set the buf position back to the marked position } } }
/** * Hexdump. * * @param bb bytebuffer * @param offset offset * @return String */ public static String hexdump(ByteBuffer bb, int offset) { byte[] bit = new byte[bb.remaining()]; bb.mark(); bb.get(bit); bb.reset(); return hexdump(bit, offset); }
@Override public List<Framedata> translateFrame(ByteBuffer buffer) throws LimitExedeedException, InvalidDataException { List<Framedata> frames = new LinkedList<Framedata>(); Framedata cur; if (incompleteframe != null) { // complete an incomplete frame while (true) { try { buffer.mark(); int available_next_byte_count = buffer.remaining(); // The number of bytes received int expected_next_byte_count = incompleteframe.remaining(); // The number of bytes to complete the incomplete frame if (expected_next_byte_count > available_next_byte_count) { // did not receive enough bytes to complete the frame incompleteframe.put(buffer.array(), buffer.position(), available_next_byte_count); buffer.position(buffer.position() + available_next_byte_count); return Collections.emptyList(); } incompleteframe.put(buffer.array(), buffer.position(), expected_next_byte_count); buffer.position(buffer.position() + expected_next_byte_count); cur = translateSingleFrame((ByteBuffer) incompleteframe.duplicate().position(0)); frames.add(cur); incompleteframe = null; break; // go on with the normal frame receival } catch (IncompleteException e) { // extending as much as suggested @SuppressWarnings("unused") int oldsize = incompleteframe.limit(); ByteBuffer extendedframe = ByteBuffer.allocate(checkAlloc(e.getPreferedSize())); assert (extendedframe.limit() > incompleteframe.limit()); incompleteframe.rewind(); extendedframe.put(incompleteframe); incompleteframe = extendedframe; return translateFrame(buffer); } } } while (buffer.hasRemaining()) { // Read as much as possible full frames buffer.mark(); try { cur = translateSingleFrame(buffer); frames.add(cur); } catch (IncompleteException e) { // remember the incomplete data buffer.reset(); int pref = e.getPreferedSize(); incompleteframe = ByteBuffer.allocate(checkAlloc(pref)); incompleteframe.put(buffer); break; } } return frames; }
public static void writeChannel() { FileOutputStream fileOut = null; FileChannel channel = null; try { InputStream input = FileChannelIoMain.class.getClassLoader().getResourceAsStream("test.txt"); byte[] byteBuffer = new byte[1024]; int size = 0; StringBuilder builer = new StringBuilder(); while ((size = input.read(byteBuffer)) != -1) { builer.append(new String(byteBuffer)); } System.out.println(builer.toString()); File file = new File("./test.txt"); if (!file.exists()) { file.createNewFile(); file.setWritable(true); } fileOut = new FileOutputStream(file, true); channel = fileOut.getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(1024); buffer.putChar('\n'); buffer.put("我是一个中国人,我的家乡在北京".getBytes("utf-8")); buffer.mark(); buffer.putChar('&'); buffer.putChar('#'); buffer.reset(); buffer.flip(); // buffer.rewind(); channel.write(buffer); buffer.clear(); } catch (Throwable e) { e.printStackTrace(); } finally { try { if (fileOut != null) { fileOut.close(); fileOut = null; } if (channel != null) { channel.close(); channel = null; } } catch (Throwable e) { e.printStackTrace(); } } }
/** Allocate a slice of the given length. */ public ByteBuffer clone(ByteBuffer buffer) { assert buffer != null; ByteBuffer cloned = allocate(buffer.remaining()); cloned.mark(); cloned.put(buffer.duplicate()); cloned.reset(); return cloned; }
/** Allocate a slice of the given length. */ public ByteBuffer clone(ByteBuffer buffer) { assert buffer != null; if (buffer.remaining() == 0) return ByteBufferUtil.EMPTY_BYTE_BUFFER; ByteBuffer cloned = allocate(buffer.remaining()); cloned.mark(); cloned.put(buffer.duplicate()); cloned.reset(); return cloned; }
/** * Decode a region of the buffer under the specified character set if possible. * * <p>If the byte stream cannot be decoded that way, the platform default is tried and if that too * fails, an exception is thrown. * * @param cs character set to use when decoding the buffer. * @param buffer buffer to pull raw bytes from. * @param start first position within the buffer to take data from. * @param end one position past the last location within the buffer to take data from. * @return a string representation of the range <code>[start,end)</code>, after decoding the * region through the specified character set. * @throws CharacterCodingException the input is not in any of the tested character sets. */ public static String decodeNoFallback( final Charset cs, final byte[] buffer, final int start, final int end) throws CharacterCodingException { final ByteBuffer b = ByteBuffer.wrap(buffer, start, end - start); b.mark(); // Try our built-in favorite. The assumption here is that // decoding will fail if the data is not actually encoded // using that encoder. // try { return decode(b, Constants.CHARSET); } catch (CharacterCodingException e) { b.reset(); } if (!cs.equals(Constants.CHARSET)) { // Try the suggested encoding, it might be right since it was // provided by the caller. // try { return decode(b, cs); } catch (CharacterCodingException e) { b.reset(); } } // Try the default character set. A small group of people // might actually use the same (or very similar) locale. // final Charset defcs = Charset.defaultCharset(); if (!defcs.equals(cs) && !defcs.equals(Constants.CHARSET)) { try { return decode(b, defcs); } catch (CharacterCodingException e) { b.reset(); } } throw new CharacterCodingException(); }
/** 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; }
@Override public byte[] nextPacket(final ByteBuffer byteBuffer) throws ProtocolViolationException { if (byteBuffer.remaining() < m_headerSize) return null; byteBuffer.mark(); final int length = NIOUtils.getPacketSizeFromByteBuffer(byteBuffer, m_headerSize, m_bigEndian); if (byteBuffer.remaining() >= length) { final byte[] packet = new byte[length]; byteBuffer.get(packet); return packet; } else { byteBuffer.reset(); return null; } }
public static String byteBufferToHexString(ByteBuffer bb) { bb.mark(); StringBuilder sb = new StringBuilder(); int offset = 0; while (bb.hasRemaining()) { if (offset % 33 == 0) sb.append("\n"); String s = Integer.toString(bb.get() & 0xFF, 16); offset++; if (s.length() == 1) sb.append("0"); sb.append(s.toUpperCase()); } bb.reset(); return sb.toString(); }
/** * Finds any occurence of <code>what</code> in the backing buffer, starting as position <code> * start</code>. The starting position is measured in bytes and the return value is in terms of * byte position in the buffer. The backing buffer is not converted to a string for this * operation. * * @return byte position of the first occurence of the search string in the UTF-8 buffer or -1 if * not found */ public int find(String what, int start) { try { ByteBuffer src = ByteBuffer.wrap(this.bytes, 0, this.length); ByteBuffer tgt = encode(what); byte b = tgt.get(); src.position(start); while (src.hasRemaining()) { if (b == src.get()) { // matching first byte src.mark(); // save position in loop tgt.mark(); // save position in target boolean found = true; int pos = src.position() - 1; while (tgt.hasRemaining()) { if (!src.hasRemaining()) { // src expired first tgt.reset(); src.reset(); found = false; break; } if (!(tgt.get() == src.get())) { tgt.reset(); src.reset(); found = false; break; // no match } } if (found) return pos; } } return -1; // not found } catch (CharacterCodingException e) { // can't get here e.printStackTrace(); return -1; } }
private final SendBuffer acquire(SctpPayload message) { final ChannelBuffer src = message.getPayloadBuffer(); final int streamNo = message.getStreamIdentifier(); final int protocolId = message.getProtocolIdentifier(); final int size = src.readableBytes(); if (size == 0) { return EMPTY_BUFFER; } if (src.isDirect()) { return new UnpooledSendBuffer(streamNo, protocolId, src.toByteBuffer()); } if (src.readableBytes() > DEFAULT_PREALLOCATION_SIZE) { return new UnpooledSendBuffer(streamNo, protocolId, src.toByteBuffer()); } Preallocation current = this.current; ByteBuffer buffer = current.buffer; int remaining = buffer.remaining(); PooledSendBuffer dst; if (size < remaining) { int nextPos = buffer.position() + size; ByteBuffer slice = buffer.duplicate(); buffer.position(align(nextPos)); slice.limit(nextPos); current.refCnt++; dst = new PooledSendBuffer(streamNo, protocolId, current, slice); } else if (size > remaining) { this.current = current = getPreallocation(); buffer = current.buffer; ByteBuffer slice = buffer.duplicate(); buffer.position(align(size)); slice.limit(size); current.refCnt++; dst = new PooledSendBuffer(streamNo, protocolId, current, slice); } else { // size == remaining current.refCnt++; this.current = getPreallocation0(); dst = new PooledSendBuffer(streamNo, protocolId, current, current.buffer); } ByteBuffer dstbuf = dst.buffer; dstbuf.mark(); src.getBytes(src.readerIndex(), dstbuf); dstbuf.reset(); return dst; }
private SendBuffer acquire(ChannelBuffer src) { final int size = src.readableBytes(); if (size == 0) { return EMPTY_BUFFER; } if (src instanceof CompositeChannelBuffer && ((CompositeChannelBuffer) src).useGathering()) { return new GatheringSendBuffer(src.toByteBuffers()); } if (src.isDirect()) { return new UnpooledSendBuffer(src.toByteBuffer()); } if (src.readableBytes() > DEFAULT_PREALLOCATION_SIZE) { return new UnpooledSendBuffer(src.toByteBuffer()); } Preallocation current = this.current; ByteBuffer buffer = current.buffer; int remaining = buffer.remaining(); PooledSendBuffer dst; if (size < remaining) { int nextPos = buffer.position() + size; ByteBuffer slice = buffer.duplicate(); buffer.position(align(nextPos)); slice.limit(nextPos); current.refCnt++; dst = new PooledSendBuffer(current, slice); } else if (size > remaining) { this.current = current = getPreallocation(); buffer = current.buffer; ByteBuffer slice = buffer.duplicate(); buffer.position(align(size)); slice.limit(size); current.refCnt++; dst = new PooledSendBuffer(current, slice); } else { // size == remaining current.refCnt++; this.current = getPreallocation0(); dst = new PooledSendBuffer(current, current.buffer); } ByteBuffer dstbuf = dst.buffer; dstbuf.mark(); src.getBytes(src.readerIndex(), dstbuf); dstbuf.reset(); return dst; }
/** Detects the frame type. This may only work on data originating from a Flash source. */ public void detectFrameType() { if (data != null && data.limit() > 0) { data.mark(); int firstByte = (data.get(0)) & 0xff; data.reset(); int frameType = (firstByte & IoConstants.MASK_VIDEO_FRAMETYPE) >> 4; if (frameType == IoConstants.FLAG_FRAMETYPE_KEYFRAME) { this.frameType = FrameType.KEYFRAME; } else if (frameType == IoConstants.FLAG_FRAMETYPE_INTERFRAME) { this.frameType = FrameType.INTERFRAME; } else if (frameType == IoConstants.FLAG_FRAMETYPE_DISPOSABLE) { this.frameType = FrameType.DISPOSABLE_INTERFRAME; } } }
@Override public ByteBuffer createBinaryFrame(Framedata framedata) { if (framedata.getOpcode() != Opcode.TEXT) { throw new RuntimeException("only text frames supported"); } ByteBuffer pay = framedata.getPayloadData(); ByteBuffer b = ByteBuffer.allocate(pay.remaining() + 2); b.put(START_OF_FRAME); pay.mark(); b.put(pay); pay.reset(); b.put(END_OF_FRAME); b.flip(); return b; }
private HandshakeState isFlashEdgeCase(ByteBuffer request) throws IncompleteHandshakeException { request.mark(); if (request.limit() > Draft.FLASH_POLICY_REQUEST.length) { return HandshakeState.NOT_MATCHED; } else if (request.limit() < Draft.FLASH_POLICY_REQUEST.length) { throw new IncompleteHandshakeException(Draft.FLASH_POLICY_REQUEST.length); } else { for (int flash_policy_index = 0; request.hasRemaining(); flash_policy_index++) { if (Draft.FLASH_POLICY_REQUEST[flash_policy_index] != request.get()) { request.reset(); return HandshakeState.NOT_MATCHED; } } return HandshakeState.MATCHED; } }
/** * Gets the content of the ByteBuffer as a byte[] without mutating the buffer (so thread safe) and * minimizing GC (i.e. object creation) */ public static byte[] asByteArray(ByteBuffer buf) { byte[] result = null; if (buf.hasArray() && buf.arrayOffset() == 0 && buf.capacity() == buf.remaining()) { result = buf.array(); } else { result = new byte[buf.remaining()]; if (buf.hasArray()) { System.arraycopy(buf.array(), buf.arrayOffset() + buf.position(), result, 0, result.length); } else { // Direct buffer ByteBuffer duplicate = buf.duplicate(); duplicate.mark(); duplicate.get(result); duplicate.reset(); } } return result; }
public static Packet read(@NotNull ByteBuffer buf) throws ParseException { if (buf == null) { throw new IllegalArgumentException("buf must be not null"); } try { PacketHeader header = PacketHeader.read(buf); if (buf.remaining() != header.size - PacketHeader.HEADER_LENGTH) { throw new ParseException( "Packet header size = %d, but buf remaining = %d", header.size, buf.remaining()); } if (header.isHello()) { buf.mark(); int connectionFlag = buf.get() & 0xFF; buf.position(buf.position() + 2); // skip 2 bytes int connectionUptime = buf.get() & 0xFF; buf.reset(); String payload = Utils.readHexString(buf); return new PacketHello(header, connectionFlag, connectionUptime, payload); } ArrayList<Command> commands = new ArrayList<>(); while (buf.hasRemaining()) { commands.add(Command.read(buf)); } return new Packet(header, commands); } catch (ParseException ex) { throw ex; } catch (Throwable ex) { buf.rewind(); throw new ParseException( "Can't parse packet %s cause ", Utils.readHexString(buf), ex.getMessage()); } }
public void reset(ByteBuffer buf) { try { buf.reset(); } catch (InvalidMarkException e) { buf.mark(); } mBase = buf.array(); mBaseOffset = buf.arrayOffset(); mBuf = buf; mBuf.order(ByteOrder.LITTLE_ENDIAN); mEob = false; mRow = null; mColumnFamily = null; mColumnQualifier = null; mValue = null; mCellFlag = 0; int version = mBuf.getInt(); if (version != SerializedCellsFlag.VERSION) throw new AssertionError( "SerializedCells version mismatch, expected " + SerializedCellsFlag.VERSION + ", got " + version); }
@Override public void reset() throws IOException { byteBuffer.reset(); }
@Override public synchronized void reset() throws IOException { buffer.reset(); syncPos(); }