Skip to Content.
Sympa Menu

notify-dpml - svn commit: r1900 - development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl

notify-dpml AT lists.ibiblio.org

Subject: DPML Notify

List archive

Chronological Thread  
  • From: niclas AT netcompartner.com
  • To: notify-dpml AT lists.ibiblio.org
  • Subject: svn commit: r1900 - development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl
  • Date: Fri, 25 Feb 2005 11:39:38 +0100

Author: niclas
Date: Fri Feb 25 11:39:38 2005
New Revision: 1900

Added:

development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/String16bitLengthLead.java
(contents, props changed)

development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/String8bitLengthLead.java
(contents, props changed)

development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/StringNullTerminated.java
(contents, props changed)
Log:
Added some common field types for strings.

Added:
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/String16bitLengthLead.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/String16bitLengthLead.java
Fri Feb 25 11:39:38 2005
@@ -0,0 +1,188 @@
+/*
+ * Copyright 2005, Niclas Hedhman
+ *
+ * Licensed 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 net.dpml.protocol.packet.impl;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+
+import java.util.Locale;
+
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+
+import net.dpml.protocol.packet.FieldType;
+import net.dpml.protocol.packet.IllegalPacketFormatException;
+
+/** Handles strings that are characters stored in a sequence, prefixed
+ * with the first two bytes being the length of the string with low-byte
+ * followed by the high-byte of the length.
+ *
+ * The encoding to use for the byte sequence is defined in the parameter
+ * "char-encoding" and defaults to ISO8859-1.
+ *
+ * @metro.component name="string-16bit-length-prefixed" lifestyle="singleton"
+ * @metro.service type="net.dpml.protocol.packet.FieldType"
+ */
+public class String16bitLengthLead
+ implements FieldType
+{
+ private String m_encoding;
+
+ /** Constructor.
+ * @param params the parameters supplied by the container.
+ */
+ public String16bitLengthLead( Parameters params )
+ throws ParameterException
+ {
+ m_encoding = params.getParameter( "char-encoding", "ISO8859-1" );
+ if( false == Charset.isSupported( m_encoding ) )
+ {
+ throw new ParameterException( "Encoding '" + m_encoding + "' is
not supported on this system." );
+ }
+
+ }
+
+ /** Returns the FieldType name.
+ * This method should return the name of the implementation that
+ * handles the serialization/deserialization of the byte stream.
+ * @return the name in english of the FieldType.
+ */
+ public String getName()
+ {
+ return "String16bitLengthLead";
+ }
+
+ /** Returns a human-readable description of the FieldType.
+ * @return a description of the field type in the locale provided.
+ */
+ public String getDescription( Locale locale )
+ {
+ return "TODO: Not implemented yet.";
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * minimum. Zero-length is not possible, as the field type would then not
+ * be able to deserialize properly. Furher constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ *
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMinimumSize()
+ {
+ return 1;
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * maximum. A constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMaximumSize()
+ {
+ return 65537;
+ }
+
+ /** Query whether this field can be varied in length.
+ * If this method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return true if the field is not a fixed length field.
+ */
+ public boolean isVariableSize()
+ {
+ return true;
+ }
+
+ /** Serialize the content.
+ * @param content the data to be stored in the field.
+ * @return the resulting byte array of the serialized data. This byte
array
+ * must be of the exact size and may not contain any
+ * non-significant bytes at the beginning or end of the array.
+ */
+ public byte[] serialize( String content )
+ throws IllegalPacketFormatException
+ {
+ if( content.length() > 65535 )
+ {
+ throw new IllegalPacketFormatException( "A maximum of 65535
bytes can be stored in this field.", this );
+ }
+ try
+ {
+ byte[] bytes = content.getBytes( m_encoding );
+ byte[] result = new byte[ bytes.length + 2 ];
+ System.arraycopy( bytes, 0, result, 2, bytes.length );
+ result[ 0 ] = (byte) ( bytes.length & 255 );
+ result[ 1 ] = (byte) ( bytes.length / 256 );
+ return result;
+ }
+ catch( UnsupportedEncodingException e )
+ {
+ // Can not happen. Encoding name has been verified in the
+ // constructor.
+ return null;
+ }
+ }
+
+ /** Deserializes the content from the input stream.
+ * @param input the byte[] that contains the content.
+ * @return the deserialized content from the input stream.
+ */
+ public String deserialize( byte[] input )
+ throws IllegalPacketFormatException
+ {
+ int in0 = input[ 0 ];
+ int in1 = input[ 1 ];
+ if( in0 < 0 )
+ {
+ in0 = in0 + 256;
+ }
+ if( in1 < 0 )
+ {
+ in1 = in1 + 256;
+ }
+ int size = in0 + in1 * 256;
+
+ if( size != input.length - 2 )
+ {
+ throw new IllegalPacketFormatException( "Field length (" + (
input.length - 1 ) +") does not match the encoded length (" + size + ").",
this );
+ }
+ try
+ {
+ byte[] data = new byte[ input.length - 2 ];
+ System.arraycopy( input, 2, data, 0, data.length );
+ return new String( data, m_encoding );
+ }
+ catch( UnsupportedEncodingException e )
+ {
+ // Can not happen. Encoding name has been verified in the
+ // constructor.
+ return null;
+ }
+ }
+}
\ No newline at end of file

Added:
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/String8bitLengthLead.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/String8bitLengthLead.java
Fri Feb 25 11:39:38 2005
@@ -0,0 +1,176 @@
+/*
+ * Copyright 2005, Niclas Hedhman
+ *
+ * Licensed 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 net.dpml.protocol.packet.impl;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+
+import java.util.Locale;
+
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+
+import net.dpml.protocol.packet.FieldType;
+import net.dpml.protocol.packet.IllegalPacketFormatException;
+
+/** Handles strings that are 8-bit characters stored in a sequence, prefixed
+ * with the first byte being the length of the string, in the range of
+ * 0 to 255.
+ * The encoding to use for the byte sequence is defined in the parameter
+ * "char-encoding" and defaults to ISO8859-1.
+ *
+ * @metro.component name="zero-terminated-string" lifestyle="singleton"
+ * @metro.service type="net.dpml.protocol.packet.FieldType"
+ */
+public class String8bitLengthLead
+ implements FieldType
+{
+ private String m_encoding;
+
+ public String8bitLengthLead( Parameters params )
+ throws ParameterException
+ {
+ m_encoding = params.getParameter( "char-encoding", "ISO8859-1" );
+ if( false == Charset.isSupported( m_encoding ) )
+ {
+ throw new ParameterException( "Encoding '" + m_encoding + "' is
not supported on this system." );
+ }
+
+ }
+
+ /** Returns the FieldType name.
+ * This method should return the name of the implementation that
+ * handles the serialization/deserialization of the byte stream.
+ * @return the name in english of the FieldType.
+ */
+ public String getName()
+ {
+ return "String8bitLengthLead";
+ }
+
+ /** Returns a human-readable description of the FieldType.
+ * @return a description of the field type in the locale provided.
+ */
+ public String getDescription( Locale locale )
+ {
+ return "TODO: Not implemented yet.";
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * minimum. Zero-length is not possible, as the field type would then not
+ * be able to deserialize properly. Furher constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ *
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMinimumSize()
+ {
+ return 1;
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * maximum. A constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMaximumSize()
+ {
+ return 256;
+ }
+
+ /** Query whether this field can be varied in length.
+ * If this method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return true if the field is not a fixed length field.
+ */
+ public boolean isVariableSize()
+ {
+ return true;
+ }
+
+ /** Serialize the content.
+ * @param content the data to be stored in the field.
+ * @return the resulting byte array of the serialized data. This byte
array
+ * must be of the exact size and may not contain any
+ * non-significant bytes at the beginning or end of the array.
+ */
+ public byte[] serialize( String content )
+ throws IllegalPacketFormatException
+ {
+ if( content.length() > 255 )
+ {
+ throw new IllegalPacketFormatException( "A maximum of 255 bytes
can be stored in this field.", this );
+ }
+ try
+ {
+ byte[] bytes = content.getBytes( m_encoding );
+ byte[] result = new byte[ bytes.length + 1 ];
+ System.arraycopy( bytes, 0, result, 1, bytes.length );
+ result[ 0 ] = (byte) bytes.length;
+ return result;
+ }
+ catch( UnsupportedEncodingException e )
+ {
+ // Can not happen. Encoding name has been verified in the
+ // constructor.
+ return null;
+ }
+ }
+
+ /** Deserializes the content from the input stream.
+ * @param input the byte[] that contains the content.
+ * @return the deserialized content from the input stream.
+ */
+ public String deserialize( byte[] input )
+ throws IllegalPacketFormatException
+ {
+ int size = input[ 0 ];
+ if( size < 0 )
+ {
+ size = size + 256;
+ }
+ if( size != input.length - 1 )
+ {
+ throw new IllegalPacketFormatException( "Field length (" + (
input.length - 1 ) +") does not match the encoded length (" + size + ").",
this );
+ }
+ try
+ {
+ byte[] data = new byte[ input.length - 1 ];
+ System.arraycopy( input, 1, data, 0, data.length );
+ return new String( data, m_encoding );
+ }
+ catch( UnsupportedEncodingException e )
+ {
+ // Can not happen. Encoding name has been verified in the
+ // constructor.
+ return null;
+ }
+ }
+}
\ No newline at end of file

Added:
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/StringNullTerminated.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/StringNullTerminated.java
Fri Feb 25 11:39:38 2005
@@ -0,0 +1,166 @@
+/*
+ * Copyright 2005, Niclas Hedhman
+ *
+ * Licensed 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 net.dpml.protocol.packet.impl;
+
+import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
+
+import java.util.Locale;
+
+import net.dpml.parameters.ParameterException;
+import net.dpml.parameters.Parameters;
+
+import net.dpml.protocol.packet.FieldType;
+import net.dpml.protocol.packet.IllegalPacketFormatException;
+
+/** Handles strings that are 8-bit characters stored in a sequence, end
+ * terminated by a so called NUL character, i.e. 0x00.
+ * The encoding to use for the byte sequence is defined in the parameter
+ * "char-encoding" and defaults to ISO8859-1.
+ *
+ * @metro.component name="zero-terminated-string" lifestyle="singleton"
+ * @metro.service type="net.dpml.protocol.packet.FieldType"
+ */
+public class StringNullTerminated
+ implements FieldType
+{
+ private String m_encoding;
+
+ public StringNullTerminated( Parameters params )
+ throws ParameterException
+ {
+ m_encoding = params.getParameter( "char-encoding", "ISO8859-1" );
+ if( false == Charset.isSupported( m_encoding ) )
+ {
+ throw new ParameterException( "Encoding '" + m_encoding + "' is
not supported on this system." );
+ }
+
+ }
+
+ /** Returns the FieldType name.
+ * This method should return the name of the implementation that
+ * handles the serialization/deserialization of the byte stream.
+ * @return the name in english of the FieldType.
+ */
+ public String getName()
+ {
+ return "StringNullTerminated";
+ }
+
+ /** Returns a human-readable description of the FieldType.
+ * @return a description of the field type in the locale provided.
+ */
+ public String getDescription( Locale locale )
+ {
+ return "TODO: Not implemented yet.";
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * minimum. Zero-length is not possible, as the field type would then not
+ * be able to deserialize properly. Furher constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ *
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMinimumSize()
+ {
+ return 1;
+ }
+
+ /** Returns the size of the field.
+ * This method returns how many bytes that the field type consists of at
a
+ * maximum. A constraint is that the minimum
+ * size of the field type can not be larger than the maximum field size,
and
+ * if the <code>isVariableSize</code> method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return the minimum number of bytes that the serialized field can
+ * consist of.
+ */
+ public int getMaximumSize()
+ {
+ return Short.MAX_VALUE - 1;
+ }
+
+ /** Query whether this field can be varied in length.
+ * If this method returns false, then both the
+ * <code>getMinimumSize</code> and <code>getMaximumSize</code> methods
+ * must return the same number.
+ * @return true if the field is not a fixed length field.
+ */
+ public boolean isVariableSize()
+ {
+ return true;
+ }
+
+ /** Serialize the content.
+ * @param content the data to be stored in the field.
+ * @return the resulting byte array of the serialized data. This byte
array
+ * must be of the exact size and may not contain any
+ * non-significant bytes at the beginning or end of the array.
+ */
+ public byte[] serialize( String content )
+ throws IllegalPacketFormatException
+ {
+ try
+ {
+ byte[] bytes = content.getBytes( m_encoding );
+ byte[] result = new byte[ bytes.length + 1 ];
+ System.arraycopy( bytes, 0, result, 0, bytes.length );
+ result[ result.length - 1 ] = 0;
+ return result;
+ }
+ catch( UnsupportedEncodingException e )
+ {
+ // Can not happen. Encoding name has been verified in the
+ // constructor.
+ return null;
+ }
+ }
+
+ /** Deserializes the content from the input stream.
+ * @param input the byte[] that contains the content.
+ * @return the deserialized content from the input stream.
+ */
+ public String deserialize( byte[] input )
+ throws IllegalPacketFormatException
+ {
+ if( input[ input.length - 1 ] != 0 )
+ {
+ throw new IllegalPacketFormatException( "Not null terminated.",
this );
+ }
+ try
+ {
+ byte[] data = new byte[ input.length - 1 ];
+ System.arraycopy( input, 0, data, 0, data.length );
+ return new String( data, m_encoding );
+ }
+ catch( UnsupportedEncodingException e )
+ {
+ // Can not happen. Encoding name has been verified in the
+ // constructor.
+ return null;
+ }
+ }
+}
\ No newline at end of file



  • svn commit: r1900 - development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl, niclas, 02/25/2005

Archive powered by MHonArc 2.6.24.

Top of Page