* @return reconstructed messages
* @throws IOException if an I/O error occurs
*/
public BridgeMsg loadMessage(File head, File body) throws IOException {
log.debug("loadMessage");
BridgeMsg msg = new BridgeMsg();
/* read meta data */
{
HeaderReader in = new HeaderReader(new InputStreamReader(new FileInputStream(head), "UTF-8"));
try {
while (in.readHeader()) {
String name = in.getName();
String value = in.getValue();
if (name.equalsIgnoreCase(MsgHdr.DUMP_MESSAGE_ID)) {
msg.setMessageId(value);
}
else if (name.equalsIgnoreCase(MsgHdr.DUMP_MESSAGE_TYPE)) {
msg.setMsgType(BridgeMsg.getTypeFromMnemonic(value));
}
else if (name.equalsIgnoreCase(MsgHdr.DUMP_PERSISTENCE)) {
msg.setPersistence(BridgePersistence.fromMnemonic(value));
}
else {
// skip
}
}
}
finally {
in.close();
}
}
/* read payload */
{
FileInputStream inFile = new FileInputStream(body);
try {
switch (msg.getMsgType()) {
case BridgeMsg.TYPE_BYTES:
log.debug("Payload TYPE_BYTES");
byte buf[] = new byte[8192];
ByteArrayOutputStream out = new ByteArrayOutputStream();
for (;;) {
int count = inFile.read(buf);
if (count < 0)
break;
out.write(buf, 0, count);
}
msg.setByteData(out.toByteArray());
break;
case BridgeMsg.TYPE_TEXT:
log.debug("Payload TYPE_TEXT");
char cbuf[] = new char[4096];
CharArrayWriter cout = new CharArrayWriter();
InputStreamReader cin = new InputStreamReader(inFile, "UTF-8");
for (;;) {
int count = cin.read(cbuf);
if (count < 0)
break;
cout.write(cbuf, 0, count);
}
msg.setTextData(cout.toString());
}
}
finally {
inFile.close();
}