List of usage examples for java.nio ByteBuffer position
public final int position()
From source file:com.l2jfree.network.mmocore.ReadWriteThread.java
private void readPacket(SelectionKey key) { @SuppressWarnings("unchecked") T con = (T) key.attachment();//w ww . j ava 2 s . c o m ByteBuffer buf = con.getReadBuffer(); if (buf == null) { buf = getReadBuffer(); buf.clear(); } int readPackets = 0; int readBytes = 0; for (;;) { final int remainingFreeSpace = buf.remaining(); int result = -2; try { result = con.getReadableByteChannel().read(buf); } catch (IOException e) { //error handling goes bellow } switch (result) { case -2: // IOException { closeConnectionImpl(con, true); return; } case -1: // EOS { closeConnectionImpl(con, false); return; } default: { buf.flip(); // try to read as many packets as possible for (;;) { final int startPos = buf.position(); if (readPackets >= getMaxIncomingPacketsPerPass() || readBytes >= getMaxIncomingBytesPerPass()) break; if (!tryReadPacket2(con, buf)) break; readPackets++; readBytes += (buf.position() - startPos); } break; } } // stop reading, if we have reached a config limit if (readPackets >= getMaxIncomingPacketsPerPass() || readBytes >= getMaxIncomingBytesPerPass()) break; // if the buffer wasn't filled completely, we should stop trying as the input channel is empty if (remainingFreeSpace > result) break; // compact the buffer for reusing the remaining bytes if (buf.hasRemaining()) buf.compact(); else buf.clear(); } // check if there are some more bytes in buffer and allocate/compact to prevent content lose. if (buf.hasRemaining()) { if (buf == getReadBuffer()) { con.setReadBuffer(getPooledBuffer().put(getReadBuffer())); } else { buf.compact(); } } else { if (buf == getReadBuffer()) { // no additional buffers used } else { con.setReadBuffer(null); recycleBuffer(buf); } } }
From source file:com.arpnetworking.tsdcore.sinks.KairosDbSink.java
/** * {@inheritDoc}/* w ww . j ava 2s . c o m*/ */ @Override protected Collection<byte[]> serialize(final PeriodicData periodicData) { // Initialize serialization structures final List<byte[]> completeChunks = Lists.newArrayList(); final ByteBuffer currentChunk = ByteBuffer.allocate(_maxRequestSize); final ByteArrayOutputStream chunkStream = new ByteArrayOutputStream(); // Extract and transform shared data final long timestamp = periodicData.getStart().plus(periodicData.getPeriod()).getMillis(); final String serializedPeriod = periodicData.getPeriod().toString(ISOPeriodFormat.standard()); final ImmutableMap<String, String> dimensions = periodicData.getDimensions(); final Serializer serializer = new Serializer(timestamp, serializedPeriod, dimensions); // Initialize the chunk buffer currentChunk.put(HEADER); // Add aggregated data for (final AggregatedData datum : periodicData.getData()) { if (!datum.isSpecified()) { LOGGER.trace().setMessage("Skipping unspecified datum").addData("datum", datum).log(); continue; } serializer.serializeDatum(completeChunks, currentChunk, chunkStream, datum); } // Add conditions for (final Condition condition : periodicData.getConditions()) { serializer.serializeCondition(completeChunks, currentChunk, chunkStream, condition); } // Add the current chunk (if any) to the completed chunks if (currentChunk.position() > HEADER_BYTE_LENGTH) { currentChunk.put(currentChunk.position() - 1, FOOTER); completeChunks.add(Arrays.copyOf(currentChunk.array(), currentChunk.position())); } return completeChunks; }
From source file:net.beaconpe.jraklib.protocol.AcknowledgePacket.java
@Override protected void _encode() { ByteBuffer payload = ByteBuffer.allocate(1024); int count = packets.length; int records = 0; if (count > 0) { int pointer = 0; int start = packets[0]; int last = packets[0]; while (pointer + 1 < count) { int current = packets[pointer++]; int diff = current - last; if (diff == 1) { last = current;/*from w w w . j a va2s.c om*/ } else if (diff > 1) { //Forget about duplicated packets (bad queues?) if (start == last) { payload.put((byte) 0x01); payload.put(Binary.writeLTriad(start)); start = last = current; } else { payload.put((byte) 0x00); payload.put(Binary.writeLTriad(start)); payload.put(Binary.writeLTriad(last)); start = last = current; } records = records + 1; } } if (start == last) { payload.put((byte) 0x01); payload.put(Binary.writeLTriad(start)); } else { payload.put((byte) 0x00); payload.put(Binary.writeLTriad(start)); payload.put(Binary.writeLTriad(last)); } records = records + 1; } putShort((short) records); put(ArrayUtils.subarray(payload.array(), 0, payload.position())); }
From source file:de.rwhq.btree.LeafNode.java
@Override public int remove(final K key, final V value) { final int offset = offsetOfKey(key); if (offset == NOT_FOUND) return 0; final int numberOfValues = get(key).size(); final ByteBuffer buffer = rawPage().bufferForWriting(offset); final byte[] buf1 = new byte[keySerializer.getSerializedLength()]; final byte[] buf2 = new byte[valueSerializer.getSerializedLength()]; int removed = 0; for (int i = 0; i < numberOfValues; i++) { buffer.get(buf1);//from www. ja va 2 s . co m buffer.get(buf2); // load only the value final V val = valueSerializer.deserialize(buf2); if (val == null) throw new IllegalStateException("value retrieved from a value page should not be null"); // we cant use a comparator here since we have none for values (its the only case we need it) if (val.equals(value)) { // also free key page // move pointers forward and reset buffer final int startingPos = buffer.position() - buf1.length - buf2.length; System.arraycopy(buffer.array(), buffer.position(), buffer.array(), startingPos, buffer.capacity() - buffer.position()); buffer.position(startingPos); removed++; } } setNumberOfEntries(getNumberOfEntries() - removed); return removed; }
From source file:co.elastic.tealess.SSLChecker.java
private void checkHandshake(SSLReport sslReport, SocketChannel socket) { final InetSocketAddress address = sslReport.getAddress(); final String name = sslReport.getHostname(); IOObserver ioObserver = new IOObserver(); ObservingSSLEngine sslEngine = new ObservingSSLEngine(ctx.createSSLEngine(name, address.getPort()), ioObserver);/* ww w . j a v a 2s . c om*/ sslReport.setIOObserver(ioObserver); sslEngine.setUseClientMode(true); try { sslEngine.beginHandshake(); } catch (SSLException e) { sslReport.setFailed(e); Throwable cause = Blame.get(e); logger.warn("beginHandshake failed: [{}] {}", cause.getClass(), cause.getMessage()); } // TODO: Is this enough bytes? int size = sslEngine.getSession().getApplicationBufferSize() * 2; ByteBuffer localText = ByteBuffer.allocate(size); ByteBuffer localWire = ByteBuffer.allocate(size); ByteBuffer peerText = ByteBuffer.allocate(size); ByteBuffer peerWire = ByteBuffer.allocate(size); // TODO: I wonder... do we need to send any data at all? localText.put("SSL TEST. HELLO.".getBytes()); localText.flip(); SSLEngineResult result; logger.info("Starting SSL handshake [{}] ", address); try { SSLEngineResult.HandshakeStatus state; state = sslEngine.getHandshakeStatus(); while (state != FINISHED) { // XXX: Use a Selector to wait for data. //logger.trace("State: {} [{}]", state, address); switch (state) { case NEED_TASK: sslEngine.getDelegatedTask().run(); state = sslEngine.getHandshakeStatus(); break; case NEED_WRAP: localWire.clear(); result = sslEngine.wrap(localText, localWire); state = result.getHandshakeStatus(); localWire.flip(); while (localWire.hasRemaining()) { socket.write(localWire); //logger.trace("Sent {} bytes [{}]", bytes, address); } localWire.compact(); break; case NEED_UNWRAP: // Try reading until we get data. Selector selector = Selector.open(); while (peerWire.position() == 0) { socket.read(peerWire); try { Thread.currentThread().sleep(5); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("Read " + peerWire.position() + " bytes"); peerWire.flip(); result = sslEngine.unwrap(peerWire, peerText); state = result.getHandshakeStatus(); peerWire.compact(); break; } } } catch (IOException e) { sslReport.setFailed(e); sslReport.setSSLSession(sslEngine.getHandshakeSession()); sslReport.setPeerCertificateDetails(peerCertificateDetails); logger.warn("beginHandshake failed", e); return; } logger.info("handshake completed [{}]", address); // Handshake OK! sslReport.setSSLSession(sslEngine.getSession()); }
From source file:edu.hawaii.soest.kilonalu.adcp.ADCPSource.java
/** * A method that executes the streaming of data from the source to the RBNB * server after all configuration of settings, connections to hosts, and * thread initiatizing occurs. This method contains the detailed code for * streaming the data and interpreting the stream. *//*from ww w . ja v a2 s . com*/ protected boolean execute() { // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; SocketChannel socket = getSocketConnection(); // while data are being sent, read them into the buffer try { // create four byte placeholders used to evaluate up to a four-byte // window. The FIFO layout looks like: // ------------------------- // in ---> | One | Two |Three|Four | ---> out // ------------------------- byte byteOne = 0x00, // set initial placeholder values byteTwo = 0x00, byteThree = 0x00, byteFour = 0x00; // Create a buffer that will store the ensemble bytes as they are read ByteBuffer ensembleBuffer = ByteBuffer.allocate(getBufferSize()); // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); // add a channel of data that will be pushed to the server. // Each ensemble will be sent to the Data Turbine as an rbnb frame. ChannelMap rbnbChannelMap = new ChannelMap(); int channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); // while there are bytes to read from the socket ... while (socket.read(buffer) != -1 || buffer.position() > 0) { // prepare the buffer for reading buffer.flip(); // while there are unread bytes in the ByteBuffer while (buffer.hasRemaining()) { byteOne = buffer.get(); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire ensemble, first by // inserting a timestamp into the channelMap. This time is merely // the time of insert into the data turbine, not the time of // observations of the measurements. That time should be parsed out // of the ensemble in the Sink client code System.out.print("\rProcessed byte # " + ensembleByteCount + " " + new String(Hex.encodeHex((new byte[] { byteOne }))) + " - log msg is: "); switch (state) { case 0: // find ensemble header id if (byteOne == 0x7F && byteTwo == 0x7F) { ensembleByteCount++; // add Header ID ensembleChecksum += (byteTwo & 0xFF); ensembleByteCount++; // add Data Source ID ensembleChecksum += (byteOne & 0xFF); state = 1; break; } else { break; } case 1: // find the Ensemble Length (LSB) ensembleByteCount++; // add Ensemble Byte Count (LSB) ensembleChecksum += (byteOne & 0xFF); state = 2; break; case 2: // find the Ensemble Length (MSB) ensembleByteCount++; // add Ensemble Byte Count (MSB) ensembleChecksum += (byteOne & 0xFF); int upperEnsembleByte = (byteOne & 0xFF) << 8; int lowerEnsembleByte = (byteTwo & 0xFF); ensembleBytes = upperEnsembleByte + lowerEnsembleByte; logger.debug("Number of Bytes in the Ensemble: " + ensembleBytes); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteFour); ensembleBuffer.put(byteThree); ensembleBuffer.put(byteTwo); ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteFour); ensembleBuffer.put(byteThree); ensembleBuffer.put(byteTwo); ensembleBuffer.put(byteOne); } state = 3; break; // verify that the header is real, not a random 0x7F7F case 3: // find the number of data types in the ensemble // set the numberOfDataTypes byte if (ensembleByteCount == NUMBER_OF_DATA_TYPES_OFFSET - 1) { ensembleByteCount++; ensembleChecksum += (byteOne & 0xFF); numberOfDataTypes = (byteOne & 0xFF); // calculate the number of bytes to the Fixed Leader ID dataTypeOneOffset = 6 + (2 * numberOfDataTypes); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } state = 4; break; } else { ensembleByteCount++; ensembleChecksum += (byteOne & 0xFF); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } break; } case 4: // find the offset to data type #1 and verify the header ID if ((ensembleByteCount == dataTypeOneOffset + 1) && byteOne == 0x00 && byteTwo == 0x00) { ensembleByteCount++; ensembleChecksum += (byteOne & 0xFF); // we are confident that the previous sequence of 0x7F7F is truly // an headerID and not a random occurrence in the stream because // we have identified the Fixed Leader ID (0x0000) the correct // number of bytes beyond the 0x7F7F headerIsVerified = true; if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } state = 5; break; } else { if (ensembleByteCount > dataTypeOneOffset + 1) { // We've hit a random 0x7F7F byte sequence that is not a true // ensemble header id. Reset the processing and look for the // next 0x7F7F sequence in the stream ensembleByteCount = 0; ensembleChecksum = 0; dataTypeOneOffset = 0; numberOfDataTypes = 0; headerIsVerified = false; ensembleBuffer.clear(); rbnbChannelMap.Clear(); channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; state = 0; if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } break; } else { // We are still parsing bytes between the purported header ID // and fixed leader ID. Keep parsing until we hit the fixed // leader ID, or until we are greater than the dataTypeOneOffset // stated value. ensembleByteCount++; ensembleChecksum += (byteOne & 0xFF); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } break; } } case 5: // read the rest of the bytes to the next Header ID // if we've made it to the next ensemble's header id, prepare to // flush the data. Also check that the calculated byte count // is greater than the recorded byte count in case of finding an // arbitrary 0x7f 0x7f sequence in the data stream if (byteOne == 0x7F && byteTwo == 0x7F && (ensembleByteCount == ensembleBytes + 3) && headerIsVerified) { // remove the last bytes from the count (byteOne and byteTwo) ensembleByteCount -= 1; // remove the last three bytes from the checksum: // the two checksum bytes are not included, and the two 0x7f //bytes belong to the next ensemble, and one of them was // previously added. Reset the buffer position due to this too. //ensembleChecksum -= (byteOne & 0xFF); ensembleChecksum -= (byteTwo & 0xFF); ensembleChecksum -= (byteThree & 0xFF); ensembleChecksum -= (byteFour & 0xFF); // We are consistently 1 byte over in the checksum. Trim it. We need to // troubleshoot why this is. CSJ 12/18/2007 ensembleChecksum = ensembleChecksum - 1; // jockey byteThree into LSB, byteFour into MSB int upperChecksumByte = (byteThree & 0xFF) << 8; int lowerChecksumByte = (byteFour & 0xFF); int trueChecksum = upperChecksumByte + lowerChecksumByte; if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put((byte) lowerChecksumByte); ensembleBuffer.put((byte) (upperChecksumByte >> 8)); } else { ensembleBuffer.compact(); ensembleBuffer.put((byte) lowerChecksumByte); ensembleBuffer.put((byte) (upperChecksumByte >> 8)); } // check if the calculated checksum (modulo 65535) is equal // to the true checksum; if so, flush to the data turbine // Also, if the checksums are off by 1 byte, also flush the // data. We need to troubleshoot this bug CSJ 06/11/2008 if (((ensembleChecksum % 65535) == trueChecksum) || ((ensembleChecksum + 1) % 65535 == trueChecksum) || ((ensembleChecksum - 1) % 65535 == trueChecksum)) { // extract just the length of the ensemble bytes out of the // ensemble buffer, and place it in the channel map as a // byte array. Then, send it to the data turbine. byte[] ensembleArray = new byte[ensembleByteCount]; ensembleBuffer.flip(); ensembleBuffer.get(ensembleArray); // send the ensemble to the data turbine rbnbChannelMap.PutTimeAuto("server"); rbnbChannelMap.PutDataAsByteArray(channelIndex, ensembleArray); getSource().Flush(rbnbChannelMap); logger.debug("flushed: " + ensembleByteCount + " " + "ens cksum: " + ensembleChecksum + "\t\t" + "ens pos: " + ensembleBuffer.position() + "\t" + "ens rem: " + ensembleBuffer.remaining() + "\t" + "buf pos: " + buffer.position() + "\t" + "buf rem: " + buffer.remaining() + "\t" + "state: " + state); logger.info("Sent ADCP ensemble to the data turbine."); // only clear all four bytes if we are not one or two bytes // from the end of the byte buffer (i.e. the header id // is split or is all in the previous buffer) if (byteOne == 0x7f && byteTwo == 0x7f && ensembleByteCount > ensembleBytes && buffer.position() == 0) { byteThree = 0x00; byteFour = 0x00; logger.debug("Cleared ONLY b3, b4."); } else if (byteOne == 0x7f && ensembleByteCount > ensembleBytes && buffer.position() == 1) { buffer.position(buffer.position() - 1); byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; logger.debug("Cleared ONLY b2, b3, b4."); } else { byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; logger.debug("Cleared ALL b1, b2, b3, b4."); } //rewind the position to before the next ensemble's header id if (buffer.position() >= 2) { buffer.position(buffer.position() - 2); logger.debug("Moved position back two, now: " + buffer.position()); } ensembleBuffer.clear(); ensembleByteCount = 0; ensembleBytes = 0; ensembleChecksum = 0; state = 0; break; } else { // The checksums don't match, move on logger.info("not equal: " + "calc chksum: " + (ensembleChecksum % 65535) + "\tens chksum: " + trueChecksum + "\tbuf pos: " + buffer.position() + "\tbuf rem: " + buffer.remaining() + "\tens pos: " + ensembleBuffer.position() + "\tens rem: " + ensembleBuffer.remaining() + "\tstate: " + state); rbnbChannelMap.Clear(); channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); ensembleBuffer.clear(); ensembleByteCount = 0; ensembleChecksum = 0; ensembleBuffer.clear(); state = 0; break; } } else { // still in the middle of the ensemble, keep adding bytes ensembleByteCount++; // add each byte found ensembleChecksum += (byteOne & 0xFF); if (ensembleBuffer.remaining() > 0) { ensembleBuffer.put(byteOne); } else { ensembleBuffer.compact(); ensembleBuffer.put(byteOne); } break; } } // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = byteOne; logger.debug("remaining:\t" + buffer.remaining() + "\tstate:\t" + state + "\tens byte count:\t" + ensembleByteCount + "\tens bytes:\t" + ensembleBytes + "\tver:\t" + headerIsVerified + "\tbyte value:\t" + new String(Hex.encodeHex((new byte[] { byteOne })))); } //end while (more unread bytes) // prepare the buffer to read in more bytes from the stream buffer.compact(); } // end while (more socket bytes to read) socket.close(); } catch (IOException e) { // handle exceptions // In the event of an i/o exception, log the exception, and allow execute() // to return false, which will prompt a retry. failed = true; e.printStackTrace(); return !failed; } catch (SAPIException sapie) { // In the event of an RBNB communication exception, log the exception, // and allow execute() to return false, which will prompt a retry. failed = true; sapie.printStackTrace(); return !failed; } return !failed; }
From source file:de.hpi.fgis.hdrs.Triple.java
public void readFields(ByteBuffer header, ByteBuffer data) { // read header Slen = header.getShort();/*from ww w . j a v a2 s. c o m*/ Plen = header.getShort(); Olen = header.getInt(); multiplicity = header.getInt(); // read data this.buffer = data.array(); int size = (int) Slen + (int) Plen + Olen; offset = data.arrayOffset() + data.position(); data.position(data.position() + size); }
From source file:de.hpi.fgis.hdrs.Triple.java
public void readFields(ByteBuffer buffer) { // read header Slen = buffer.getShort();/*from w w w . j a v a 2 s .com*/ Plen = buffer.getShort(); Olen = buffer.getInt(); multiplicity = buffer.getInt(); // read data this.buffer = buffer.array(); int size = (int) Slen + (int) Plen + Olen; offset = buffer.arrayOffset() + buffer.position(); buffer.position(buffer.position() + size); }
From source file:edu.mbl.jif.imaging.mmtiff.MultipageTiffWriter.java
private void writeMMHeaderAndSummaryMD(JSONObject summaryMD) throws IOException { if (summaryMD.has("Comment")) { summaryMD.remove("Comment"); }/*from ww w . ja va 2 s . c om*/ String summaryMDString = summaryMD.toString(); int mdLength = summaryMDString.length(); ByteBuffer buffer = ByteBuffer.allocate(40).order(BYTE_ORDER); if (BYTE_ORDER.equals(ByteOrder.BIG_ENDIAN)) { buffer.asCharBuffer().put(0, (char) 0x4d4d); } else { buffer.asCharBuffer().put(0, (char) 0x4949); } buffer.asCharBuffer().put(1, (char) 42); buffer.putInt(4, 40 + mdLength); //8 bytes for file header + //8 bytes for index map offset header and offset + //8 bytes for display settings offset header and display settings offset //8 bytes for comments offset header and comments offset //8 bytes for summaryMD header summary md length + //1 byte for each character of summary md buffer.putInt(32, SUMMARY_MD_HEADER); buffer.putInt(36, mdLength); ByteBuffer[] buffers = new ByteBuffer[2]; buffers[0] = buffer; buffers[1] = ByteBuffer.wrap(getBytesFromString(summaryMDString)); fileChannel_.write(buffers); filePosition_ += buffer.position() + mdLength; }
From source file:edu.tsinghua.lumaqq.qq.packets.out._05.TransferPacket.java
@Override protected void putBody(ByteBuffer buf) { if (!requestSend) { // 2. 8/*from w w w.j a va 2s . c o m*/ buf.putLong(0x0100000000000000L); // 3. session id, 4 buf.putInt(sessionId); // 4 buf.putInt(0); // ?? if (dataReply) { buf.putChar((char) 0x0001); buf.put((byte) 0x02); } else { buf.putChar((char) 0x04); buf.putInt(0); } } else if (data) { // 2. 8??0x1000000000000001? if (last) buf.putLong(0x0100000000000000L); else buf.putLong(0x0100000000000001L); // 3. session id, 4 buf.putInt(sessionId); // 4. 4 buf.putInt(0); // 5. ?2 buf.putChar((char) fragment.length); // 6. ? buf.put(fragment); } else { // 2. 8 buf.putLong(0x0100000000000000L); // 3. session id, 4 buf.putInt(sessionId); // 4. 4 buf.putInt(0); // 5. ???2 buf.putChar((char) 0); // 6. 25? int pos = buf.position(); buf.putChar((char) 0); // 7. md5 buf.put(md5); // 8. ??md5 byte[] fileNameBytes = fileName.getBytes(); buf.put(md5(fileNameBytes)); // 9. 4 buf.putInt(imageLength); // 10. ??2 buf.putChar((char) fileName.length()); // 11. ?? buf.put(fileNameBytes); // 12. 8 buf.putLong(0); char len = (char) (buf.position() - pos); buf.putChar(pos - 2, len); buf.putChar(pos, len); } }