Repository: tika
Updated Branches:
  refs/heads/master 91313e36f -> d447193f2


TIKA-1885: Addition of ZeroSizeFileDetector based on Pull Request from Adesh 
Gupta.


Project: http://git-wip-us.apache.org/repos/asf/tika/repo
Commit: http://git-wip-us.apache.org/repos/asf/tika/commit/d447193f
Tree: http://git-wip-us.apache.org/repos/asf/tika/tree/d447193f
Diff: http://git-wip-us.apache.org/repos/asf/tika/diff/d447193f

Branch: refs/heads/master
Commit: d447193f29531df3022f5137b8f0ec1c73e58cc8
Parents: 91313e3
Author: David Meikle <[email protected]>
Authored: Sun May 8 11:20:11 2016 +0100
Committer: David Meikle <[email protected]>
Committed: Sun May 8 11:20:11 2016 +0100

----------------------------------------------------------------------
 .../tika/detect/ZeroSizeFileDetector.java       | 38 ++++++++++++
 .../java/org/apache/tika/mime/MediaType.java    |  3 +
 .../org/apache/tika/mime/MediaTypeRegistry.java |  2 +
 .../tika/detect/ZeroSizeFileDetectorTest.java   | 63 ++++++++++++++++++++
 4 files changed, 106 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tika/blob/d447193f/tika-core/src/main/java/org/apache/tika/detect/ZeroSizeFileDetector.java
----------------------------------------------------------------------
diff --git 
a/tika-core/src/main/java/org/apache/tika/detect/ZeroSizeFileDetector.java 
b/tika-core/src/main/java/org/apache/tika/detect/ZeroSizeFileDetector.java
new file mode 100644
index 0000000..524e80c
--- /dev/null
+++ b/tika-core/src/main/java/org/apache/tika/detect/ZeroSizeFileDetector.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.detect;
+
+import java.io.InputStream;
+import java.io.IOException;
+
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.mime.MediaType;
+
+/**
+ * Detector to identify zero length files as application/x-zerovalue
+ */
+public class ZeroSizeFileDetector implements Detector
+{
+       public MediaType detect(InputStream stream, Metadata metadata) throws 
IOException
+       {
+        if(stream != null && stream.available() == 0) {
+            return MediaType.ZERO_SIZE;
+        } else {
+            return MediaType.OCTET_STREAM;
+        }
+       }
+}

http://git-wip-us.apache.org/repos/asf/tika/blob/d447193f/tika-core/src/main/java/org/apache/tika/mime/MediaType.java
----------------------------------------------------------------------
diff --git a/tika-core/src/main/java/org/apache/tika/mime/MediaType.java 
b/tika-core/src/main/java/org/apache/tika/mime/MediaType.java
index 0080c4a..d0aaf5c 100644
--- a/tika-core/src/main/java/org/apache/tika/mime/MediaType.java
+++ b/tika-core/src/main/java/org/apache/tika/mime/MediaType.java
@@ -71,6 +71,9 @@ public final class MediaType implements 
Comparable<MediaType>, Serializable {
     public static final MediaType OCTET_STREAM =
             parse("application/octet-stream");
 
+       public static final MediaType ZERO_SIZE =
+            parse("application/x-zerosize");
+
     public static final MediaType TEXT_PLAIN = parse("text/plain");
 
     public static final MediaType TEXT_HTML = parse("text/html");

http://git-wip-us.apache.org/repos/asf/tika/blob/d447193f/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java
----------------------------------------------------------------------
diff --git 
a/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java 
b/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java
index 33dce8f..37aecda 100644
--- a/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java
+++ b/tika-core/src/main/java/org/apache/tika/mime/MediaTypeRegistry.java
@@ -197,6 +197,8 @@ public class MediaTypeRegistry implements Serializable {
         } else if ("text".equals(type.getType())
                 && !MediaType.TEXT_PLAIN.equals(type)) {
             return MediaType.TEXT_PLAIN;
+        } else if(type.getType().contains("zero") && 
!MediaType.ZERO_SIZE.equals(type)){
+            return MediaType.ZERO_SIZE;
         } else if (!MediaType.OCTET_STREAM.equals(type)) {
             return MediaType.OCTET_STREAM;
         } else {

http://git-wip-us.apache.org/repos/asf/tika/blob/d447193f/tika-core/src/test/java/org/apache/tika/detect/ZeroSizeFileDetectorTest.java
----------------------------------------------------------------------
diff --git 
a/tika-core/src/test/java/org/apache/tika/detect/ZeroSizeFileDetectorTest.java 
b/tika-core/src/test/java/org/apache/tika/detect/ZeroSizeFileDetectorTest.java
new file mode 100644
index 0000000..1f535d4
--- /dev/null
+++ 
b/tika-core/src/test/java/org/apache/tika/detect/ZeroSizeFileDetectorTest.java
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.tika.detect;
+
+import org.apache.tika.metadata.Metadata;
+import org.apache.tika.mime.MediaType;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+
+public class ZeroSizeFileDetectorTest {
+
+    private Detector detector;
+
+    @Before
+    public void setUp() {
+        detector = new ZeroSizeFileDetector();
+    }
+
+    @Test
+    public void testDetectZeroValue() {
+        byte[] data = "".getBytes();
+        detect(data, MediaType.ZERO_SIZE);
+        System.out.println();
+    }
+
+    @Test
+    public void testDetectNonZeroValue() {
+        byte[] data = "Testing 1...2...3".getBytes();
+        detect(data, MediaType.OCTET_STREAM);
+        System.out.println();
+    }
+
+    private void detect(byte[] data, MediaType type) {
+        try {
+            InputStream stream = new ByteArrayInputStream(data);
+            assertEquals(type, detector.detect(stream, new Metadata()));
+        } catch (IOException e) {
+            fail("Unexpected exception from ZeroSizeFileDetector");
+        }
+    }
+
+}

Reply via email to