001    package railo.commons.io.reader;
002    
003    import java.io.BufferedReader;
004    import java.io.ByteArrayInputStream;
005    import java.io.IOException;
006    import java.io.InputStreamReader;
007    import java.nio.CharBuffer;
008    
009    import railo.commons.io.IOUtil;
010    
011    /**
012     * InputStream Reader for byte arrays, support mark
013     */
014    public final class ByteArrayInputStreamReader extends InputStreamReader {
015    
016            private final BufferedReader br;
017            private final String charsetName;
018    
019            public ByteArrayInputStreamReader(ByteArrayInputStream bais, String charsetName) throws IOException {
020                    super(bais, charsetName);
021                    this.br=IOUtil.toBufferedReader(IOUtil.getReader(bais, charsetName));
022                    this.charsetName=charsetName;
023            }
024            
025            public ByteArrayInputStreamReader(byte[] barr, String charsetName) throws IOException {
026                    this(new ByteArrayInputStream(barr), charsetName);
027            }
028    
029            public ByteArrayInputStreamReader(String str, String charsetName) throws IOException {
030                    this(new ByteArrayInputStream(str.getBytes(charsetName)), charsetName);
031            }
032    
033            /**
034             *
035             * @see java.io.InputStreamReader#close()
036             */
037            public void close() throws IOException {
038                    br.close();
039            }
040    
041            /**
042             *
043             * @see java.io.InputStreamReader#getEncoding()
044             */
045            public String getEncoding() {
046                    return charsetName;
047            }
048    
049            /**
050             *
051             * @see java.io.InputStreamReader#read()
052             */
053            public int read() throws IOException {
054                    return br.read();
055            }
056    
057            /**
058             *
059             * @see java.io.InputStreamReader#read(char[], int, int)
060             */
061            public int read(char[] cbuf, int offset, int length) throws IOException {
062                    return br.read(cbuf, offset, length);
063            }
064    
065            /**
066             *
067             * @see java.io.InputStreamReader#ready()
068             */
069            public boolean ready() throws IOException {
070                    return br.ready();
071            }
072    
073            /**
074             *
075             * @see java.io.Reader#mark(int)
076             */
077            public void mark(int readAheadLimit) throws IOException {
078                    br.mark(readAheadLimit);
079            }
080    
081            /**
082             *
083             * @see java.io.Reader#markSupported()
084             */
085            public boolean markSupported() {
086                    return br.markSupported();
087            }
088    
089            /**
090             *
091             * @see java.io.Reader#read(java.nio.CharBuffer)
092             */
093            public int read(CharBuffer target) throws IOException {
094                    return br.read(target.array());
095            }
096    
097            /**
098             *
099             * @see java.io.Reader#read(char[])
100             */
101            public int read(char[] cbuf) throws IOException {
102                    return br.read(cbuf);
103            }
104    
105            /**
106             *
107             * @see java.io.Reader#reset()
108             */
109            public void reset() throws IOException {
110                    br.reset();
111            }
112    
113            /**
114             *
115             * @see java.io.Reader#skip(long)
116             */
117            public long skip(long n) throws IOException {
118                    return br.skip(n);
119            }
120    
121    }