Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

NUTCH-1997: Fix for Add CBOR magic header to CommonCrawlDataDumper output contributed by Giuseppe Totaro, and Luke Sh.

Chris Mattmannbf0d49c4c04c
diff --git a/CHANGES.txt b/CHANGES.txt
index 92e84f803..5dffd9c4b 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -2,6 +2,9 @@ Nutch Change Log
Nutch Current Development 1.10-SNAPSHOT
+* NUTCH-1997 Add CBOR "magic header" to CommonCrawlDataDumper
+ output (Giuseppe Totaro, Luke Sh via mattmann)
+
* NUTCH-1991 Tika mime detection not using Nutch supplied tika-mimetypes.xml for content based
detection (Iain Lopata, snagel via mattmann)
diff --git a/src/java/org/apache/nutch/tools/CommonCrawlDataDumper.java b/src/java/org/apache/nutch/tools/CommonCrawlDataDumper.java
index ff18c6a5b..3cd01a8d0 100644
--- a/src/java/org/apache/nutch/tools/CommonCrawlDataDumper.java
+++ b/src/java/org/apache/nutch/tools/CommonCrawlDataDumper.java
@@ -515,6 +515,31 @@ public class CommonCrawlDataDumper {
tarOutput.setLongFileMode(TarArchiveOutputStream.LONGFILE_GNU);
}
+ /**
+ * Writes the CBOR "Self-Describe Tag" (value 55799, serialized as 3-byte
+ * sequence of {@code 0xd9d9f7}) at the current position. This method must
+ * be used to write the CBOR magic number at the beginning of the document.
+ * Since version 2.5, <a
+ * href="https://github.com/FasterXML/jackson-dataformat-cbor"
+ * >jackson-dataformat-cbor</a> will support the {@code WRITE_TYPE_HEADER}
+ * feature to write that type tag at the beginning of the document.
+ *
+ * @see <a href="https://tools.ietf.org/html/rfc7049#section-2.4.5">RFC
+ * 7049</a>
+ * @param generator {@link CBORGenerator} object used to create a CBOR-encoded document.
+ * @throws IOException if any I/O error occurs.
+ */
+ private void writeMagicHeader(CBORGenerator generator) throws IOException {
+ // Writes self-describe CBOR
+ // https://tools.ietf.org/html/rfc7049#section-2.4.5
+ // It will be supported in jackson-cbor since 2.5
+ byte[] header = new byte[3];
+ header[0] = (byte) 0xd9;
+ header[1] = (byte) 0xd9;
+ header[2] = (byte) 0xf7;
+ generator.writeBytes(header, 0, header.length);
+ }
+
private byte[] serializeCBORData(String jsonData) {
CBORFactory factory = new CBORFactory();
@@ -524,6 +549,8 @@ public class CommonCrawlDataDumper {
try {
stream = new ByteArrayOutputStream();
generator = factory.createGenerator(stream);
+ // Writes CBOR tag
+ writeMagicHeader(generator);
generator.writeString(jsonData);
generator.flush();
stream.flush();