Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You 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.apache.catalina.tribes.io;
 
 
The object reader object is an object used in conjunction with java.nio TCP messages. This object stores the message bytes in a XByteBuffer until a full package has been received. This object uses an XByteBuffer which is an extendable object buffer that also allows for message encoding and decoding.
 
 public class ObjectReader {
 
     private static final Log log = LogFactory.getLog(ObjectReader.class);
 
     private XByteBuffer buffer;
 
     protected long lastAccess = System.currentTimeMillis();
 
     protected boolean accessed = false;
     private boolean cancelled;
 
     public ObjectReader(int packetSize) {
         this. = new XByteBuffer(packetSizetrue);
     }
    
Creates an ObjectReader for a TCP NIO socket channel

Parameters:
channel - the channel to be read.
 
     public ObjectReader(SocketChannel channel) {
         this(channel.socket());
     }

    
Creates an ObjectReader for a TCP socket

Parameters:
socket Socket
 
     public ObjectReader(Socket socket) {
         try{
             this. = new XByteBuffer(socket.getReceiveBufferSize(), true);
         }catch ( IOException x ) {
             //unable to get buffer size
             .warn("Unable to retrieve the socket receiver buffer size, setting to default 43800 bytes.");
             this. = new XByteBuffer(43800,true);
         }
     }
 
     public synchronized void access() {
         this. = true;
         this. = System.currentTimeMillis();
     }
 
     public synchronized void finish() {
         this. = false;
         this. = System.currentTimeMillis();
     }
 
     public boolean isAccessed() {
         return this.;
     }

    
Append new bytes to buffer.

Parameters:
data new transfer buffer
len length in buffer
count whether to return the count
Returns:
number of messages that was sent to callback (or -1 if count == false)
Throws:
java.io.IOException
See also:
XByteBuffer.countPackages()
 
     public int append(ByteBuffer dataint lenboolean countthrows java.io.IOException {
        .append(data,len);
        int pkgCnt = -1;
        if ( count ) pkgCnt = .countPackages();
       return pkgCnt;
   }
     public int append(byte[] data,int off,int lenboolean count) {
        .append(data,off,len);
        int pkgCnt = -1;
        if ( count ) pkgCnt = .countPackages();
        return pkgCnt;
    }

    
Send buffer to cluster listener (callback). Is message complete receiver send message to callback?

    public ChannelMessage[] execute() throws java.io.IOException {
        int pkgCnt = .countPackages();
        ChannelMessage[] result = new ChannelMessage[pkgCnt];
        for (int i=0; i<pkgCnti++)  {
            ChannelMessage data = .extractPackage(true);
            result[i] = data;
        }
        return result;
    }
    public int bufferSize() {
        return .getLength();
    }
    public boolean hasPackage() {
        return .countPackages(true)>0;
    }
    
Returns the number of packages that the reader has read

Returns:
int
    public int count() {
        return .countPackages();
    }
    public void close() {
        this. = null;
    }
    public long getLastAccess() {
        return ;
    }
    public boolean isCancelled() {
        return ;
    }
    public void setLastAccess(long lastAccess) {
        this. = lastAccess;
    }
    public void setCancelled(boolean cancelled) {
        this. = cancelled;
    }
New to GrepCode? Check out our FAQ X