Field implements Comparable<Field> { protected byte[] getBytes(Object value) { byte[] bytes = new byte[this.length]; Arrays.fill(bytes, (byte) ' '); String stringValue = value == null ? null : value.toString(); if (stringValue != null && !stringValue.equals("")) { byte[] valueBytes = value.toString().getBytes(); int numberToCopy = valueBytes.length > this.length ? this.length : valueBytes.length; System.arraycopy(valueBytes, 0, bytes, 0, numberToCopy); } return bytes; } 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 testGetBytes_inputWithExtraChars() { byte[] actualBytes = field.getBytes("abcdefg"); assertEquals("abcd", new String(actualBytes)); actualBytes = field.getBytes("abcd"); assertEquals("abcd", new String(actualBytes)); actualBytes = field.getBytes("ab"); assertEquals("ab ", new String(actualBytes)); }
@Test public void testGetBytes_null() { byte[] actualBytes = field.getBytes(null); assertEquals(" ", new String(actualBytes)); }
@Test public void testGetBytes_emptyString() { byte[] actualBytes = field.getBytes(""); assertEquals(" ", new String(actualBytes)); }