package pakman;
import java.io.*;
import org.beanio.stream.*;
import org.beanio.stream.fixedlength.FixedLengthRecordParserFactory;
public class CustomFixedLengthParserFactory extends FixedLengthRecordParserFactory {
private Integer recordLength;
@Override
public RecordReader createReader(Reader in) throws IllegalArgumentException {
final RecordReader reader = super.createReader(in);
return new RecordReader() {
public Object read() throws IOException, RecordIOException {
String record = (String) reader.read();
if (record != null) {
record = pad(record);
}
return record;
}
public void close() throws IOException {
reader.close();
}
public int getRecordLineNumber() {
return reader.getRecordLineNumber();
}
public String getRecordText() {
return reader.getRecordText();
}
};
}
private String pad(String record) {
if (recordLength == null) {
return record;
}
int n = recordLength - record.length();
if (n <= 0) {
return record;
}
StringBuilder s = new StringBuilder(record);
for (int i=0; i<n; i++) {
s.append(' ');
}
return s.toString();
}
public Integer getRecordLength() {
return recordLength;
}
public void setRecordLength(Integer recordLength) {
this.recordLength = recordLength;
}
}
<beanio xmlns="http://www.beanio.org/2012/03"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.beanio.org/2012/03 http://www.beanio.org/2012/03/mapping.xsd">
<stream name="stream" format="fixedlength">
<parser class="pakman.CustomFixedLengthParserFactory">
<property name="recordLength" value="20" />
</parser>
<record name="user" class="map">
<field name="firstName" length="10" />
<field name="lastName" length="10" />
</record>
</stream>
</beanio>