List of usage examples for java.nio ByteBuffer hasRemaining
public final boolean hasRemaining()
From source file:org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.java
/** * Execute the specified listener, committing or rolling back the transaction afterwards (if necessary). * * @param channel the Rabbit Channel to operate on * @param messageIn the received Rabbit Message * @throws Exception Any Exception./*w w w. j a v a2 s .c o m*/ * * @see #invokeListener * @see #handleListenerException */ protected void executeListener(Channel channel, Message messageIn) throws Exception { if (!isRunning()) { if (logger.isWarnEnabled()) { logger.warn( "Rejecting received message because the listener container has been stopped: " + messageIn); } throw new MessageRejectedWhileStoppingException(); } try { Message message = messageIn; if (this.afterReceivePostProcessors != null) { for (MessagePostProcessor processor : this.afterReceivePostProcessors) { message = processor.postProcessMessage(message); } } Object batchFormat = message.getMessageProperties().getHeaders() .get(MessageProperties.SPRING_BATCH_FORMAT); if (MessageProperties.BATCH_FORMAT_LENGTH_HEADER4.equals(batchFormat) && this.deBatchingEnabled) { ByteBuffer byteBuffer = ByteBuffer.wrap(message.getBody()); MessageProperties messageProperties = message.getMessageProperties(); messageProperties.getHeaders().remove(MessageProperties.SPRING_BATCH_FORMAT); while (byteBuffer.hasRemaining()) { int length = byteBuffer.getInt(); if (length < 0 || length > byteBuffer.remaining()) { throw new ListenerExecutionFailedException("Bad batched message received", new MessageConversionException( "Insufficient batch data at offset " + byteBuffer.position()), message); } byte[] body = new byte[length]; byteBuffer.get(body); messageProperties.setContentLength(length); // Caveat - shared MessageProperties. Message fragment = new Message(body, messageProperties); invokeListener(channel, fragment); } } else { invokeListener(channel, message); } } catch (Exception ex) { if (messageIn.getMessageProperties().isFinalRetryForMessageWithNoId()) { if (this.statefulRetryFatalWithNullMessageId) { throw new FatalListenerExecutionException( "Illegal null id in message. Failed to manage retry for message: " + messageIn); } else { throw new ListenerExecutionFailedException("Cannot retry message more than once without an ID", new AmqpRejectAndDontRequeueException("Not retryable; rejecting and not requeuing", ex), messageIn); } } handleListenerException(ex); throw ex; } }
From source file:org.commoncrawl.io.internal.NIOHttpConnection.java
private boolean accumulateHeaders(int headersMax) throws IOException { if (_incomingAccumulationBuffer == null) { _incomingAccumulationBuffer = new ByteArrayOutputStream(HTTP_HEADER_SIZE_MAX); }/*w w w.j ava 2 s.c om*/ ByteBuffer currentBuffer = null; boolean eolFound = false; while (!eolFound && (currentBuffer = _inBuf.read()) != null) { while (!eolFound && currentBuffer.hasRemaining()) { byte c = currentBuffer.get(); _incomingAccumulationBuffer.write(c); if (c == '\n') { if (_lastCharWasLF) { eolFound = true; } else { _lastCharWasLF = true; } } else if (c != '\r' || _lastChar != '\n') { _lastCharWasLF = false; } _lastChar = c; if (eolFound) { if (currentBuffer.hasRemaining()) { // if trailing data in buffer , push it back for content phase _inBuf.putBack(currentBuffer); } return true; } else { if (headersMax != -1 && _incomingAccumulationBuffer.size() > headersMax) throw new IOException("Header Size Limit Reached With No Terminator!"); } } } return false; }
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 w w w. ja va 2 s.c o m*/ 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:org.opendaylight.lispflowmapping.lisp.serializer.MapRequestSerializer.java
public MapRequest deserialize(ByteBuffer requestBuffer) { try {/* w ww.j a v a 2s. co m*/ MapRequestBuilder builder = new MapRequestBuilder(); byte typeAndFlags = requestBuffer.get(); builder.setAuthoritative(ByteUtil.extractBit(typeAndFlags, Flags.AUTHORITATIVE)); builder.setMapDataPresent(ByteUtil.extractBit(typeAndFlags, Flags.MAP_DATA_PRESENT)); builder.setProbe(ByteUtil.extractBit(typeAndFlags, Flags.PROBE)); builder.setSmr(ByteUtil.extractBit(typeAndFlags, Flags.SMR)); byte moreFlags = requestBuffer.get(); builder.setPitr(ByteUtil.extractBit(moreFlags, Flags.PITR)); builder.setSmrInvoked(ByteUtil.extractBit(moreFlags, Flags.SMR_INVOKED)); int itrCount = ByteUtil.getUnsignedByte(requestBuffer) + 1; int recordCount = ByteUtil.getUnsignedByte(requestBuffer); builder.setNonce(requestBuffer.getLong()); LispAddressSerializerContext ctx = new LispAddressSerializerContext( LispAddressSerializerContext.MASK_LEN_MISSING); builder.setSourceEid(new SourceEidBuilder() .setEid(LispAddressSerializer.getInstance().deserializeEid(requestBuffer, ctx)).build()); if (builder.getItrRloc() == null) { builder.setItrRloc(new ArrayList<ItrRloc>()); } for (int i = 0; i < itrCount; i++) { builder.getItrRloc().add(new ItrRlocBuilder() .setRloc(LispAddressSerializer.getInstance().deserializeRloc(requestBuffer)).build()); } if (builder.getEidItem() == null) { builder.setEidItem(new ArrayList<EidItem>()); } for (int i = 0; i < recordCount; i++) { builder.getEidItem().add(new EidItemBuilder() .setEid(EidRecordSerializer.getInstance().deserialize(requestBuffer)).build()); } if (builder.isMapDataPresent() && requestBuffer.hasRemaining()) { try { builder.setMapReply( new org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.lisp.proto.rev151105.maprequest.MapReplyBuilder() .setMappingRecord( MappingRecordSerializer.getInstance().deserialize(requestBuffer)) .build()) .build(); } catch (RuntimeException re) { LOG.warn("Couldn't deserialize Map-Reply encapsulated in Map-Request", re); } } return builder.build(); } catch (RuntimeException re) { throw new LispSerializationException( "Couldn't deserialize Map-Request (len=" + requestBuffer.capacity() + ")", re); } }
From source file:org.opendaylight.lispflowmapping.implementation.serializer.MapRequestSerializer.java
public MapRequest deserialize(ByteBuffer requestBuffer) { try {//from w w w . ja v a 2 s . c om MapRequestBuilder builder = new MapRequestBuilder(); byte typeAndFlags = requestBuffer.get(); builder.setAuthoritative(ByteUtil.extractBit(typeAndFlags, Flags.AUTHORITATIVE)); builder.setMapDataPresent(ByteUtil.extractBit(typeAndFlags, Flags.MAP_DATA_PRESENT)); builder.setProbe(ByteUtil.extractBit(typeAndFlags, Flags.PROBE)); builder.setSmr(ByteUtil.extractBit(typeAndFlags, Flags.SMR)); byte moreFlags = requestBuffer.get(); builder.setPitr(ByteUtil.extractBit(moreFlags, Flags.PITR)); builder.setSmrInvoked(ByteUtil.extractBit(moreFlags, Flags.SMR_INVOKED)); int itrCount = ByteUtil.getUnsignedByte(requestBuffer) + 1; int recordCount = ByteUtil.getUnsignedByte(requestBuffer); builder.setNonce(requestBuffer.getLong()); builder.setSourceEid( new SourceEidBuilder() .setLispAddressContainer(LispAFIConvertor .toContainer(LispAddressSerializer.getInstance().deserialize(requestBuffer))) .build()); if (builder.getItrRloc() == null) { builder.setItrRloc(new ArrayList<ItrRloc>()); } for (int i = 0; i < itrCount; i++) { builder.getItrRloc() .add(new ItrRlocBuilder().setLispAddressContainer(LispAFIConvertor .toContainer(LispAddressSerializer.getInstance().deserialize(requestBuffer))) .build()); } if (builder.getEidRecord() == null) { builder.setEidRecord(new ArrayList<EidRecord>()); } for (int i = 0; i < recordCount; i++) { builder.getEidRecord().add(EidRecordSerializer.getInstance().deserialize(requestBuffer)); } if (builder.isMapDataPresent() && requestBuffer.hasRemaining()) { try { builder.setMapReply( new org.opendaylight.yang.gen.v1.urn.opendaylight.lfm.control.plane.rev150314.maprequest.MapReplyBuilder( new EidToLocatorRecordBuilder( EidToLocatorRecordSerializer.getInstance().deserialize(requestBuffer)) .build()).build()); } catch (RuntimeException re) { LOG.warn("couldn't deserialize map reply encapsulated in map request. {}", re.getMessage()); } } return builder.build(); } catch (RuntimeException re) { throw new LispSerializationException( "Couldn't deserialize Map-Request (len=" + requestBuffer.capacity() + ")", re); } }
From source file:com.l2jfree.network.mmocore.ReadWriteThread.java
private void readPacket(SelectionKey key) { @SuppressWarnings("unchecked") T con = (T) key.attachment();/* w w w . j a v a2s. c om*/ 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:edu.hawaii.soest.kilonalu.ctd.SBE37Source.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. *//* ww w .j a v a2s.c o m*/ protected boolean execute() { logger.debug("SBE37Source.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; // while data are being sent, read them into the buffer try { this.socketChannel = getSocketConnection(); // 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 sample bytes as they are read ByteBuffer sampleBuffer = ByteBuffer.allocate(getBufferSize()); // create a byte buffer to store bytes from the TCP stream ByteBuffer buffer = ByteBuffer.allocateDirect(getBufferSize()); // create a character string to store characters from the TCP stream StringBuilder responseString = new StringBuilder(); // add a channel of data that will be pushed to the server. // Each sample will be sent to the Data Turbine as an rbnb frame. ChannelMap rbnbChannelMap = new ChannelMap(); int channelIndex = rbnbChannelMap.Add(getRBNBChannelName()); // wake the instrument with an initial take sample command this.command = this.commandPrefix + getInstrumentID() + "TS" + this.commandSuffix; this.sentCommand = queryInstrument(this.command); // verify the instrument ID is correct while (getInstrumentID() == null) { // allow time for the instrument response streamingThread.sleep(2000); buffer.clear(); // send the command and update the sentCommand status this.sentCommand = queryInstrument(this.command); // read the response into the buffer. Note that the streamed bytes // are 8-bit, not 16-bit Unicode characters. Use the US-ASCII // encoding instead. while (this.socketChannel.read(buffer) != -1 || buffer.position() > 0) { buffer.flip(); while (buffer.hasRemaining()) { String nextCharacter = new String(new byte[] { buffer.get() }, "US-ASCII"); responseString.append(nextCharacter); } // look for the command line ending if (responseString.toString().indexOf("S>") > 0) { // parse the ID from the idCommand response int idStartIndex = responseString.indexOf("=") + 2; int idStopIndex = responseString.indexOf("=") + 4; String idString = responseString.substring(idStartIndex, idStopIndex); // test that the ID is a valid number and set the instrument ID if ((new Integer(idString)).intValue() > 0) { setInstrumentID(idString); buffer.clear(); logger.debug("Instrument ID is " + getInstrumentID() + "."); break; } else { logger.debug("Instrument ID \"" + idString + "\" was not set."); } } else { break; } buffer.compact(); if (getInstrumentID() != null) { break; } } } // instrumentID is set // allow time for the instrument response streamingThread.sleep(5000); this.command = this.commandPrefix + getInstrumentID() + this.takeSampleCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); // while there are bytes to read from the socket ... while (this.socketChannel.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(); //logger.debug("b1: " + new String(Hex.encodeHex((new byte[]{byteOne}))) + "\t" + // "b2: " + new String(Hex.encodeHex((new byte[]{byteTwo}))) + "\t" + // "b3: " + new String(Hex.encodeHex((new byte[]{byteThree}))) + "\t" + // "b4: " + new String(Hex.encodeHex((new byte[]{byteFour}))) + "\t" + // "sample pos: " + sampleBuffer.position() + "\t" + // "sample rem: " + sampleBuffer.remaining() + "\t" + // "sample cnt: " + sampleByteCount + "\t" + // "buffer pos: " + buffer.position() + "\t" + // "buffer rem: " + buffer.remaining() + "\t" + // "state: " + state //); // Use a State Machine to process the byte stream. // Start building an rbnb frame for the entire sample, 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 sample in the Sink client code switch (state) { case 0: // sample line is begun by S> // note bytes are in reverse order in the FIFO window if (byteOne == 0x3E && byteTwo == 0x53) { // we've found the beginning of a sample, move on state = 1; break; } else { break; } case 1: // read the rest of the bytes to the next EOL characters // sample line is terminated by S> // note bytes are in reverse order in the FIFO window if (byteOne == 0x3E && byteTwo == 0x53) { sampleByteCount++; // add the last byte found to the count // add the last byte found to the sample buffer if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); sampleBuffer.put(byteOne); } // extract just the length of the sample bytes (less 2 bytes // to exclude the 'S>' prompt characters) out of the // sample buffer, and place it in the channel map as a // byte array. Then, send it to the data turbine. byte[] sampleArray = new byte[sampleByteCount - 2]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); // send the sample to the data turbine rbnbChannelMap.PutTimeAuto("server"); String sampleString = new String(sampleArray, "US-ASCII"); rbnbChannelMap.PutMime(channelIndex, "text/plain"); rbnbChannelMap.PutDataAsString(channelIndex, sampleString); getSource().Flush(rbnbChannelMap); logger.info("Sample: " + sampleString); logger.info("flushed data to the DataTurbine. "); byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; sampleBuffer.clear(); sampleByteCount = 0; //rbnbChannelMap.Clear(); //logger.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); //state = 0; // Once the sample is flushed, take a new sample if (getInstrumentID() != null) { // allow time for the instrument response streamingThread.sleep(2000); this.command = this.commandPrefix + getInstrumentID() + this.takeSampleCommand + this.commandSuffix; this.sentCommand = queryInstrument(command); } } else { // not 0x0A0D // still in the middle of the sample, keep adding bytes sampleByteCount++; // add each byte found if (sampleBuffer.remaining() > 0) { sampleBuffer.put(byteOne); } else { sampleBuffer.compact(); logger.debug("Compacting sampleBuffer ..."); sampleBuffer.put(byteOne); } break; } // end if for 0x0A0D EOL } // end switch statement // shift the bytes in the FIFO window byteFour = byteThree; byteThree = byteTwo; byteTwo = 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) this.socketChannel.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; } catch (java.lang.InterruptedException ie) { ie.printStackTrace(); } return !failed; }
From source file:com.healthmarketscience.jackcess.Table.java
/** * Writes a new table defined by the given TableCreator to the database. * @usage _advanced_method_/*from www .j av a2 s.c om*/ */ protected static void writeTableDefinition(TableCreator creator) throws IOException { // first, create the usage map page createUsageMapDefinitionBuffer(creator); // next, determine how big the table def will be (in case it will be more // than one page) JetFormat format = creator.getFormat(); int idxDataLen = (creator.getIndexCount() * (format.SIZE_INDEX_DEFINITION + format.SIZE_INDEX_COLUMN_BLOCK)) + (creator.getLogicalIndexCount() * format.SIZE_INDEX_INFO_BLOCK); int totalTableDefSize = format.SIZE_TDEF_HEADER + (format.SIZE_COLUMN_DEF_BLOCK * creator.getColumns().size()) + idxDataLen + format.SIZE_TDEF_TRAILER; // total up the amount of space used by the column and index names (2 // bytes per char + 2 bytes for the length) for (Column col : creator.getColumns()) { int nameByteLen = (col.getName().length() * JetFormat.TEXT_FIELD_UNIT_SIZE); totalTableDefSize += nameByteLen + 2; } for (IndexBuilder idx : creator.getIndexes()) { int nameByteLen = (idx.getName().length() * JetFormat.TEXT_FIELD_UNIT_SIZE); totalTableDefSize += nameByteLen + 2; } // now, create the table definition PageChannel pageChannel = creator.getPageChannel(); ByteBuffer buffer = pageChannel.createBuffer(Math.max(totalTableDefSize, format.PAGE_SIZE)); writeTableDefinitionHeader(creator, buffer, totalTableDefSize); if (creator.hasIndexes()) { // index row counts IndexData.writeRowCountDefinitions(creator, buffer); } // column definitions Column.writeDefinitions(creator, buffer); if (creator.hasIndexes()) { // index and index data definitions IndexData.writeDefinitions(creator, buffer); Index.writeDefinitions(creator, buffer); } //End of tabledef buffer.put((byte) 0xff); buffer.put((byte) 0xff); // write table buffer to database if (totalTableDefSize <= format.PAGE_SIZE) { // easy case, fits on one page buffer.putShort(format.OFFSET_FREE_SPACE, (short) (buffer.remaining() - 8)); // overwrite page free space // Write the tdef page to disk. pageChannel.writePage(buffer, creator.getTdefPageNumber()); } else { // need to split across multiple pages ByteBuffer partialTdef = pageChannel.createPageBuffer(); buffer.rewind(); int nextTdefPageNumber = PageChannel.INVALID_PAGE_NUMBER; while (buffer.hasRemaining()) { // reset for next write partialTdef.clear(); if (nextTdefPageNumber == PageChannel.INVALID_PAGE_NUMBER) { // this is the first page. note, the first page already has the // page header, so no need to write it here nextTdefPageNumber = creator.getTdefPageNumber(); } else { // write page header writeTablePageHeader(partialTdef); } // copy the next page of tdef bytes int curTdefPageNumber = nextTdefPageNumber; int writeLen = Math.min(partialTdef.remaining(), buffer.remaining()); partialTdef.put(buffer.array(), buffer.position(), writeLen); ByteUtil.forward(buffer, writeLen); if (buffer.hasRemaining()) { // need a next page nextTdefPageNumber = pageChannel.allocateNewPage(); partialTdef.putInt(format.OFFSET_NEXT_TABLE_DEF_PAGE, nextTdefPageNumber); } // update page free space partialTdef.putShort(format.OFFSET_FREE_SPACE, (short) (partialTdef.remaining() - 8)); // overwrite page free space // write partial page to disk pageChannel.writePage(partialTdef, curTdefPageNumber); } } }
From source file:org.lnicholls.galleon.togo.ToGo.java
public boolean Download(Video video, CancelDownload cancelDownload) { ServerConfiguration serverConfiguration = Server.getServer().getServerConfiguration(); GoBackConfiguration goBackConfiguration = Server.getServer().getGoBackConfiguration(); ArrayList videos = new ArrayList(); GetMethod get = null;//from w ww . j a v a2 s .c o m try { URL url = new URL(video.getUrl()); Protocol protocol = new Protocol("https", new TiVoSSLProtocolSocketFactory(), 443); HttpClient client = new HttpClient(); // TODO How to get TiVo address?? client.getHostConfiguration().setHost(url.getHost(), 443, protocol); String password = Tools.decrypt(serverConfiguration.getMediaAccessKey()); if (video.getParentalControls() != null && video.getParentalControls().booleanValue()) { if (serverConfiguration.getPassword() == null) throw new NullPointerException("Parental Controls Password is null"); password = password + Tools.decrypt(serverConfiguration.getPassword()); } Credentials credentials = new UsernamePasswordCredentials("tivo", password); //client.getState().setCredentials("TiVo DVR", url.getHost(), credentials); client.getState().setCredentials(null, url.getHost(), credentials); get = new GetMethod(video.getUrl()); client.executeMethod(get); if (get.getStatusCode() != 200) { log.debug("Status code: " + get.getStatusCode()); return false; } InputStream input = get.getResponseBodyAsStream(); String path = serverConfiguration.getRecordingsPath(); File dir = new File(path); if (!dir.exists()) { dir.mkdirs(); } String name = getFilename(video); File file = null; if (goBackConfiguration.isGroupByShow()) { if (video.getSeriesTitle() != null && video.getSeriesTitle().trim().length() > 0) { path = path + File.separator + clean(video.getSeriesTitle()); File filePath = new File(path); if (!filePath.exists()) filePath.mkdirs(); file = new File(path + File.separator + name); } else file = new File(path + File.separator + name); } else { file = new File(path + File.separator + name); } // TODO Handle retransfers /* if (file.exists() && video.getStatus()!=Video.STATUS_DOWNLOADING) { log.debug("duplicate file: "+file); try { List list = VideoManager.findByPath(file.getCanonicalPath()); if (list!=null && list.size()>0) { video.setDownloadSize(file.length()); video.setDownloadTime(0); video.setPath(file.getCanonicalPath()); video.setStatus(Video.STATUS_DELETED); VideoManager.updateVideo(video); return true; } } catch (HibernateException ex) { log.error("Video update failed", ex); } } */ log.info("Downloading: " + name); WritableByteChannel channel = new FileOutputStream(file, false).getChannel(); long total = 0; double diff = 0.0; ByteBuffer buf = ByteBuffer.allocateDirect(1024 * 4); byte[] bytes = new byte[1024 * 4]; int amount = 0; int index = 0; long target = video.getSize(); long start = System.currentTimeMillis(); long last = start; while (amount == 0 && total < target) { while (amount >= 0 && !cancelDownload.cancel()) { if (index == amount) { amount = input.read(bytes); index = 0; total = total + amount; } while (index < amount && buf.hasRemaining()) { buf.put(bytes[index++]); } buf.flip(); int numWritten = channel.write(buf); if (buf.hasRemaining()) { buf.compact(); } else { buf.clear(); } if ((System.currentTimeMillis() - last > 10000) && (total > 0)) { try { video = VideoManager.retrieveVideo(video.getId()); if (video.getStatus() == Video.STATUS_DOWNLOADING) { diff = (System.currentTimeMillis() - start) / 1000.0; if (diff > 0) { video.setDownloadSize(total); video.setDownloadTime((int) diff); VideoManager.updateVideo(video); } } } catch (HibernateException ex) { log.error("Video update failed", ex); } last = System.currentTimeMillis(); } } if (cancelDownload.cancel()) { channel.close(); return false; } } diff = (System.currentTimeMillis() - start) / 1000.0; channel.close(); if (diff != 0) log.info("Download rate=" + (total / 1024) / diff + " KBps"); try { video.setPath(file.getCanonicalPath()); VideoManager.updateVideo(video); } catch (HibernateException ex) { log.error("Video update failed", ex); } } catch (MalformedURLException ex) { Tools.logException(ToGo.class, ex, video.getUrl()); return false; } catch (Exception ex) { Tools.logException(ToGo.class, ex, video.getUrl()); return false; } finally { if (get != null) get.releaseConnection(); } return true; }
From source file:org.globus.gsi.gssapi.GlobusGSSContextImpl.java
private byte[] wrap(byte[] inBuf, int off, int len) throws GSSException { try {//from w w w .ja v a 2 s . c om /*DEL this.conn.getOutStream().write(inBuf, off, len); */ ByteBuffer inByteBuff = ByteBuffer.wrap(inBuf, off, len); this.outByteBuff.clear(); outByteBuff = this.sslDataWrap(inByteBuff, outByteBuff); outByteBuff.flip(); if (inByteBuff.hasRemaining()) { throw new Exception("Not all data processed; Original: " + len + " Remaining: " + inByteBuff.remaining() + " Handshaking status: " + sslEngine.getHandshakeStatus()); } } catch (Exception e) { throw new GlobusGSSException(GSSException.FAILURE, e); } if (this.outByteBuff.hasRemaining()) { // TODO can we avoid this copy if the ByteBuffer is array based // and we return that array, each time allocating a new array // for outByteBuff? byte[] out = new byte[this.outByteBuff.remaining()]; this.outByteBuff.get(out, 0, out.length); return out; } else return null; /*DEL return this.out.toByteArray(); */ }