Code example for InputStream

Methods: markmarkSupportedreadreset

0
     * a buffer is extracted, leaving the stream in its original state. 
     */ 
    public final Charset detectCharset(InputStream is)
    { 
        // Only support marking streams 
        if (!is.markSupported())
        { 
            throw new IllegalArgumentException("The InputStream must support marks.  Wrap the stream in a BufferedInputStream.");
        } 
        try 
        { 
            int bufferSize = getBufferSize();
            if (bufferSize < 0)
            { 
                throw new RuntimeException("The required buffer size may not be negative: " + bufferSize);
            } 
            // Mark the stream for just a few more than we actually will need 
            is.mark(bufferSize);
            // Create a buffer to hold the data 
            byte[] buffer = new byte[bufferSize];
            // Fill it 
            int read = is.read(buffer);
            // Create an appropriately sized buffer 
            if (read > -1 && read < buffer.length)
            { 
                byte[] copyBuffer = new byte[read];
                System.arraycopy(buffer, 0, copyBuffer, 0, read);
                buffer = copyBuffer;
            } 
            // Detect 
            return detectCharset(buffer);
        } 
        catch (IOException e)
        { 
            // Attempt a reset 
            throw new AlfrescoRuntimeException("IOException while attempting to detect charset encoding.", e);
        } 
        finally 
        { 
            try { is.reset(); } catch (Throwable ee) {}
        } 
    } 
     
    public final Charset detectCharset(byte[] buffer)
    { 
Connect your IDE to all the code out there  Get Codota for Java