@Override
    public Charset detect(InputStream input, Metadata metadata) throws IOException {
        input.mark(MAX_BYTES);
        byte[] bytes = new byte[MAX_BYTES];
        try {
            int numRead = IOUtils.read(input, bytes);
            if (numRead < MIN_BYTES) {
                return null;
            } else if (numRead < MAX_BYTES) {
                //s
                byte[] tmpBytes = new byte[numRead];
                System.arraycopy(bytes, 0, tmpBytes, 0, numRead);
                bytes = tmpBytes;
            }
        } finally {
            input.reset();
        }
        for (int i = 0; i < BOMS.length; i++) {
            ByteOrderMark bom = BOMS[i];
            if (startsWith(bom, bytes)) {
                return CHARSETS[i];
            }
        }
        return null;
    }
@Test
    public void testBasic() throws Exception {
        EncodingDetector detector = new BOMDetector();
        for (ByteOrderMark bom : new ByteOrderMark[]{
                ByteOrderMark.UTF_8, ByteOrderMark.UTF_16BE,
                ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE
        }) {
            UnsynchronizedByteArrayOutputStream bos = createStream(bom);
            try (BOMInputStream bomInputStream =
                         new BOMInputStream(UnsynchronizedByteArrayInputStream.builder().setByteArray(bos.toByteArray()).get(),
                                 ByteOrderMark.UTF_8, ByteOrderMark.UTF_32BE, ByteOrderMark.UTF_32LE,
                                 ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_16LE)) {
                assertEquals(bom, bomInputStream.getBOM());
            }
            try (UnsynchronizedByteArrayInputStream is =
                         UnsynchronizedByteArrayInputStream.builder().setByteArray(bos.toByteArray()).get()) {
                assertEquals(Charset.forName(bom.getCharsetName()), detector.detect(is, new Metadata()));
                int cnt = 0;
                int c = is.read();
                while (c > -1) {
                    cnt++;
                    c = is.read();
                }
                assertEquals(100 + bom.getBytes().length, cnt);
            }
        }
    }