Field implements Comparable<Field> { public String read(ByteBuffer buffer) { byte[] bytes = new byte[this.length]; buffer.position(this.start); buffer.get(bytes); String fieldValue = new String(bytes); if (!whitespaceSignificant || fieldValue.trim().isEmpty()) { fieldValue = fieldValue.trim(); } log.debug("Read {}='{}'", this.id, fieldValue); return fieldValue; } Field(String id, Integer start, Integer length);  Field(String id, Integer start, Integer length, boolean composite);  Field(String id, Integer start, Integer length, boolean composite, boolean input, String alias);  Field(String id, Integer start, Integer length, boolean composite, boolean input, String alias,
            boolean whitespace); void write(ByteBuffer buffer); void write(Object value, ByteBuffer buffer); String read(ByteBuffer buffer); boolean isComposite(); String getAlias(); boolean isAliased(); boolean isInput(); boolean isWhitespaceSignificant(); @Override int compareTo(Field o); @Override int hashCode(); @Override boolean equals(Object obj); Integer getLength(); String getId(); Integer getStart(); @Override String toString(); final static Comparator<Field> DEFAULT_SORT; final static Comparator<Field> NAME_SORT; }
@Test public void testRead_withSignificantWhitespaceButEmptyWithSpaces() { Field aField = new Field("abc", 2, 6, false,false,null,true); buffer.position(0); byte[] bytes = new byte[]{'1','2',' ',' ',' ',' ',' ',' '}; buffer.put(bytes); String result = aField.read(buffer); assertEquals("",result); }
@Test public void testRead_withSignificantWhitespaceButEmptyWithNull() { Field aField = new Field("abc", 2, 6, false,false,null,true); buffer.position(0); byte[] bytes = new byte[]{'1','2','\u0000','\u0000','\u0000','\u0000','\u0000','\u0000'}; buffer.put(bytes); String result = aField.read(buffer); assertEquals("",result); }