List of usage examples for java.nio ByteBuffer position
public final int position()
From source file:com.slytechs.capture.file.editor.FileEditorImpl.java
public void replaceInPlace(final long global, final boolean copy) throws IOException { // existing record from buffer final ByteBuffer original = this.get(global, headerReader); // its length final int length = original.limit() - original.position(); // create new buffer by copy of the original final PartialLoader loader = new MemoryCacheLoader(original, copy, headerReader); // now the replacement by region with the new buffer this.edits.replace(global, length, length, loader); this.autoflushChange(length * (copy ? 2 : 1)); }
From source file:com.turn.ttorrent.common.TorrentCreator.java
/** * Return the concatenation of the SHA-1 hashes of a file's pieces. * * <p>/*from ww w . ja va2s. co m*/ * Hashes the given file piece by piece using the default Torrent piece * length (see {@link #PIECE_LENGTH}) and returns the concatenation of * these hashes, as a string. * </p> * * <p> * This is used for creating Torrent meta-info structures from a file. * </p> * * @param file The file to hash. */ public /* for testing */ static byte[] hashFiles(Executor executor, List<File> files, long nbytes, int pieceLength) throws InterruptedException, IOException { int npieces = (int) Math.ceil((double) nbytes / pieceLength); byte[] out = new byte[Torrent.PIECE_HASH_SIZE * npieces]; CountDownLatch latch = new CountDownLatch(npieces); ByteBuffer buffer = ByteBuffer.allocate(pieceLength); long start = System.nanoTime(); int piece = 0; for (File file : files) { logger.info("Hashing data from {} ({} pieces)...", new Object[] { file.getName(), (int) Math.ceil((double) file.length() / pieceLength) }); FileInputStream fis = FileUtils.openInputStream(file); FileChannel channel = fis.getChannel(); int step = 10; try { while (channel.read(buffer) > 0) { if (buffer.remaining() == 0) { buffer.flip(); executor.execute(new ChunkHasher(out, piece, latch, buffer)); buffer = ByteBuffer.allocate(pieceLength); piece++; } if (channel.position() / (double) channel.size() * 100f > step) { logger.info(" ... {}% complete", step); step += 10; } } } finally { channel.close(); fis.close(); } } // Hash the last bit, if any if (buffer.position() > 0) { buffer.flip(); executor.execute(new ChunkHasher(out, piece, latch, buffer)); piece++; } // Wait for hashing tasks to complete. latch.await(); long elapsed = System.nanoTime() - start; logger.info("Hashed {} file(s) ({} bytes) in {} pieces ({} expected) in {}ms.", new Object[] { files.size(), nbytes, piece, npieces, String.format("%.1f", elapsed / 1e6) }); return out; }
From source file:tachyon.master.RawTables.java
/** * Update the metadata of the specified raw table. It will check if the table exists. * * @param tableId The id of the raw table * @param metadata The new metadata of the raw table * @throws TachyonException/*from w w w . ja v a 2 s. com*/ */ // TODO add version number. public synchronized void updateMetadata(int tableId, ByteBuffer metadata) throws TachyonException { Pair<Integer, ByteBuffer> data = mData.get(tableId); if (null == data) { throw new TachyonException("The raw table " + tableId + " does not exist."); } if (metadata == null) { data.setSecond(ByteBuffer.allocate(0)); } else { long maxVal = mTachyonConf.getBytes(Constants.MAX_TABLE_METADATA_BYTE, 0L); if (metadata.limit() - metadata.position() >= maxVal) { throw new TachyonException("Too big table metadata: " + metadata.toString()); } ByteBuffer tMetadata = ByteBuffer.allocate(metadata.limit() - metadata.position()); tMetadata.put(metadata.array(), metadata.position(), metadata.limit() - metadata.position()); tMetadata.flip(); data.setSecond(tMetadata); } }
From source file:eu.stratosphere.runtime.io.network.netty.InboundEnvelopeDecoder.java
/** * Copies min(from.readableBytes(), to.remaining() bytes from Nettys ByteBuf to the Java NIO ByteBuffer. *//*w ww . j a v a 2 s . c om*/ private void copy(ByteBuf src, ByteBuffer dst) { // This branch is necessary, because an Exception is thrown if the // destination buffer has more remaining (writable) bytes than // currently readable from the Netty ByteBuf source. if (src.isReadable()) { if (src.readableBytes() < dst.remaining()) { int oldLimit = dst.limit(); dst.limit(dst.position() + src.readableBytes()); src.readBytes(dst); dst.limit(oldLimit); } else { src.readBytes(dst); } } }
From source file:com.spidertracks.datanucleus.convert.ByteConverterContext.java
/** * Get the row key for the given id//from www . j a v a 2 s . co m * * @param ec * @param id * @return */ public Bytes getRowKeyForId(Object id) { ByteBuffer buffer = getRowKeyForId(id, null); buffer.limit(buffer.position()); buffer.reset(); return Bytes.fromByteBuffer(buffer); }
From source file:com.l2jfree.network.mmocore.ReadWriteThread.java
private void parseClientPacket(ByteBuffer buf, int dataSize, T client) { final int pos = buf.position(); final DataSizeHolder dsh = getDataSizeHolder().init(dataSize); if (client.decipher(buf, dsh) && buf.hasRemaining()) { // remove useless bytes dsh.decreaseSize(dsh.getMinPadding()); // calculate possibly remaining useless bytes final int maxPossiblePadding = dsh.getMaxPadding() - dsh.getMinPadding(); // apply limit final int limit = buf.limit(); buf.limit(pos + dsh.getSize());//from www. j a v a 2 s. c o m final int opcode = buf.get() & 0xFF; if (getMMOController().canReceivePacketFrom(client, opcode)) { RP cp = getPacketHandler().handlePacket(buf, client, opcode); if (cp != null) { System.out.println("READ: " + client.getState() + " " + cp.getClass().getSimpleName()); // remove useless bytes #2, using packet specs int maxLeftoverPadding = maxPossiblePadding; final int overflow = buf.remaining() - cp.getMaximumLength(); if (maxPossiblePadding > 0 && // there may be useless bytes overflow > 0) // and we have too much { // avoid any damage to the packet body final int removable = Math.min(overflow, maxPossiblePadding); buf.limit(buf.limit() - removable); maxLeftoverPadding -= removable; } getMmoBuffer().setByteBuffer(buf); cp.setClient(client); try { if (getMmoBuffer().getAvailableBytes() < cp.getMinimumLength()) { getMMOController().report(ErrorMode.BUFFER_UNDER_FLOW, client, cp, null); } else if (getMmoBuffer().getAvailableBytes() > cp.getMaximumLength()) { getMMOController().report(ErrorMode.BUFFER_OVER_FLOW, client, cp, null); } else { cp.read(getMmoBuffer()); client.executePacket(cp); if (buf.hasRemaining() && // some unused data, a bad sign buf.remaining() > maxLeftoverPadding) // and definitely not padded bytes { // FIXME disabled until packet structures updated properly //report(ErrorMode.BUFFER_OVER_FLOW, client, cp, null); MMOController._log.info("Invalid packet format (buf: " + buf + ", dataSize: " + dataSize + ", pos: " + pos + ", limit: " + limit + ", opcode: 0x" + HexUtil.fillHex(opcode, 2) + ") used for reading - " + client + " - " + cp.getType() + " - " + getMMOController().getVersionInfo()); } } } catch (BufferUnderflowException e) { getMMOController().report(ErrorMode.BUFFER_UNDER_FLOW, client, cp, e); } catch (RuntimeException e) { getMMOController().report(ErrorMode.FAILED_READING, client, cp, e); } getMmoBuffer().setByteBuffer(null); } } buf.limit(limit); } }
From source file:byps.test.TestRemoteStreams.java
/** * Send and receive a stream with content length information. * /*ww w . j a v a2s . c o m*/ * @throws InterruptedException * @throws IOException */ @Test public void testRemoteStreamsOneStreamContentLength() throws InterruptedException, IOException { log.info("testRemoteStreamsOneStreamContentLength("); String str = "hello"; InputStream istrm = new ByteArrayInputStream(str.getBytes()); remote.setImage(istrm); InputStream istrmR = remote.getImage(); ByteBuffer buf = BWire.bufferFromStream(istrmR); String strR = new String(buf.array(), buf.position(), buf.remaining()); TestUtils.assertEquals(log, "stream", str, strR); remote.setImage(null); TestUtils.checkTempDirEmpty(client); log.info(")testRemoteStreamsOneStreamContentLength"); }
From source file:edu.hawaii.soest.pacioos.text.SocketTextSource.java
@Override protected boolean execute() { log.debug("SocketTextSource.execute() called."); // do not execute the stream if there is no connection if (!isConnected()) return false; boolean failed = false; /* Get a connection to the instrument */ SocketChannel socket = getSocketConnection(); if (socket == null) return false; // while data are being sent, read them into the buffer try {/*from w w w . j av a2 s .c om*/ // 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()); // 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(); // log the byte stream String character = new String(new byte[] { byteOne }); if (log.isDebugEnabled()) { List<Byte> whitespaceBytes = new ArrayList<Byte>(); whitespaceBytes.add(new Byte((byte) 0x0A)); whitespaceBytes.add(new Byte((byte) 0x0D)); if (whitespaceBytes.contains(new Byte(byteOne))) { character = new String(Hex.encodeHex((new byte[] { byteOne }))); } } log.debug("char: " + character + "\t" + "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); // evaluate each byte to find the record delimiter(s), and when found, validate and // send the sample to the DataTurbine. int numberOfChannelsFlushed = 0; if (getRecordDelimiters().length == 2) { // have we hit the delimiters in the stream yet? if (byteTwo == getFirstDelimiterByte() && byteOne == getSecondDelimiterByte()) { sampleBuffer.put(byteOne); sampleByteCount++; // extract just the length of the sample bytes out of the // sample buffer, and place it in the channel map as a // byte array. Then, send it to the DataTurbine. log.debug("Sample byte count: " + sampleByteCount); byte[] sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); String sampleString = new String(sampleArray, "US-ASCII"); if (validateSample(sampleString)) { numberOfChannelsFlushed = sendSample(sampleString); } sampleBuffer.clear(); sampleByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; log.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); } else { // 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(); log.debug("Compacting sampleBuffer ..."); sampleBuffer.put(byteOne); } } } else if (getRecordDelimiters().length == 1) { // have we hit the delimiter in the stream yet? if (byteOne == getFirstDelimiterByte()) { sampleBuffer.put(byteOne); sampleByteCount++; // extract just the length of the sample bytes out of the // sample buffer, and place it in the channel map as a // byte array. Then, send it to the DataTurbine. byte[] sampleArray = new byte[sampleByteCount]; sampleBuffer.flip(); sampleBuffer.get(sampleArray); String sampleString = new String(sampleArray, "US-ASCII"); if (validateSample(sampleString)) { numberOfChannelsFlushed = sendSample(sampleString); } sampleBuffer.clear(); sampleByteCount = 0; byteOne = 0x00; byteTwo = 0x00; byteThree = 0x00; byteFour = 0x00; log.debug("Cleared b1,b2,b3,b4. Cleared sampleBuffer. Cleared rbnbChannelMap."); } else { // 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(); log.debug("Compacting sampleBuffer ..."); sampleBuffer.put(byteOne); } } } // end getRecordDelimiters().length // 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) 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; log.error("There was a communication error in sending the data sample. The message was: " + e.getMessage()); if (log.isDebugEnabled()) { 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; log.error("There was an RBNB error while sending the data sample. The message was: " + sapie.getMessage()); if (log.isDebugEnabled()) { sapie.printStackTrace(); } return !failed; } return !failed; }
From source file:byps.test.TestRemoteStreams.java
/** * Send and receive a stream without content length information * /*from ww w . java 2 s. c om*/ * @throws InterruptedException * @throws IOException */ @Test public void testRemoteStreamsOneStreamChunked() throws InterruptedException, IOException { log.info("testRemoteStreamsOneStreamChunked("); String str = "hello"; final ByteArrayInputStream bis = new ByteArrayInputStream(str.getBytes()); InputStream istrm = new BContentStream() { @Override public long getContentLength() { return -1L; } @Override public int read() throws IOException { return bis.read(); } }; remote.setImage(istrm); InputStream istrmR = remote.getImage(); ByteBuffer buf = BWire.bufferFromStream(istrmR); String strR = new String(buf.array(), buf.position(), buf.remaining()); TestUtils.assertEquals(log, "stream", str, strR); remote.setImage(null); TestUtils.checkTempDirEmpty(client); log.info(")testRemoteStreamsOneStreamChunked"); }
From source file:byps.test.TestRemoteStreams.java
/** * Send file stream.//from w w w .j a v a 2 s . c om * A file stream has the fileName property set. * @throws InterruptedException * @throws IOException */ @Test public void testRemoteStreamsFileStream() throws InterruptedException, IOException { log.info("testRemoteStreamsFileStream("); File file = File.createTempFile("byps", ".txt"); String str = "hello"; FileOutputStream fos = null; try { fos = new FileOutputStream(file); fos.write(str.getBytes()); } finally { if (fos != null) fos.close(); } InputStream istrm = new BContentStreamWrapper(file); remote.setImage(istrm); BContentStream istrmR = (BContentStream) remote.getImage(); TestUtils.assertEquals(log, "Content-Type", "text/plain", istrmR.getContentType()); TestUtils.assertEquals(log, "Content-Length", file.length(), istrmR.getContentLength()); TestUtils.assertEquals(log, "FileName", file.getName(), istrmR.getFileName()); ByteBuffer buf = BWire.bufferFromStream(istrmR); String strR = new String(buf.array(), buf.position(), buf.remaining()); TestUtils.assertEquals(log, "stream", str, strR); TestUtils.assertEquals(log, "Content-Type", "text/plain", istrmR.getContentType()); TestUtils.assertEquals(log, "Content-Length", file.length(), istrmR.getContentLength()); TestUtils.assertEquals(log, "FileName", file.getName(), istrmR.getFileName()); remote.setImage(null); TestUtils.checkTempDirEmpty(client); log.info(")testRemoteStreamsFileStream"); }