Skip to content

Commit

Permalink
Resolved archivesunleashed/aut issue #128. (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
jrwiebe authored and ruebot committed Dec 4, 2017
1 parent 12e6464 commit 3eb093a
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions src/main/java/io/archivesunleashed/data/ArcRecordUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,34 @@ public static ARCRecord fromBytes(final byte[] bytes) throws IOException {
* @return raw contents
* @throws IOException if there is an issue
*/
public static byte[] toBytes(ARCRecord record) throws IOException {
public static byte[] toBytes(final ARCRecord record) throws IOException {
ARCRecordMetaData meta = record.getMetaData();

String metaline = meta.getUrl() + " " + meta.getIp() + " " + meta.getDate() + " "
+ meta.getMimetype() + " " + (int) meta.getLength() + "\n";
String metaline = meta.getUrl() + " " + meta.getIp()
+ " " + meta.getDate() + " " + meta.getMimetype()
+ " " + (int) meta.getLength();
String versionEtc = "";


if (meta.getOffset() == 0) {
versionEtc = meta.getVersion().replace(".", " ") +
" " + meta.getOrigin() + "\n" +
"URL IP-address Archive-date Content-type Archive-length\n";
versionEtc = "\n" + meta.getVersion().replace(".", " ")
+ " " + meta.getOrigin() + "\n"
+ "URL IP-address Archive-date Content-type Archive-length";
metaline += versionEtc;
}

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
dout.write(metaline.getBytes());
copyStream(record, (int) meta.getLength() - versionEtc.length(), true, dout);

dout.write("\n".getBytes());

long recordLength = meta.getLength() - versionEtc.length();
long len = IOUtils.copyLarge(new BoundedInputStream(record, recordLength),
dout);
if (len != recordLength) {
LOG.error("Read " + len + " bytes but expected " + recordLength
+ " bytes. Continuing...");
}
return baos.toByteArray();
}

Expand All @@ -95,22 +104,18 @@ public static byte[] toBytes(ARCRecord record) throws IOException {
* @return raw contents
* @throws IOException if there is an issue
*/
public static byte[] getContent(ARCRecord record) throws IOException {
public static byte[] getContent(final ARCRecord record) throws IOException {
ARCRecordMetaData meta = record.getMetaData();

ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(baos);
String versionEtc = "";

if (meta.getOffset() == 0) {
versionEtc = meta.getVersion().replace(".", " ") +
" InternetArchive\n" + // Should have meta.getOrigin()
"URL IP-address Archive-date Content-type Archive-length\n";
dout.write(versionEtc.getBytes());
versionEtc = "\n" + meta.getVersion().replace(".", " ")
+ " " + meta.getOrigin() + "\n"
+ "URL IP-address Archive-date Content-type Archive-length";
}
copyStream(record, (int) meta.getLength() - versionEtc.length(), true, dout);

return baos.toByteArray();
return copyToByteArray(record, (int) meta.getLength()
- versionEtc.length(), true);
}

/**
Expand Down

0 comments on commit 3eb093a

Please sign in to comment.