Skip to Content.
Sympa Menu

notify-dpml - svn commit: r1914 - 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: r1914 - development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl
  • Date: Tue, 01 Mar 2005 05:31:34 +0100

Author: niclas
Date: Tue Mar 1 05:31:32 2005
New Revision: 1914

Added:

development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/FixedLengthString.java
(contents, props changed)
Log:
Fixed length field added.

Added:
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/FixedLengthString.java
==============================================================================
--- (empty file)
+++
development/laboratory/planet/components/protocol/packetimpl/src/main/net/dpml/protocol/packet/impl/FixedLengthString.java
Tue Mar 1 05:31:32 2005
@@ -0,0 +1,161 @@
+/*
+ * 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 translation of strings into a fixed length character sequence.
+ *
+ * @metro.component name="fixed-length-string" lifestyle="singleton"
+ * @metro.service type="net.dpml.protocol.packet.FieldType"
+ */
+public class FixedLengthString
+ implements FieldType
+{
+ private int m_fieldLength;
+ private String m_encoding;
+
+ public FixedLengthString( Parameters params )
+ throws IllegalArgumentException, ParameterException
+ {
+ m_fieldLength = params.getParameterAsInteger( "field-length", 1 );
+ 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." );
+ }
+ }
+
+ public int getFieldLength()
+ {
+ return m_fieldLength;
+ }
+
+ /** 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 "FixedLengthString";
+ }
+
+ /** 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 m_fieldLength;
+ }
+
+ /** 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 m_fieldLength;
+ }
+
+ /** 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
+ {
+ if( content.length() != m_fieldLength )
+ throw new IllegalPacketFormatException( "The expected field
length was '" + m_fieldLength + "', but the length of the content was '" +
content.length() + "'.", this );
+ return content.getBytes( m_encoding );
+ }
+ 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
+ {
+ try
+ {
+ return new String( input, m_encoding );
+ }
+ catch( UnsupportedEncodingException e )
+ {
+ // Can not happen. Encoding name has been verified in the
+ // constructor.
+ return null;
+ }
+ }
+}



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

Archive powered by MHonArc 2.6.24.

Top of Page