Code example for ByteBuffer

Methods: clear, compact, hasRemaining, position, putInt

0
        lock.lock();
        try { 
            setInputMode(buffer, inBufferInputMode);
            int bytesRead=0;
            try{ 
                bytesRead = decoder.read(buffer);
            } catch(MalformedChunkCodingException ignore) {
            	// we assume that this is a truncated chunk, hence simply ignore the exception 
            	// https://issues.apache.org/jira/browse/HTTPCORE-195 
            	// we should add the EoF character 
            	buffer.putInt(-1);
            	// now the buffer's position should give us the bytes read. 
            	bytesRead = buffer.position();
            	 
            } 
 
            // if consumer is at error we have to let the producer complete 
            if (consumerError) {
                buffer.clear();
            } 
 
            if (!buffer.hasRemaining()) {
                // Input buffer is full. Suspend client input 
                // until the origin handler frees up some space in the buffer 
                producerIoControl.suspendInput();
            } 
 
            // If there is some content in the input buffer make sure consumer output is active 
            if (buffer.position() > 0 || decoder.isCompleted()) {
                if (consumerIoControl != null) {
                    consumerIoControl.requestOutput();
                } 
                readCondition.signalAll();
            } 
 
            if (decoder.isCompleted()) {
                producerCompleted = true;
            } 
           return bytesRead;
        } finally { 
            lock.unlock();
        } 
    } 
 
    @Override 
    public String toString() {
        return name;
    } 
 
    public void consumerError() { 
        lock.lock();
        try { 
            this.consumerError = true;
            writeCondition.signalAll();
        } finally { 
            lock.unlock();
        } 
    } 
 
    public void producerError() { 
        lock.lock();
        try { 
            this.producerError = true;
            readCondition.signalAll();
        } finally { 
            lock.unlock();
        } 
    } 
 
    /** 
     * Creates an InputStream object on the underlying ByteBuffer. The returned 
     * InputStream can be used to read bytes from the underlying buffer which 
     * is being filled by the producer. 
     * 
     * @return An InputStream object 
     */ 
    public synchronized InputStream getInputStream() {
        if (inputStream == null) {
            inputStream = new ByteBufferInputStream();
        } 
        return inputStream;
    } 
 
    /** 
     * Creates a separate ByteBuffer for the output data and returns an OutputStream 
     * on top of it. 
     * 
     * @return An OutputStream object 
     */ 
    public synchronized OutputStream getOutputStream() {
        if (outputStream == null) {
            outputBuffer = baseConfig.getBufferFactory().getBuffer();
            outBufferInputMode = new AtomicBoolean(true);
            outputStream = new ByteBufferOutputStream();
        } 
        return outputStream;
    } 
     
     
    /** 
     * Creates a separate ByteBuffer for the output data and returns an OutputStream 
     * on top of it. 
     * 
     * @return An OutputStream object 
     */ 
    public synchronized OutputStream resetOutputStream() {
    	outputBuffer = baseConfig.getBufferFactory().getBuffer();
        outBufferInputMode = new AtomicBoolean(true);
        outputStream = new ByteBufferOutputStream();
        return outputStream;
    } 
 
    public synchronized void setSerializationComplete(boolean serializationComplete) {
        if (!this.serializationComplete) {
            this.serializationComplete = serializationComplete;
            if (consumerIoControl != null && hasData(outputBuffer, outBufferInputMode)) {
                consumerIoControl.requestOutput();
            } 
        } 
    } 
     
    public synchronized void setSerializationCompleteWithoutData(boolean serializationComplete) {
        if (!this.serializationComplete) {
            this.serializationComplete = serializationComplete;
            consumerIoControl.requestOutput();
        } 
    } 
     
    public void setRawSerializationComplete(boolean rawSerializationComplete) {
    	this.rawSerializationComplete = rawSerializationComplete;
    } 
     
    public void forceSetSerializationRest(){ 
    	if(this.serializationComplete){
    		this.serializationComplete = false;
    	} 
    } 
 
     
     
 
    public boolean isSerializationComplete() { 
		return serializationComplete;
	} 
 
	public ByteBuffer getBuffer() {
        return buffer;
    } 
 
    public boolean hasHttpProducer() { 
        return hasHttpProducer;
    } 
 
    private void setInputMode(ByteBuffer buffer, AtomicBoolean inputMode) {
        if (inputMode.compareAndSet(false, true)) {
            if (buffer.hasRemaining()) {
                buffer.compact();
            } else { 
                buffer.clear();
            } 
        } 
    } 
 
    private void setOutputMode(ByteBuffer buffer, AtomicBoolean inputMode) {