private byte[] toBytes(String str) throws ProtocolCodecException { try { return str.getBytes(charsetEncoding); } catch (UnsupportedEncodingException e) { throw new ProtocolCodecException(e); } }
/** * @return the message and the hexdump of the unknown part. */ @Override public String getMessage() { String message = super.getMessage(); if (message == null) { message = ""; } if (hexdump != null) { return message + (message.length() > 0 ? " " : "") + "(Hexdump: " + hexdump + ')'; } return message; }
private byte[] toBytes(String str) throws ProtocolCodecException { try { return str.getBytes(charsetEncoding); } catch (UnsupportedEncodingException e) { throw new ProtocolCodecException(e); } }
/** * @return the message and the hexdump of the unknown part. */ @Override public String getMessage() { String message = super.getMessage(); if (message == null) { message = ""; } if (hexdump != null) { return message + (message.length() > 0 ? " " : "") + "(Hexdump: " + hexdump + ')'; } return message; }
private byte[] toBytes(String str) throws ProtocolCodecException { try { return str.getBytes(charsetEncoding); } catch (UnsupportedEncodingException e) { throw new ProtocolCodecException(e); } }
/** * Returns the message and the hexdump of the unknown part. */ @Override public String getMessage() { String message = super.getMessage(); if (message == null) { message = ""; } if (hexdump != null) { return message + (message.length() > 0 ? " " : "") + "(Hexdump: " + hexdump + ')'; } return message; }
private byte checkType ( final IoBuffer buffer, final byte expectedType, final boolean allowNull ) throws Exception { final byte type = buffer.get (); if ( allowNull && type == TYPE_NULL ) { return type; } if ( type != expectedType ) { if ( type == 0 && !allowNull ) { throw new ProtocolCodecException ( String.format ( "Failed to decode. Field is transmitted as null but defined as not-null." ) ); } throw new ProtocolCodecException ( String.format ( "Failed to decode string: Expected type %02x, found: %02x", expectedType, type ) ); } return type; }
/** * Returns the message and the hexdump of the unknown part. */ public String getMessage() { String message = super.getMessage(); if( message == null ) { message = ""; } if( hexdump != null ) { return message + ( ( message.length() > 0 ) ? " " : "" ) + "(Hexdump: " + hexdump + ')'; } else { return message; } }
private byte checkType ( final IoBuffer buffer, final byte expectedType, final boolean allowNull ) throws Exception { final byte type = buffer.get (); if ( allowNull && type == TYPE_NULL ) { return type; } if ( type != expectedType ) { if ( type == 0 && !allowNull ) { throw new ProtocolCodecException ( String.format ( "Failed to decode. Field is transmitted as null but defined as not-null." ) ); } throw new ProtocolCodecException ( String.format ( "Failed to decode string: Expected type %02x, found: %02x", expectedType, type ) ); } return type; }
private void encodeProperties ( final IoBuffer data, final Map<String, String> properties ) throws ProtocolCodecException { final CharsetEncoder encoder = this.defaultCharset.newEncoder (); data.putUnsignedShort ( properties.size () ); for ( final Map.Entry<String, String> entry : properties.entrySet () ) { try { data.putPrefixedString ( entry.getKey (), encoder ); data.putPrefixedString ( entry.getValue (), encoder ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } } }
@Override public void decode ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput output ) throws Exception { final short magic = data.getShort (); final byte version = data.get (); final int sequence = data.getInt (); final byte commandCode = data.get (); if ( magic != 1202 ) { throw new ProtocolCodecException ( String.format ( "Magic code should be 1202 but is %s", magic ) ); } if ( version != 1 ) { throw new ProtocolCodecException ( String.format ( "Version should be %s but is %s", 1, version ) ); } decodeMessage ( sequence, commandCode, data, output ); }
@Override public void decode ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput output ) throws Exception { final short magic = data.getShort (); final byte version = data.get (); final int sequence = data.getInt (); final byte commandCode = data.get (); if ( magic != 1202 ) { throw new ProtocolCodecException ( String.format ( "Magic code should be 1202 but is %s", magic ) ); } if ( version != 1 ) { throw new ProtocolCodecException ( String.format ( "Version should be %s but is %s", 1, version ) ); } decodeMessage ( sequence, commandCode, data, output ); }
logger.debug(CLASS_NAME + s); throw new ProtocolCodecException("Invalid AMQP 0.9.1 Protocol Header"); throw new ProtocolCodecException(s);
throw new ProtocolCodecException ( e );
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { // get message bytes byte[] bytes; if (message instanceof String) { bytes = toBytes((String) message); } else if (message instanceof Message) { bytes = toBytes(message.toString()); } else { throw new ProtocolCodecException("Invalid FIX message object type: " + message.getClass()); } // write bytes to buffer and output it IoBuffer buffer = IoBuffer.allocate(bytes.length); buffer.put(bytes); buffer.flip(); out.write(buffer); } }
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { // get message bytes byte[] bytes; if (message instanceof String) { bytes = toBytes((String) message); } else if (message instanceof Message) { bytes = toBytes(message.toString()); } else { throw new ProtocolCodecException("Invalid FIX message object type: " + message.getClass()); } // write bytes to buffer and output it IoBuffer buffer = IoBuffer.allocate(bytes.length); buffer.put(bytes); buffer.flip(); out.write(buffer); } }
@Override public void encode(IoSession session, Object message, ProtocolEncoderOutput out) throws ProtocolCodecException { // get message bytes byte[] bytes; if (message instanceof String) { bytes = toBytes((String) message); } else if (message instanceof Message) { bytes = toBytes(message.toString()); } else { throw new ProtocolCodecException("Invalid FIX message object type: " + message.getClass()); } // write bytes to buffer and output it IoBuffer buffer = IoBuffer.allocate(bytes.length); buffer.put(bytes); buffer.flip(); out.write(buffer); } }
private boolean processHello ( final IoSession session, final IoBuffer data, final ProtocolDecoderOutput out ) throws ProtocolCodecException { final int len = messageLength ( data ); if ( len < 0 ) { return false; } final byte version = data.get (); if ( version != 0x01 ) { throw new ProtocolCodecException ( String.format ( "Protocol version %s is unsupported", version ) ); } final short nodeId = data.getShort (); final EnumSet<Hello.Features> features = data.getEnumSetShort ( Hello.Features.class ); out.write ( new Hello ( nodeId, features ) ); return true; }
private BrowseAdded.Entry decodeBrowserAddEntry ( final IoBuffer data, final IoSession session ) throws ProtocolCodecException { final short register = (short)data.getUnsignedShort (); // FIXME: validate if short works final byte b = data.get (); final DataType dataType = DataType.fromByte ( b ); if ( dataType == null ) { throw new ProtocolCodecException ( String.format ( "Data type %s is unkown", b ) ); } final Set<BrowseAdded.Entry.Flags> flags = data.getEnumSet ( BrowseAdded.Entry.Flags.class ); final CharsetDecoder decoder = Sessions.getCharsetDecoder ( session ); try { final String name = data.getPrefixedString ( decoder ); final String description = data.getPrefixedString ( decoder ); final String unit = data.getPrefixedString ( decoder ); return new BrowseAdded.Entry ( register, name, description, unit, dataType, flags ); } catch ( final CharacterCodingException e ) { throw new ProtocolCodecException ( e ); } }
private Object decodeData ( final IoBuffer data ) throws ProtocolCodecException { data.order ( ByteOrder.LITTLE_ENDIAN ); final byte dataType = data.get (); switch ( dataType ) { case 0: return null; case 1: return data.get () != 0x00; case 2: return data.getInt (); case 3: return data.getLong (); case 4: return data.getFloat (); default: throw new ProtocolCodecException ( String.format ( "Data type %02x is unknown", dataType ) ); } }