|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.writersforge.catalan.text.AsciiFieldManager
A field-level manager for packing and unpacking ASCII data. Provides methods for loading a special field format specification string and using that to extract packed data from an ASCII string into Java data types, and to insert Java data types into ASCII strings.
The field spec is a series of field type and size declarations encoded into a single String parameter in the AsciiFieldManager constructor. Each field contains a mandatory numerical length and type, and an optional array count. The field length determines how many ASCII characters of data correspond to a single element of that field data. The array count determines how many consecutive elements field exist in that slice of the ASCII data. Whitespace in the field spec is completely ignored.
The field type must be one of six categories:
| Type | Description | Java Type |
|---|---|---|
| x | padding | N/A |
| b | byte | java.lang.Byte |
| c | character | java.lang.Character |
| i | integer | java.lang.Integer |
| s | string | java.lang.String |
| f | float | java.lang.Double |
Non-array field specs are simply the field length and the type. Thus, the
field spec "4s 3i" declares seven characters of ASCII data; the first four
characters make up a String object and the final three
characters are converted into an Integer object. Thus, the
ASCII data "1234567" would yield a String field of "1234" and an Integer
field of 567. This association works both ways, so conversely a String
field of "nope" and an Integer of 34 would pack into ASCII data "nope34 ".
Array field specs provide an easy way to load large chunks of uniformly
sized data fields into a single Java array object. To declare an array
spec, append the array size to the field spec, surrounded by square
brackets. For example, a field spec of "2s 2i[4]" declares one
two-character String and four two-character Integer objects. The ASCII
data "0123456789" would unpack into a String of "01" and an Integer[]
array equivalent to the Java code: new Integer[] { 23, 45, 67, 89
}.
| Constructor Summary | |
AsciiFieldManager(java.lang.String fieldFormat)
Creates a new instance of AsciiFieldManager. |
|
| Method Summary | |
java.lang.Object |
extractValue(int fieldIndex,
java.lang.String asciiData)
Parses the asciiData input data and pulls out the data corresponding to the given field index, then converts that data to the correct Java data type. |
int |
getFieldArraySize(int field)
Retrieves the number of array elements of the given field. |
int |
getFieldCount()
Calculates the number of discrete fields in the format spec. |
int |
getFieldLength(int field)
Retrieves the length of the given field in its ASCII version. |
java.lang.Class |
getFieldType(int field)
Retrieves the Java type that corresponds to the given field number. |
void |
insertValue(int fieldIndex,
java.lang.StringBuffer asciiData,
java.lang.Object newValue)
Converts the newValue object into ASCII content and inserts it into the asciiData buffer. |
void |
normalize(java.lang.StringBuffer data)
Fills in current padding without touching any existing data fields. |
void |
setPadding(java.lang.String padding)
Assigns a new custom padding specification. |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
public AsciiFieldManager(java.lang.String fieldFormat)
throws java.lang.IllegalArgumentException
fieldFormat - Python-style packing format specification
java.lang.IllegalArgumentException - if fieldFormat is not syntactically
valid| Method Detail |
public int getFieldCount()
public java.lang.Class getFieldType(int field)
String, field 1 would be a Double[], and
field 2 would be a Integer[].
field - the index of the field in the format spec
public int getFieldLength(int field)
field - the index of the field in the format spec
public int getFieldArraySize(int field)
field - the index of the field in the format spec
public void setPadding(java.lang.String padding)
normalize(java.lang.StringBuffer)public void normalize(java.lang.StringBuffer data)
normalize() on an empty
StringBuffer with default padding will result in a
StringBuffer filled with spaces to the exact data length
determined by the field spec. The padding string will be cycled
through across multiple padding fields. This allows for complex
padding. For example, given the following code:
String spec = "2x2s4x3s3x";
AsciiFieldManager fields = new AsciiFieldManager (spec);
fields.setPadding ("--XXXX");
StringBuffer data = new StringBuffer ();
fields.normalize (data);
The new value of data would be:
"-- XXXX --X"The padding is split across the first two padding fields, and cycled through again in the third padding field.
data - the ASCII data to normalize
public java.lang.Object extractValue(int fieldIndex,
java.lang.String asciiData)
throws java.lang.IllegalArgumentException
fieldIndex - the index of the field in the format specasciiData - input data that follows the format spec
java.lang.IllegalArgumentException - if asciiData cannot be converted into
a valid object for the given field
public void insertValue(int fieldIndex,
java.lang.StringBuffer asciiData,
java.lang.Object newValue)
fieldIndex - the index of the field in the format specasciiData - output target to write the data tonewValue - the input data to write to asciiData
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||