@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(); }
@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; }
@Test public void testAliasTo() throws Exception { final ByteBuffer key = ByteBuffer.wrap("abc".getBytes()); key.mark(); final ByteBuffer alias = ByteBuffer.wrap("def".getBytes()); alias.mark(); ((SignatureVerifierWithIdentityStore) signatureVerifier).aliasTo(key, alias); final HashIdentity retrieved = identityDatastore.get(key); assertNotNull(retrieved); assertEquals(key, retrieved.getHash()); assertTrue(retrieved.getHashIdentity() instanceof ByteBuffer); assertEquals(alias, retrieved.getHashIdentity()); }
/** * 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); } }
/** * Constructs a Row from a buffer that can be read via a SerializedCellsReader * * @param buf byte array that can be read via SerializedCellsReader */ public Row(byte[] buf) { mSerializedCells = ByteBuffer.wrap(buf); mSerializedCells.rewind(); mSerializedCells.mark(); mReader.reset(mSerializedCells); if (mReader.next()) mRowKey = mReader.get_row(); }
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 public void testExists() throws Exception { final HashIdentity identity = new HashIdentity(); final ByteBuffer key = ByteBuffer.wrap("abc".getBytes()); key.mark(); identity.setHash(key); final HashIdentityValue value = new HashIdentityValue(); value.setId((long) 0); value.setConfirmers(new ArrayList<>()); identity.setHashIdentity(value); identityDatastore.put(key, identity); assertTrue(((SignatureVerifierWithIdentityStore) signatureVerifier).exists(key)); final ByteBuffer keyFalse = ByteBuffer.wrap("def".getBytes()); keyFalse.mark(); assertFalse(((SignatureVerifierWithIdentityStore) signatureVerifier).exists(keyFalse)); }
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 } } }
private void realloc(int size) { ByteBuffer buffer = ByteBuffer.allocate(size); this.buffer.flip(); buffer.put(this.buffer); buffer.mark(); this.buffer = buffer; }
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; }
/** * 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); }
/** 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()); }
@Override public synchronized void mark(int readlimit) { buffer.mark(); int pos = buffer.position(); int max = buffer.capacity() - pos; int newLimit = Math.min(max, pos + readlimit); buffer.limit(newLimit); }
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; }
@Test public void testPut() throws Exception { final ByteBuffer key = ByteBuffer.wrap("abc".getBytes()); key.mark(); final HashIdentityValue identity = new HashIdentityValue(); identity.setId((long) 0); identity.setConfirmers(new ArrayList<>()); ((SignatureVerifierWithIdentityStore) signatureVerifier).put(key, identity); assertEquals(identity, identityDatastore.get(key).getHashIdentity()); }
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; } }
Base1() throws GoraException, NoSuchAlgorithmException { final ByteBuffer b0 = ByteBuffer.wrap("0".getBytes()); b0.mark(); e0 = new com.kareebo.contacts.thrift.EncryptionKey( b0, com.kareebo.contacts.thrift.EncryptionAlgorithm.RSA2048); final User user0 = userDatastore.get(clientId.getUser()); final Map<CharSequence, Client> clients0 = user0.getClients(); clients0.values().iterator().next().getKeys().setEncryption(TypeConverter.convert(e0)); final ByteBuffer b1 = ByteBuffer.wrap("1".getBytes()); b1.mark(); e1 = new com.kareebo.contacts.thrift.EncryptionKey( b1, com.kareebo.contacts.thrift.EncryptionAlgorithm.RSA2048); final Client client01 = createClient(clientId01, verificationKey); client01.getKeys().setEncryption(TypeConverter.convert(e1)); clients0.put(TypeConverter.convert(clientId01.getClient()), client01); user0.setClients(clients0); final Client client10 = createClient(clientId10, verificationKey); final Client client11 = createClient(clientId11, verificationKey); final User user1 = new User(); user1.setBlind(b0); final Map<CharSequence, Client> clients1 = new HashMap<>(2); clients1.put(TypeConverter.convert(clientId10.getClient()), client10); clients1.put(TypeConverter.convert(clientId11.getClient()), client11); user1.setClients(clients1); user1.setIdentities(new ArrayList<>()); user1.setSentRequests(new ArrayList<>()); user1.setId(clientId10.getUser()); userDatastore.put(clientId.getUser(), user0); userDatastore.put(clientId10.getUser(), user1); final HashIdentity hashIdentity = new HashIdentity(); hashIdentity.setHash(b0); final HashIdentityValue hashIdentityValue = new HashIdentityValue(); hashIdentityValue.setConfirmers(new ArrayList<>()); hashIdentityValue.setId(clientId10.getUser()); hashIdentity.setHashIdentity(hashIdentityValue); u.setAlgorithm(HashAlgorithm.SHA256); u.setBuffer(b0); identityDataStore.put(b0, hashIdentity); }
/** 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 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(); }
@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; } }
/** * 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; }