|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.tika.detect; |
| 18 | + |
| 19 | +/** |
| 20 | + * Utility class for computing a histogram of the bytes seen in a stream. |
| 21 | + * |
| 22 | + * @since Apache Tika 1.2 |
| 23 | + */ |
| 24 | +public class TextStatistics { |
| 25 | + |
| 26 | + private final int[] counts = new int[256]; |
| 27 | + |
| 28 | + private int total = 0; |
| 29 | + |
| 30 | + public void addData(byte[] buffer, int offset, int length) { |
| 31 | + for (int i = 0; i < length; i++) { |
| 32 | + counts[buffer[offset + i] & 0xff]++; |
| 33 | + total++; |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Checks whether at least one byte was seen and that the bytes that |
| 39 | + * were seen were mostly plain text (i.e. < 2% control, > 90% ASCII range). |
| 40 | + * |
| 41 | + * @see <a href="https://issues.apache.org/jira/browse/TIKA-483">TIKA-483</a> |
| 42 | + * @see <a href="https://issues.apache.org/jira/browse/TIKA-688">TIKA-688</a> |
| 43 | + * @return <code>true</code> if the seen bytes were mostly safe ASCII, |
| 44 | + * <code>false</code> otherwise |
| 45 | + */ |
| 46 | + public boolean isMostlyAscii() { |
| 47 | + int control = count(0, 0x20); |
| 48 | + int ascii = count(0x20, 128); |
| 49 | + int safe = countSafeControl(); |
| 50 | + return total > 0 |
| 51 | + && (control - safe) * 100 < total * 2 |
| 52 | + && (ascii + safe) * 100 > total * 90; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * Returns the total number of bytes seen so far. |
| 57 | + * |
| 58 | + * @return count of all bytes |
| 59 | + */ |
| 60 | + public int count() { |
| 61 | + return total; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the number of occurrences of the given byte. |
| 66 | + * |
| 67 | + * @param b byte |
| 68 | + * @return count of the given byte |
| 69 | + */ |
| 70 | + public int count(int b) { |
| 71 | + return counts[b & 0xff]; |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Counts control characters (i.e. < 0x20, excluding tab, CR, LF, |
| 76 | + * page feed and escape). |
| 77 | + * <p> |
| 78 | + * This definition of control characters is based on section 4 of the |
| 79 | + * "Content-Type Processing Model" Internet-draft |
| 80 | + * (<a href="http://webblaze.cs.berkeley.edu/2009/mime-sniff/mime-sniff.txt" |
| 81 | + * >draft-abarth-mime-sniff-01</a>). |
| 82 | + * <pre> |
| 83 | + * +-------------------------+ |
| 84 | + * | Binary data byte ranges | |
| 85 | + * +-------------------------+ |
| 86 | + * | 0x00 -- 0x08 | |
| 87 | + * | 0x0B | |
| 88 | + * | 0x0E -- 0x1A | |
| 89 | + * | 0x1C -- 0x1F | |
| 90 | + * +-------------------------+ |
| 91 | + * </pre> |
| 92 | + * |
| 93 | + * @see <a href="https://issues.apache.org/jira/browse/TIKA-154">TIKA-154</a> |
| 94 | + * @return count of control characters |
| 95 | + */ |
| 96 | + public int countControl() { |
| 97 | + return count(0, 0x20) - countSafeControl(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Counts "safe" (i.e. seven-bit non-control) ASCII characters. |
| 102 | + * |
| 103 | + * @see #countControl() |
| 104 | + * @return count of safe ASCII characters |
| 105 | + */ |
| 106 | + public int countSafeAscii() { |
| 107 | + return count(0x20, 128) + countSafeControl(); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Counts eight bit characters, i.e. bytes with their highest bit set. |
| 112 | + * |
| 113 | + * @return count of eight bit characters |
| 114 | + */ |
| 115 | + public int countEightBit() { |
| 116 | + return count(128, 256); |
| 117 | + } |
| 118 | + |
| 119 | + private int count(int from, int to) { |
| 120 | + assert 0 <= from && to < counts.length; |
| 121 | + int count = 0; |
| 122 | + for (int i = from; i < to; i++) { |
| 123 | + count += counts[i]; |
| 124 | + } |
| 125 | + return count; |
| 126 | + } |
| 127 | + |
| 128 | + private int countSafeControl() { |
| 129 | + return count('\t') + count('\n') + count('\r') // tab, LF, CR |
| 130 | + + count(0x0c) + count(0x1b); // new page, escape |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments