Java Code Examples for org.apache.mina.core.buffer.IoBuffer#getInt()
The following examples show how to use
org.apache.mina.core.buffer.IoBuffer#getInt() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BinaryProtocol.java From neoscada with Eclipse Public License 1.0 | 6 votes |
@Override public Object decodeMessage ( final DataMessage message ) throws Exception { final IoBuffer data = message.getData (); // we only peek final int messageCode = data.getInt ( 0 ); logger.trace ( "Decoding message: {}", messageCode ); final BinaryMessageCodec codec = this.codeMap.get ( messageCode ); if ( codec == null ) { throw new IllegalStateException ( String.format ( "Unable to decode unknown message code: 0x%08x", messageCode ) ); } return codec.decodeMessage ( this.context, data ); }
Example 2
Source File: MinaCodecAdapter.java From JobX with Apache License 2.0 | 6 votes |
@Override public boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { if (in.limit() <= 0 || in.remaining() < Constants.HEADER_SIZE) { return false; } in.mark(); int dataLength = in.getInt(); if (in.remaining() < dataLength) { //logger.warn("[JobX]serializer error!body length < {}", dataLength); in.reset(); return false; } byte[] data = new byte[dataLength]; in.get(data); Object obj = serializer.deserialize(data,type); out.write(obj); return true; }
Example 3
Source File: ProtocolDecoderImpl.java From neoscada with Eclipse Public License 1.0 | 6 votes |
private boolean processWriteResult ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException { final int len = messageLength ( data ); if ( len < 0 ) { return false; } try { final int operationId = data.getInt (); final int errorCode = data.getUnsignedShort (); final String errorMessage = decodeString ( session, data ); out.write ( new WriteResult ( operationId, errorCode, errorMessage ) ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } return true; }
Example 4
Source File: ProtocolDecoderImpl.java From neoscada with Eclipse Public License 1.0 | 6 votes |
private boolean processWriteCommand ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException { final int len = messageLength ( data ); if ( len < 0 ) { return false; } final int registerNumber = data.getUnsignedShort (); final int operationId = data.getInt (); final Variant value = decodeVariant ( session, data ); out.write ( new WriteCommand ( registerNumber, value, operationId ) ); return true; }
Example 5
Source File: DefaultBinaryContext.java From neoscada with Eclipse Public License 1.0 | 6 votes |
private <T extends Collection<Double>> T decodeDoubleCollection ( final IoBuffer buffer, final CollectionAllocator<Double, T> allactor ) throws Exception { final byte type = checkType ( buffer, TYPE_DOUBLE_LIST, true ); if ( type == TYPE_NULL ) { return null; } else { final int items = buffer.getInt (); final T result = allactor.allocate ( items ); fillDoubleCollection ( buffer, items, result ); return result; } }
Example 6
Source File: ByteArrayFieldTypeHandler.java From CXTouch with GNU General Public License v3.0 | 6 votes |
@Override public byte[] decode(IoBuffer inBuffer) { if (inBuffer.remaining() < 4) { return null; } inBuffer.mark(); int length = inBuffer.getInt(); if (inBuffer.remaining() < length) { inBuffer.reset(); return null; } byte[] bytes = new byte[length]; inBuffer.get(bytes); return bytes; }
Example 7
Source File: TestTupleDecoder.java From streamsx.topology with Apache License 2.0 | 5 votes |
/** * Decode bytes an attribute at a time, once enough information exists to * maintain a tuple This code maintains state in the session as attributes, * namely: * <UL> * <LI>tuple - The partially initialized tuple to be sent to the next * handler in the chain. * <LI>attributeIndex - The next attribute to be decoded * </UL> */ @Override protected boolean doDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws Exception { Integer testerId = null; if (!session.containsAttribute("testerId")) { if (in.remaining() < 4) return false; testerId = in.getInt(); } if (!in.prefixedDataAvailable(4)) { if (testerId != null) session.setAttribute("testerId", testerId); return false; } if (testerId == null) { testerId = (Integer) session.removeAttribute("testerId"); } int tupleLength = in.getInt(); byte[] tupleData = new byte[tupleLength]; in.get(tupleData); out.write(new TestTuple(testerId, tupleData)); return in.remaining() >= 4; }
Example 8
Source File: EventPoolStatusUpdate.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.ae.data.message.EventPoolStatusUpdate decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes String eventPoolId = null; org.eclipse.scada.core.data.SubscriptionState state = null; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { eventPoolId = _context.decodeString ( _data ); } break; case 2: { state = _context.decodeEnum ( _data, org.eclipse.scada.core.data.SubscriptionState.class ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.ae.data.message.EventPoolStatusUpdate ( eventPoolId, state ); }
Example 9
Source File: AcknowledgeResponse.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.ae.data.message.AcknowledgeResponse decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes org.eclipse.scada.core.data.Response response = null; org.eclipse.scada.core.data.ErrorInformation errorInformation = null; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { response = org.eclipse.scada.core.protocol.ngp.codec.Structures.decodeResponse ( _context, _data, false ); } break; case 2: { errorInformation = org.eclipse.scada.core.protocol.ngp.codec.Structures.decodeErrorInformation ( _context, _data, true ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.ae.data.message.AcknowledgeResponse ( response, errorInformation ); }
Example 10
Source File: GetFactoryWithDataRequest.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.ca.data.message.GetFactoryWithDataRequest decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes org.eclipse.scada.core.data.Request request = null; String factoryId = null; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { request = org.eclipse.scada.core.protocol.ngp.codec.Structures.decodeRequest ( _context, _data, false ); } break; case 2: { factoryId = _context.decodeString ( _data ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.ca.data.message.GetFactoryWithDataRequest ( request, factoryId ); }
Example 11
Source File: ApplyDiffResponse.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.ca.data.message.ApplyDiffResponse decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes org.eclipse.scada.core.data.Response response = null; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { response = org.eclipse.scada.core.protocol.ngp.codec.Structures.decodeResponse ( _context, _data, false ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.ca.data.message.ApplyDiffResponse ( response ); }
Example 12
Source File: UnsubscribeEventPool.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.ae.data.message.UnsubscribeEventPool decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes String eventPoolId = null; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { eventPoolId = _context.decodeString ( _data ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.ae.data.message.UnsubscribeEventPool ( eventPoolId ); }
Example 13
Source File: SumkProtocolDecoder.java From sumk with Apache License 2.0 | 4 votes |
protected boolean innerDecode(IoSession session, IoBuffer in, ProtocolDecoderOutput out) throws CharacterCodingException, ProtocolDecoderException { int protocol = in.getInt(); if ((protocol & 0xFF000000) != Protocols.MAGIC) { if (Logs.rpc().isTraceEnabled()) { Logs.rpc().trace(in.getString(Profile.UTF8.newDecoder())); } throw new ProtocolDecoderException("error magic," + Integer.toHexString(protocol)); } int prefixLength = 0, maxDataLength = 0; if ((protocol & Protocols.ONE) != 0) { prefixLength = 1; maxDataLength = 0xFF; } else if ((protocol & Protocols.TWO) != 0) { prefixLength = 2; maxDataLength = 0xFFFF; } else if ((protocol & Protocols.FOUR) != 0) { prefixLength = 4; maxDataLength = Protocols.MAX_LENGTH; } else { if (AppInfo.getBoolean("sumk.rpc.log.code.error", true)) { Logs.rpc().error("error byte length protocol," + Integer.toHexString(protocol)); } throw new ProtocolDecoderException("error byte length protocol," + Integer.toHexString(protocol)); } if (in.remaining() < prefixLength) { return false; } int dataSize = 0; switch (prefixLength) { case 1: dataSize = in.getUnsigned(); break; case 2: dataSize = in.getUnsignedShort(); break; case 4: dataSize = in.getInt(); break; } if (dataSize < 0 || dataSize > maxDataLength) { throw new BufferDataException("dataLength: " + dataSize); } if (in.remaining() < dataSize) { return false; } byte[] bs = new byte[dataSize]; in.get(bs); out.write(new ProtocolObject(protocol, bs)); return true; }
Example 14
Source File: ErrorResponse.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.ca.data.message.ErrorResponse decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes org.eclipse.scada.core.data.Response response = null; org.eclipse.scada.core.data.ErrorInformation errorInformation = null; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { response = org.eclipse.scada.core.protocol.ngp.codec.Structures.decodeResponse ( _context, _data, false ); } break; case 2: { errorInformation = org.eclipse.scada.core.protocol.ngp.codec.Structures.decodeErrorInformation ( _context, _data, false ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.ca.data.message.ErrorResponse ( response, errorInformation ); }
Example 15
Source File: ProtobufDecoder.java From gameserver with Apache License 2.0 | 4 votes |
/** * Decode the XinqiMessage from byte array. * @param in * @return * @throws InvalidProtocolBufferException */ public static final XinqiMessage decodeXinqiMessage(IoBuffer in) throws InvalidProtocolBufferException { // Make sure all the header bytes are ready. if ( !in.prefixedDataAvailable(HEADER_LENGTH, MAX_LENGTH) ) { return null; } int length = in.getInt() - 6; int type = in.getShort(); XinqiMessage message = new XinqiMessage(); //XinqiMessage message.type = type; message.index = in.getInt(); byte[] array = new byte[length]; in.get(array); if ( log.isDebugEnabled() ) { log.debug("length:"+length+", type:"+message.type+", index:"+message.index); } MessageLite request = IdToMessage.idToMessage(message.type); if ( request == null ) { if ( log.isWarnEnabled() ) { log.warn("No id found for message type. return empty message. "); } //Note: return an empty message rather than null so that //next messages can be parsed. return message; } request = request.newBuilderForType().mergeFrom(array).build(); // in.skip(length); if ( log.isDebugEnabled() ) { log.debug("DecodedMessage:"+request.getClass().getName()+"[\n"+request+"]"); } message.payload = request; if ( log.isDebugEnabled() ) { log.debug("XinqiMessage:[\n"+message.toString()+"]"); } return message; }
Example 16
Source File: ListUpdate.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.hd.data.message.ListUpdate decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes java.util.Set<org.eclipse.scada.hd.data.HistoricalItemInformation> addedOrModified = null; java.util.Set<String> removed = null; boolean fullUpdate = false; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { addedOrModified = org.eclipse.scada.hd.protocol.ngp.codec.Structures.decodeSetHistoricalItemInformation ( _context, _data, true ); } break; case 2: { removed = _context.decodeStringSet ( _data ); } break; case 3: { fullUpdate = _context.decodePrimitiveBoolean ( _data ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.hd.data.message.ListUpdate ( addedOrModified, removed, fullUpdate ); }
Example 17
Source File: FrameDecoder.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override protected boolean doDecode ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput output ) throws Exception { logger.trace ( "decode data - session: {}, data: {}", session, data ); if ( data.remaining () < HEADER_SIZE ) { return false; } final int position = data.position (); final byte version = data.get ( position ); // peek at version if ( version != 0x01 ) { throw new IllegalStateException ( String.format ( "Version 0x%02x is not supported.", version ) ); } final int frameTypeOrdinal = data.get ( position + 1 ); // peek at frame type final FrameType frameType = FrameType.values ()[frameTypeOrdinal]; // may case an exception, that is ok then final int dataLength = data.getInt ( position + 2 ); // we need to look at "here" + 2 logger.trace ( "Data length: {}, remainingData: {}", dataLength, data.remaining () - 6 ); if ( data.remaining () < HEADER_SIZE + dataLength ) { return false; } // consume fields data.get (); // version - #0 data.get (); // frame type - #1 data.getInt (); // dataLength - #2 // data - #6 final IoBuffer frameData = data.getSlice ( dataLength ); // this also consumes the buffer 'data' final Frame frame = new Frame ( frameType, frameData ); logger.trace ( "Decoded frame: {} ... {} bytes remaining", frame, data.remaining () ); output.write ( frame ); return true; }
Example 18
Source File: SubscribeFolder.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.da.data.message.SubscribeFolder decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes java.util.List<String> location = null; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { location = _context.decodeStringList ( _data ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.da.data.message.SubscribeFolder ( location ); }
Example 19
Source File: MessageUtil.java From CXTouch with GNU General Public License v3.0 | 4 votes |
public static String readString(IoBuffer buffer, LengthType lenType, CharsetDecoder decoder) throws NoArrayException { int length; int remain = buffer.remaining(); buffer.mark(); if (lenType == LengthType.INT) { if (remain < 4) { buffer.reset(); throw new NoArrayException(); } length = buffer.getInt(); } else if(lenType == LengthType.SHORT) { if (remain < 2) { buffer.reset(); throw new NoArrayException(); } length = buffer.getShort(); } else if (lenType == LengthType.BYTE) { if (remain < 1) { buffer.reset(); throw new NoArrayException(); } length = buffer.get(); } else { buffer.reset(); throw new IllegalArgumentException("Illegal type:" + lenType); } if (length < 0) { return null; } if (length == 0) { return ""; } else { if (buffer.remaining() < length) { buffer.reset(); throw new NoArrayException(); } try { return buffer.getString(length, decoder); } catch (CharacterCodingException e) { throw new RuntimeException("Reading string failed:" + e.getMessage(), e); } } }
Example 20
Source File: LoadMore.java From neoscada with Eclipse Public License 1.0 | 4 votes |
@Override public org.eclipse.scada.ae.data.message.LoadMore decodeMessage ( final BinaryContext _context, final IoBuffer _data ) throws Exception { // message code { final int messageCode = _data.getInt (); if ( messageCode != MESSAGE_CODE ) { throw new IllegalStateException ( String.format ( "Expected messageCode %s but found %s", MESSAGE_CODE, messageCode ) ); } } final byte numberOfFields = _data.get (); // decode attributes long queryId = 0L; int count = 0; logger.trace ( "Decoding {} fields", numberOfFields ); for ( int i = 0; i < numberOfFields; i++ ) { final byte fieldNumber = _data.get (); switch ( fieldNumber ) { case 1: { queryId = _context.decodePrimitiveLong ( _data ); } break; case 2: { count = _context.decodePrimitiveInt ( _data ); } break; default: logger.warn ( "Received unknown field number: {}", fieldNumber ); break; } } // create object return new org.eclipse.scada.ae.data.message.LoadMore ( queryId, count ); }