ByteBuffer compact - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for ByteBuffer compact method.

This code example shows how to use the following methods:capacity, flip, limit, position, compact
		return length;
	} 
 
	@Override 
	public int put(byte[] chunk) {
		int startPosition = buf.position();
		int endPosition = buf.limit();
		//compact the buffer before every put operation... 
		int roomLeft = buf.capacity()-endPosition;
		if(chunk.length > roomLeft) {
			buf.compact();
			buf.flip();
			startPosition = buf.position();
			endPosition = buf.limit();
		} 
		 
		//prepare for more writing 
		buf.position(buf.limit());
		buf.limit(buf.capacity());
		buf.put(chunk);

7
CodeRank
This code example shows how to use the following methods:flip, limit, position, remaining, compact
 
	/** 
	 * Copy available events into the specified buffer. 
	 */ 
	public synchronized void copyEvents(ByteBuffer dest) {
		queue.flip();
		int old_limit = queue.limit();
		if (dest.remaining() < queue.remaining())
			queue.limit(dest.remaining() + queue.position());
		dest.put(queue);
		queue.limit(old_limit);
		queue.compact();
	} 
 
	/** 
	 * Put an event into the queue. 
	 * @return true if the event fitted into the queue, false otherwise 
	 */ 
	public synchronized boolean putEvent(ByteBuffer event) {
		if (event.remaining() != event_size)
7
CodeRank
This code example shows how to use the following methods:flip, hasRemaining, compact
    public void produceContent( 
            final ContentEncoder encoder, final IOControl ioctrl) throws IOException {
        if (this.channel == null) {
            this.channel = Channels.newChannel(this.entity.getContent());
        } 
        final int i = this.channel.read(this.buffer);
        this.buffer.flip();
        encoder.write(this.buffer);
        final boolean buffering = this.buffer.hasRemaining();
        this.buffer.compact();
        if (i == -1 && !buffering) {
            encoder.complete();
            close(); 
        } 
    } 
 
    public boolean isRepeatable() { 
        return this.entity.isRepeatable();
    } 
 
7
CodeRank
This code example shows how to use the following methods:clear, flip, limit, array, compact
		} 
	 
		@Override 
		protected void onBinaryMessage(ByteBuffer message) throws IOException
		{ 
			message.flip();
			message.compact();
			log.debug(message,message.limit());
			byte[] copy = Arrays.copyOf(message.array(), message.limit());
			message.clear();
			//log.debug("flip/compact/clear"); 
			log.debug("client->server(B)", copy.length);
			minaAdapter.write(IoBuffer.wrap(copy));
			message.clear();
		} 
 
		@Override 
		protected void onTextMessage(CharBuffer message) throws IOException
		{ 
			message.flip();
6
CodeRank
This code example shows how to use the following methods:limit, position, compact, get, ByteBuffer.position
         /// \todo refactor this for streaming processing, incl. fail fast on invalid UTF-8 within frame already 
 
         // within frame 
 
         // see if we buffered complete frame 
         if (mFrameBuffer.position() >= mFrameHeader.mTotalLen) {
 
            // cut out frame payload 
            byte[] framePayload = null;
            int oldPosition = mFrameBuffer.position();
            if (mFrameHeader.mPayloadLen > 0) {
               framePayload = new byte[mFrameHeader.mPayloadLen];
               mFrameBuffer.position(mFrameHeader.mHeaderLen);
               mFrameBuffer.get(framePayload, 0, (int) mFrameHeader.mPayloadLen);
            } 
            mFrameBuffer.position(mFrameHeader.mTotalLen);
            mFrameBuffer.limit(oldPosition);
            mFrameBuffer.compact();
 
            if (mFrameHeader.mOpcode > 7) {

6
CodeRank
This code example shows how to use the following methods:flip, hasRemaining, compact
    } 
 
    public void produceContent( 
            final ContentEncoder encoder,
            final IOControl ioctrl) throws IOException {
        int i = this.channel.read(this.buffer);
        this.buffer.flip();
        encoder.write(this.buffer);
        boolean buffering = this.buffer.hasRemaining();
        this.buffer.compact();
        if (i == -1 && !buffering) {
            encoder.complete();
            this.channel.close();
        } 
    } 
 
    public void finish() { 
        try { 
            this.channel.close();
        } catch (IOException ignore) {
7
CodeRank
This code example shows how to use the following methods:limit, position, compact
     * "Move" the packet data out of the buffer we're sitting on and into 
     * buf at the current position. 
     */ 
    void movePacket(ByteBuffer buf) {
        Log.v("ddms", "moving " + mLength + " bytes");
        int oldPosn = mBuffer.position();
 
        mBuffer.position(0);
        mBuffer.limit(mLength);
        buf.put(mBuffer);
        mBuffer.position(mLength);
        mBuffer.limit(oldPosn);
        mBuffer.compact();      // shift posn...limit, posn<-pending data
    } 
 
    /** 
     * Consume the JDWP packet. 
     * 
     * On entry and exit, "position" is the #of bytes in the buffer. 
     */ 
6
CodeRank
This code example shows how to use the following methods:clear, hasRemaining, remaining, compact
    /** 
     * Sets input mode. The buffer can now be written into. 
     */ 
    protected void setInputMode() { 
        if (this.mode != INPUT_MODE) {
            if (this.buffer.hasRemaining()) {
                this.buffer.compact();
            } else { 
                this.buffer.clear();
            } 
            this.mode = INPUT_MODE;
        } 
    } 
 
    private void expandCapacity(int capacity) {
        ByteBuffer oldbuffer = this.buffer;
        this.buffer = allocator.allocate(capacity);
        oldbuffer.flip();
        this.buffer.put(oldbuffer);
    } 

6
CodeRank
This code example shows how to use the following methods:flip, limit, position, remaining, compact
 
	/** 
	 * Copy available events into the specified buffer. 
	 */ 
	public synchronized void copyEvents(ByteBuffer dest) {
		queue.flip();
		int old_limit = queue.limit();
		if (dest.remaining() < queue.remaining())
			queue.limit(dest.remaining() + queue.position());
		dest.put(queue);
		queue.limit(old_limit);
		queue.compact();
	} 
 
	/** 
	 * Put an event into the queue. 
	 * @return true if the event fitted into the queue, false otherwise 
	 */ 
	public synchronized boolean putEvent(ByteBuffer event) {
		if (event.remaining() != event_size)
6
CodeRank
This code example shows how to use the following methods:capacity, flip, hasRemaining, limit, position
			processTelnetCommand(); 
		} 
	} 
 
	private void onSelectWrite(SelectionKey key) throws IOException {
		output.flip();
		if (output.hasRemaining()) {
			try { 
				socketChannel.write(output);
			} catch(IOException ex) {
				closeSocketChannel(); 
				throw ex;
			} 
			output.compact();
			output.limit(output.capacity());
		} 
 
		if (output.position() == 0) {
			enableWrites(false); 
		} 

6
CodeRank

Related ByteBuffer compact Questions & Answers:

compact Method Overview
Compacts this buffer  (optional operation).
See Code Examples for other Java 8 ByteBuffer Methods: