Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (C) FuseSource, Inc. http://fusesource.com Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.fusesource.fabric.dosgi.tcp;
 
 
 
 public class LengthPrefixedCodec implements ProtocolCodec {
 
 
     int write_buffer_size = 1024 * 64;
     long write_counter = 0L;
     ByteBuffer write_buffer = ByteBuffer.allocate(0);
 
     int next_write_size = 0;
 
     public boolean full() {
         return  >= ( >> 1);
     }
 
     protected boolean empty() {
         return .remaining() == 0 && ==0;
     }
 
     public void setWritableByteChannel(WritableByteChannel channel) {
         this. = channel;
         if (channel instanceof SocketChannel) {
             try {
                 ((SocketChannelchannel).socket().setSendBufferSize();
             } catch (SocketException e) {
                 e.printStackTrace();
             }
         }
     }
 
     public BufferState write(Object valuethrows IOException {
         if (full()) {
             return .;
         } else {
             boolean wasEmpty = empty();
             Buffer buffer = (Buffervalue;
              += buffer.length;
             .add(buffer);
             return wasEmpty ? . : .;
         }
     }
 
     public BufferState flush() throws IOException {
         if (.remaining() == 0 &&  > 0) {
             if.size()==1 ) {
                  = .remove(0).toByteBuffer();
             } else {
                 // consolidate the buffers into 1 big buffer to reduce
                 // the number of system calls we do.
 
                 if.capacity() <  ) {
                     // Re-allocate if we need a bigger write buffer...
                      = ByteBuffer.allocate();
                 } else if <  && .capacity() >  )  {
                     // Re-allocate if We don't need that big write buffer anymore..
                      = ByteBuffer.allocate();
                 }
 
                 .clear();
                 forBuffer b) {
                     .put(b.datab.offsetb.length);
                 }
                 .clear();
                  = 0;
                 .flip();
             }
 
         }
        if (.remaining() != 0) {
             += .write();
        }
        return empty() ? . : .;
    }
    public long getWriteCounter() {
        return ;
    }
    long read_counter = 0L;
    int read_buffer_size = 1024 * 64;
    ByteBuffer read_buffer = ByteBuffer.allocate(4);
    public void setReadableByteChannel(ReadableByteChannel channel) {
         = channel;
        if (channel instanceof SocketChannel) {
            try {
                ((SocketChannelchannel).socket().setReceiveBufferSize();
            } catch (SocketException e) {
                e.printStackTrace();
            }
        }
    }
    public Object read() throws IOException {
        while(true) {
            if.remaining()!=0 ) {
                // keep reading from the channel until we fill the read buffer..
                int count = .read();
                if (count == -1) {
                    throw new EOFException("Peer disconnected");
                } else if (count == 0) {
                    return null;
                }
                 += count;
            } else {
                //read buffer is full.. interpret it..
                .flip();
                if.capacity() == 4 ) {
                    // Finding out the
                    int size = .getInt(0);
                    ifsize < 4 ) {
                        throw new ProtocolException("Expecting a size greater than 3");
                    }
                    ifsize == 4 ) {
                        // weird.. empty frame.. guess it could happen.
                        Buffer rc = new Buffer();
                         = ByteBuffer.allocate(4);
                        return rc;
                    } else {
                        // Resize to the right size.. this resumes the reads..
                        ByteBuffer next = ByteBuffer.allocate(size);
                        next.putInt(size);
                         = next;
                    }
                } else {
                    // finish loading the rest of the buffer..
                    Buffer rc = new Buffer();
                     = ByteBuffer.allocate(4);
                    return rc;
                }
            }
        }
    }
    public long getReadCounter() {
        return ;
    }
New to GrepCode? Check out our FAQ X