protected float[] readByteFrequencies(final InputStream input) throws IOException {
        ReadableByteChannel inputChannel;
        // TODO: any reason to avoid closing of input & inputChannel?
        try {
            inputChannel = Channels.newChannel(input);
            // long inSize = inputChannel.size();
            float[] histogram = new float[257];
            histogram[0] = 1;

            // create buffer with capacity of maxBufSize bytes
            ByteBuffer buf = ByteBuffer.allocate(1024 * 5);
            int bytesRead = inputChannel.read(buf); // read into buffer.

            float max = -1;
            while (bytesRead != -1) {

                ((Buffer) buf).flip(); // make buffer ready for read

                while (buf.hasRemaining()) {
                    byte byt = buf.get();
                    int idx = byt;
                    idx++;
                    if (byt < 0) {
                        idx = 256 + idx;
                        histogram[idx]++;
                    } else {
                        histogram[idx]++;
                    }
                    max = Math.max(max, histogram[idx]);
                }

                buf.clear(); // make buffer ready for writing
                bytesRead = inputChannel.read(buf);
            }

            int i;
            for (i = 1; i < histogram.length; i++) {
                histogram[i] /= max;
                histogram[i] = (float) Math.sqrt(histogram[i]);
            }

            return histogram;
        } finally {
            // inputChannel.close();
        }
    }
Read the {@code inputstream} and build a byte frequency histogram

@param input stream to read from
@return byte frequencies array
@throws IOException
tika-core/src/main/java/org/apache/tika/detect/TrainedModelDetector.java