/* * 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 * <code>XByteBuffer</code> 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.
*/ publicclass ObjectReader {
privatestaticfinal Log log = LogFactory.getLog(ObjectReader.class); protectedstaticfinal StringManager sm = StringManager.getManager(ObjectReader.class);
public ObjectReader(int packetSize) { this.buffer = new XByteBuffer(packetSize, true);
} /** * Creates an <code>ObjectReader</code> for a TCP NIO socket channel * @param channel - the channel to be read.
*/ public ObjectReader(SocketChannel channel) { this(channel.socket());
}
/** * Creates an <code>ObjectReader</code> for a TCP socket * @param socket Socket
*/ public ObjectReader(Socket socket) { try{ this.buffer = new XByteBuffer(socket.getReceiveBufferSize(), true);
}catch ( IOException x ) { //unable to get buffer size
log.warn(sm.getString("objectReader.retrieveFailed.socketReceiverBufferSize",
Integer.toString(Constants.DEFAULT_CLUSTER_MSG_BUFFER_SIZE))); this.buffer = new XByteBuffer(Constants.DEFAULT_CLUSTER_MSG_BUFFER_SIZE, true);
}
}
/** * Append new bytes to buffer. * @see XByteBuffer#countPackages() * @param data new transfer buffer * @param len length in buffer * @param count whether to return the count * @return number of messages that was sent to callback (or -1 if count == false)
*/ publicint append(ByteBuffer data, int len, boolean count) {
buffer.append(data,len); int pkgCnt = -1; if ( count ) {
pkgCnt = buffer.countPackages();
} return pkgCnt;
}
publicboolean hasPackage() { return buffer.countPackages(true)>0;
} /** * Returns the number of packages that the reader has read * @return int
*/ publicint count() { return buffer.countPackages();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.