List of usage examples for java.nio ByteBuffer position
public final int position()
From source file:edu.umn.cs.spatialHadoop.core.ZCurvePartitioner.java
@Override public void write(DataOutput out) throws IOException { mbr.write(out);//w w w .j a va 2 s. com out.writeInt(zSplits.length); ByteBuffer bbuffer = ByteBuffer.allocate(zSplits.length * 8); for (long zSplit : zSplits) bbuffer.putLong(zSplit); if (bbuffer.hasRemaining()) throw new RuntimeException("Did not calculate buffer size correctly"); out.write(bbuffer.array(), bbuffer.arrayOffset(), bbuffer.position()); }
From source file:client.MultiplexingClient.java
/** * Creates a new packet with a size chosen randomly between * MIN_SIZE and MAX_SIZE. //from w w w. j a va 2s . com */ private ByteBuffer generateNextPacket() { // Generate a random size between int size = MIN_SIZE + r.nextInt(maxPcktSize - MIN_SIZE); ByteBuffer buffer = ByteBuffer.allocate(size); buffer.put(SimpleProtocolDecoder.STX); for (int i = 0; i < size - 2; i++) { buffer.put((byte) ('0' + (i % 10))); } buffer.put(SimpleProtocolDecoder.ETX); buffer.limit(buffer.position()); buffer.flip(); return buffer; }
From source file:com.kylinolap.common.hll.HyperLogLogPlusCounter.java
public void readRegisters(ByteBuffer in) throws IOException { byte scheme = in.get(); if ((scheme & COMPRESSION_FLAG) > 0) { scheme ^= COMPRESSION_FLAG;/*from www .j a v a2 s. c o m*/ int compressedLen = BytesUtil.readVInt(in); int end = in.position() + compressedLen; byte[] decompressed = DEFAULT_COMPRESSOR.decompress(in, in.position(), compressedLen); in.position(end); in = ByteBuffer.wrap(decompressed); } if (scheme == 0) { // map scheme clear(); int size = BytesUtil.readVInt(in); if (size > m) throw new IllegalArgumentException( "register size (" + size + ") cannot be larger than m (" + m + ")"); int indexLen = getRegisterIndexSize(); for (int i = 0; i < size; i++) { int key = BytesUtil.readUnsigned(in, indexLen); registers[key] = in.get(); } } else { // array scheme for (int i = 0; i < m; i++) { registers[i] = in.get(); } } }
From source file:com.l2jfree.gameserver.network.L2Client.java
@Override public boolean decrypt(ByteBuffer buf, int size) { getCrypt().decrypt(buf.array(), buf.position(), size); return true;/*from w ww . j a v a 2 s . c om*/ }
From source file:com.intel.chimera.cipher.Openssl.java
/** * Finishes a multiple-part operation. The data is encrypted or decrypted, * depending on how this cipher was initialized. * <p/>/*from w w w . jav a2 s . com*/ * * The result is stored in the output buffer. Upon return, the output buffer's * position will have advanced by n, where n is the value returned by this * method; the output buffer's limit will not have changed. * <p/> * * If <code>output.remaining()</code> bytes are insufficient to hold the result, * a <code>ShortBufferException</code> is thrown. * <p/> * * Upon finishing, this method resets this cipher object to the state it was * in when previously initialized. That is, the object is available to encrypt * or decrypt more data. * <p/> * * If any exception is thrown, this cipher object need to be reset before it * can be used again. * * @param output the output ByteBuffer * @return int number of bytes stored in <code>output</code> * @throws ShortBufferException * @throws IllegalBlockSizeException * @throws BadPaddingException */ public int doFinal(ByteBuffer output) throws ShortBufferException, IllegalBlockSizeException, BadPaddingException { checkState(); Utils.checkArgument(output.isDirect(), "Direct buffer is required."); int len = OpensslNative.doFinal(context, output, output.position(), output.remaining()); output.position(output.position() + len); return len; }
From source file:com.l2jfree.gameserver.network.L2Client.java
@Override public boolean encrypt(final ByteBuffer buf, final int size) { getCrypt().encrypt(buf.array(), buf.position(), size); buf.position(buf.position() + size); return true;// www . j a va2 s.c om }
From source file:org.wso2.carbon.inbound.endpoint.protocol.http2.http2Encoder.java
@Override public int write(ByteBuffer src) throws IOException { while (src.hasRemaining()) { byte[] b; b = new byte[src.remaining()]; src.get(b);//from ww w. j a v a 2 s .c o m if (src.hasRemaining()) encoder.writeData(chContext, streamId, Unpooled.wrappedBuffer(b), 0, false, promise); else { encoder.writeData(chContext, streamId, Unpooled.wrappedBuffer(b), 0, true, promise); isComplete = true; } } return src.position(); }
From source file:org.red5.stream.http.servlet.TransportSegmentFeeder.java
/** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse * response)//from w w w . j av a 2 s . c o m */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.debug("Segment feed requested"); // get red5 context and segmenter if (service == null) { ApplicationContext appCtx = (ApplicationContext) getServletContext() .getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); service = (SegmenterService) appCtx.getBean("segmenter.service"); } // get the requested stream / segment String servletPath = request.getServletPath(); String streamName = servletPath.split("\\.")[0]; log.debug("Stream name: {}", streamName); if (service.isAvailable(streamName)) { response.setContentType("video/MP2T"); // data segment Segment segment = null; // setup buffers and output stream byte[] buf = new byte[188]; ByteBuffer buffer = ByteBuffer.allocate(188); ServletOutputStream sos = response.getOutputStream(); // loop segments while ((segment = service.getSegment(streamName)) != null) { do { buffer = segment.read(buffer); // log.trace("Limit - position: {}", (buffer.limit() - buffer.position())); if ((buffer.limit() - buffer.position()) == 188) { buffer.get(buf); // write down the output stream sos.write(buf); } else { log.info("Segment result has indicated a problem"); // verifies the currently requested stream segment // number against the currently active segment if (service.getSegment(streamName) == null) { log.debug("Requested segment is no longer available"); break; } } buffer.clear(); } while (segment.hasMoreData()); log.trace("Segment {} had no more data", segment.getIndex()); // flush sos.flush(); // segment had no more data segment.cleanupThreadLocal(); } buffer.clear(); buffer = null; } else { // let requester know that stream segment is not available response.sendError(404, "Requested segmented stream not found"); } }
From source file:com.buaa.cfs.nfs3.WriteCtx.java
public void writeData(DataOutputStream fos) throws IOException { Preconditions.checkState(fos != null); ByteBuffer dataBuffer; try {/* w w w . j av a 2s . c o m*/ dataBuffer = getData(); } catch (Exception e1) { LOG.error("Failed to get request data offset:" + offset + " count:" + count + " error:" + e1); throw new IOException("Can't get WriteCtx.data"); } byte[] data = dataBuffer.array(); int position = dataBuffer.position(); int limit = dataBuffer.limit(); Preconditions.checkState(limit - position == count); // Modified write has a valid original count if (position != 0) { if (limit != getOriginalCount()) { throw new IOException("Modified write has differnt original size." + "buff position:" + position + " buff limit:" + limit + ". " + toString()); } } // Now write data // fos.write(data, position, count); }
From source file:net.kungfoo.grizzly.proxy.impl.ConnectingHandler.java
public void outputReady(final NHttpClientConnection conn, final ContentEncoder encoder) { System.out.println(conn + " [proxy->origin] output ready"); HttpContext context = conn.getContext(); ProxyProcessingInfo proxyTask = (ProxyProcessingInfo) context.getAttribute(ProxyProcessingInfo.ATTRIB); synchronized (proxyTask) { ConnState connState = proxyTask.getOriginState(); if (connState != ConnState.REQUEST_SENT && connState != ConnState.REQUEST_BODY_STREAM) { throw new IllegalStateException("Illegal target connection state: " + connState); }/*from w w w .j av a 2s . co m*/ try { // TODO: propper handling of POST ByteBuffer src = proxyTask.getInBuffer(); final int srcSize = src.limit(); if (src.position() != 0) { System.out.println(conn + " [proxy->origin] buff not consumed yet"); return; } ByteChunk chunk = new ByteChunk(srcSize); Request originalRequest = proxyTask.getOriginalRequest(); int read; int encRead = 0; long bytesWritten = 0; while ((read = originalRequest.doRead(chunk)) != -1) { System.out.println(conn + " [proxy->origin] " + read + " bytes read"); if (read > srcSize) { src = ByteBuffer.wrap(chunk.getBytes(), chunk.getOffset(), read); } else { src.put(chunk.getBytes(), chunk.getOffset(), read); } src.flip(); encRead = encoder.write(src); bytesWritten += encRead; src.compact(); chunk.reset(); if (encRead == 0) { System.out.println(conn + " [proxy->origin] encoder refused to consume more"); break; } else { System.out.println(conn + " [proxy->origin] " + encRead + " consumed by encoder"); } } System.out.println(conn + " [proxy->origin] " + bytesWritten + " bytes written"); System.out.println(conn + " [proxy->origin] " + encoder); src.compact(); if (src.position() == 0 && encRead != 0) { encoder.complete(); } // Update connection state if (encoder.isCompleted()) { System.out.println(conn + " [proxy->origin] request body sent"); proxyTask.setOriginState(ConnState.REQUEST_BODY_DONE); } else { proxyTask.setOriginState(ConnState.REQUEST_BODY_STREAM); } } catch (IOException ex) { shutdownConnection(conn); } } }