本文整理匯總了Java中org.apache.mina.core.buffer.IoBuffer.limit方法的典型用法代碼示例。如果您正苦於以下問題:Java IoBuffer.limit方法的具體用法?Java IoBuffer.limit怎麽用?Java IoBuffer.limit使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.mina.core.buffer.IoBuffer
的用法示例。
在下文中一共展示了IoBuffer.limit方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: write
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Writes <code>data</code> {@link IoBuffer} to the <code>buf</code>
* {@link IoBuffer} which buffers write requests for the
* <code>session</code> {@ link IoSession} until buffer is full
* or manually flushed.
*
* @param session the session where buffer will be written
* @param data the data to buffer
* @param buf the buffer where data will be temporarily written
*/
private void write(IoSession session, IoBuffer data, IoBuffer buf) {
try {
int len = data.remaining();
if (len >= buf.capacity()) {
/*
* If the request length exceeds the size of the output buffer,
* flush the output buffer and then write the data directly.
*/
NextFilter nextFilter = session.getFilterChain().getNextFilter(this);
internalFlush(nextFilter, session, buf);
nextFilter.filterWrite(session, new DefaultWriteRequest(data));
return;
}
if (len > (buf.limit() - buf.position())) {
internalFlush(session.getFilterChain().getNextFilter(this), session, buf);
}
synchronized (buf) {
buf.put(data);
}
} catch (Throwable e) {
session.getFilterChain().fireExceptionCaught(e);
}
}
示例2: readObject
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
public Object readObject() throws ClassNotFoundException, IOException {
int objectSize = in.readInt();
if (objectSize <= 0) {
throw new StreamCorruptedException("Invalid objectSize: " + objectSize);
}
if (objectSize > maxObjectSize) {
throw new StreamCorruptedException("ObjectSize too big: " + objectSize + " (expected: <= " + maxObjectSize
+ ')');
}
IoBuffer buf = IoBuffer.allocate(objectSize + 4, false);
buf.putInt(objectSize);
in.readFully(buf.array(), 4, objectSize);
buf.position(0);
buf.limit(objectSize + 4);
return buf.getObject(classLoader);
}
示例3: decode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
int beginPos = in.position();
int limit = in.limit();
for (int i = beginPos; i < limit; i++) {
byte b = in.get(i);
if (!canSkip(b)) {
in.position(i);
int answer = this.skippedBytes;
this.skippedBytes = 0;
return finishDecode(answer);
}
skippedBytes++;
}
in.position(limit);
return this;
}
示例4: decodeTimeoutFrame
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
private void decodeTimeoutFrame ( final IoSession session, final IoBuffer currentFrame, final ProtocolDecoderOutput out )
{
logger.trace ( "timeout () frame = {}", currentFrame.getHexDump () );
if ( currentFrame.limit () <= Constants.RTU_HEADER_SIZE )
{
throw new ModbusProtocolError ( "frame must be at least 4 bytes long (address + data[] + crc low + crc high" );
}
currentFrame.order ( ByteOrder.LITTLE_ENDIAN );
final int receivedCrc = currentFrame.getUnsignedShort ( currentFrame.limit () - 2 );
currentFrame.order ( ByteOrder.BIG_ENDIAN );
final int actualCrc = Checksum.crc16 ( currentFrame.array (), 0, currentFrame.limit () - 2 );
if ( receivedCrc != actualCrc )
{
final String hd = currentFrame.getHexDump ();
logger.info ( "CRC error - received: {}, calculated: {} - data: {}", receivedCrc, actualCrc, hd );
throw new ChecksumProtocolException ( String.format ( "CRC error. received: %04x, but actually was: %04x - Data: %s", receivedCrc, actualCrc, hd ) );
}
currentFrame.position ( 0 );
// get unit id
final byte unitIdentifier = currentFrame.get ();
final int len = currentFrame.limit () - ( 2 /*crc*/+ 1/*unit id*/);
final IoBuffer pdu = IoBuffer.allocate ( len );
for ( int i = 0; i < len; i++ )
{
pdu.put ( currentFrame.get () );
}
pdu.flip ();
// decode and send
logger.trace ( "Decoded PDU - data: {}", pdu.getHexDump () );
out.write ( new Pdu ( 0, unitIdentifier, pdu ) );
}
示例5: write
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
protected int write(NioSession session, IoBuffer buf, int length) throws Exception {
if (buf.remaining() <= length) {
return session.getChannel().write(buf.buf());
}
int oldLimit = buf.limit();
buf.limit(buf.position() + length);
try {
return session.getChannel().write(buf.buf());
} finally {
buf.limit(oldLimit);
}
}
示例6: doDecode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
@Override
protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception {
byte decryptedPacket[] = new byte[in.limit()];
in.get(decryptedPacket, 0, in.limit());
out.write(decryptedPacket);
return true;
}
示例7: removeTo
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Remove component <code>ByteArray</code>s to the given index (splitting
* them if necessary) and returning them in a single <code>ByteArray</code>.
* The caller is responsible for freeing the returned object.
*
* TODO: Document free behaviour more thoroughly.
*/
public ByteArray removeTo(int index) {
if (index < first() || index > last()) {
throw new IndexOutOfBoundsException();
}
// Optimisation when removing exactly one component.
// if (index == start() + getFirst().length()) {
// ByteArray component = getFirst();
// removeFirst();
// return component;
// }
// Removing
CompositeByteArray prefix = new CompositeByteArray(byteArrayFactory);
int remaining = index - first();
while (remaining > 0) {
ByteArray component = removeFirst();
if (component.last() <= remaining) {
// Remove entire component.
prefix.addLast(component);
remaining -= component.last();
} else {
// Remove part of component. Do this by removing entire
// component then readding remaining bytes.
// TODO: Consider using getIoBuffers(), as would avoid
// performance problems for nested ComponentByteArrays.
IoBuffer bb = component.getSingleIoBuffer();
// get the limit of the buffer
int originalLimit = bb.limit();
// set the position to the beginning of the buffer
bb.position(0);
// set the limit of the buffer to what is remaining
bb.limit(remaining);
// create a new IoBuffer, sharing the data with 'bb'
IoBuffer bb1 = bb.slice();
// set the position at the end of the buffer
bb.position(remaining);
// gets the limit of the buffer
bb.limit(originalLimit);
// create a new IoBuffer, sharing teh data with 'bb'
IoBuffer bb2 = bb.slice();
// create a new ByteArray with 'bb1'
ByteArray ba1 = new BufferByteArray(bb1) {
@Override
public void free() {
// Do not free. This will get freed
}
};
// add the new ByteArray to the CompositeByteArray
prefix.addLast(ba1);
remaining -= ba1.last();
// final for anonymous inner class
final ByteArray componentFinal = component;
ByteArray ba2 = new BufferByteArray(bb2) {
@Override
public void free() {
componentFinal.free();
}
};
// add the new ByteArray to the CompositeByteArray
addFirst(ba2);
}
}
// return the CompositeByteArray
return prefix;
}
示例8: decodeAuto
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Decode a line using the default delimiter on the current system
*/
private void decodeAuto(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws CharacterCodingException, ProtocolDecoderException {
int matchCount = ctx.getMatchCount();
// Try to find a match
int oldPos = in.position();
int oldLimit = in.limit();
while (in.hasRemaining()) {
byte b = in.get();
boolean matched = false;
switch (b) {
case '\r':
// Might be Mac, but we don't auto-detect Mac EOL
// to avoid confusion.
matchCount++;
break;
case '\n':
// UNIX
matchCount++;
matched = true;
break;
default:
matchCount = 0;
}
if (matched) {
// Found a match.
int pos = in.position();
in.limit(pos);
in.position(oldPos);
ctx.append(in);
in.limit(oldLimit);
in.position(pos);
if (ctx.getOverflowPosition() == 0) {
IoBuffer buf = ctx.getBuffer();
buf.flip();
buf.limit(buf.limit() - matchCount);
try {
byte[] data = new byte[buf.limit()];
buf.get(data);
CharsetDecoder decoder = ctx.getDecoder();
CharBuffer buffer = decoder.decode(ByteBuffer.wrap(data));
String str = new String(buffer.array());
writeText(session, str, out);
} finally {
buf.clear();
}
} else {
int overflowPosition = ctx.getOverflowPosition();
ctx.reset();
throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
}
oldPos = pos;
matchCount = 0;
}
}
// Put remainder to buf.
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
}
示例9: decodeNormal
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* Decode a line using the delimiter defined by the caller
*/
private void decodeNormal(Context ctx, IoSession session, IoBuffer in, ProtocolDecoderOutput out)
throws CharacterCodingException, ProtocolDecoderException {
int matchCount = ctx.getMatchCount();
// Try to find a match
int oldPos = in.position();
int oldLimit = in.limit();
while (in.hasRemaining()) {
byte b = in.get();
if (delimBuf.get(matchCount) == b) {
matchCount++;
if (matchCount == delimBuf.limit()) {
// Found a match.
int pos = in.position();
in.limit(pos);
in.position(oldPos);
ctx.append(in);
in.limit(oldLimit);
in.position(pos);
if (ctx.getOverflowPosition() == 0) {
IoBuffer buf = ctx.getBuffer();
buf.flip();
buf.limit(buf.limit() - matchCount);
try {
writeText(session, buf.getString(ctx.getDecoder()), out);
} finally {
buf.clear();
}
} else {
int overflowPosition = ctx.getOverflowPosition();
ctx.reset();
throw new RecoverableProtocolDecoderException("Line is too long: " + overflowPosition);
}
oldPos = pos;
matchCount = 0;
}
} else {
// fix for DIRMINA-506 & DIRMINA-536
in.position(Math.max(0, in.position() - matchCount));
matchCount = 0;
}
}
// Put remainder to buf.
in.position(oldPos);
ctx.append(in);
ctx.setMatchCount(matchCount);
}
示例10: decode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
int beginPos = in.position();
int limit = in.limit();
int terminatorPos = -1;
for (int i = beginPos; i < limit; i++) {
byte b = in.get(i);
if (b == CR) {
lastIsCR = true;
} else {
if (b == LF && lastIsCR) {
terminatorPos = i;
break;
}
lastIsCR = false;
}
}
if (terminatorPos >= 0) {
IoBuffer product;
int endPos = terminatorPos - 1;
if (beginPos < endPos) {
in.limit(endPos);
if (buffer == null) {
product = in.slice();
} else {
buffer.put(in);
product = buffer.flip();
buffer = null;
}
in.limit(limit);
} else {
// When input contained only CR or LF rather than actual data...
if (buffer == null) {
product = IoBuffer.allocate(0);
} else {
product = buffer.flip();
buffer = null;
}
}
in.position(terminatorPos + 1);
return finishDecode(product, out);
}
in.position(beginPos);
if (buffer == null) {
buffer = IoBuffer.allocate(in.remaining());
buffer.setAutoExpand(true);
}
buffer.put(in);
if (lastIsCR) {
buffer.position(buffer.position() - 1);
}
return this;
}
示例11: decode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
int terminatorPos = in.indexOf(terminator);
if (terminatorPos >= 0) {
int limit = in.limit();
IoBuffer product;
if (in.position() < terminatorPos) {
in.limit(terminatorPos);
if (buffer == null) {
product = in.slice();
} else {
buffer.put(in);
product = buffer.flip();
buffer = null;
}
in.limit(limit);
} else {
// When input contained only terminator rather than actual data...
if (buffer == null) {
product = IoBuffer.allocate(0);
} else {
product = buffer.flip();
buffer = null;
}
}
in.position(terminatorPos + 1);
return finishDecode(product, out);
}
if (buffer == null) {
buffer = IoBuffer.allocate(in.remaining());
buffer.setAutoExpand(true);
}
buffer.put(in);
return this;
}
示例12: decode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
DecodingState state = getCurrentState();
final int limit = in.limit();
int pos = in.position();
try {
for (;;) {
// Wait for more data if all data is consumed.
if (pos == limit) {
break;
}
DecodingState oldState = state;
state = state.decode(in, childOutput);
// If finished, call finishDecode
if (state == null) {
return finishDecode(childProducts, out);
}
int newPos = in.position();
// Wait for more data if nothing is consumed and state didn't change.
if (newPos == pos && oldState == state) {
break;
}
pos = newPos;
}
return this;
} catch (Exception e) {
state = null;
throw e;
} finally {
this.currentState = state;
// Destroy if decoding is finished or failed.
if (state == null) {
cleanup();
}
}
}
示例13: decode
import org.apache.mina.core.buffer.IoBuffer; //導入方法依賴的package包/類
/**
* {@inheritDoc}
*/
public DecodingState decode(IoBuffer in, ProtocolDecoderOutput out) throws Exception {
int beginPos = in.position();
int terminatorPos = -1;
int limit = in.limit();
for (int i = beginPos; i < limit; i++) {
byte b = in.get(i);
if (isTerminator(b)) {
terminatorPos = i;
break;
}
}
if (terminatorPos >= 0) {
IoBuffer product;
if (beginPos < terminatorPos) {
in.limit(terminatorPos);
if (buffer == null) {
product = in.slice();
} else {
buffer.put(in);
product = buffer.flip();
buffer = null;
}
in.limit(limit);
} else {
// When input contained only terminator rather than actual data...
if (buffer == null) {
product = IoBuffer.allocate(0);
} else {
product = buffer.flip();
buffer = null;
}
}
in.position(terminatorPos + 1);
return finishDecode(product, out);
}
if (buffer == null) {
buffer = IoBuffer.allocate(in.remaining());
buffer.setAutoExpand(true);
}
buffer.put(in);
return this;
}