I have a method which extracts filedata
and converts it into a String
array:
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import org.apache.james.mime4j.message.BodyPart;
import org.apache.james.mime4j.message.Message;
import org.apache.james.mime4j.message.Multipart;
import org.apache.james.mime4j.message.TextBody;
protected String[] extractLedesText(byte[] fileData) {
// Remove the BOM if present
byte[] array = { (byte) 0xEF, (byte) 0xBB, (byte) 0xBF };
byte[] data = { fileData[0], fileData[1], fileData[2] };
if (fileData.length > 3 && Arrays.equals(data, array)) {
fileData = ArrayUtils.subarray(fileData, 3, fileData.length-1);
}
String ledes = new String(fileData);
if (ledes.startsWith("MIME")) {
try {
ledes = null;
Message signed = new Message(new ByteArrayInputStream(fileData));
for (BodyPart part : ((Multipart) signed.getBody()).getBodyParts()) {
if (part.getMimeType().equalsIgnoreCase("text/plain")) {
TextBody tb = (TextBody) part.getBody();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
tb.writeTo(baos);
return extractLedesText(baos.toByteArray());
}
}
throw new BaseApplicationException(
"No MIME part found with MIME type of 'text/plain' while parsing submitted invoice file.");
} catch (IOException ioe) {
throw new BaseApplicationException(ioe);
}
} else {
return ledes.split("\\[]");
}
}
For example, below is the leads file:
LEDES98BI V2[] INVOICE_DATE|INVOICE_NUMBER|CLIENT_ID|LAW_FIRM_MATTER_ID|INVOICE_TOTAL[] 20150301|INV-Error_Test1|160|LF_MAT_1221|22[] 20150301|INV-Error_Test1|160|LF_MAT_1221|22[] 20150301|INV-Error_Test1|160|LF_MAT_1221|22[]
The extractLedesText
method converts the above file data to string array of lines.
We recently upgraded to Java 8 and am wondering whether this method can be optimized further.
Message
class? And the other Javadocs... \$\endgroup\$ – h.j.k. Mar 26 '15 at 0:32