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            @Override
034            public void close() throws IOException {
035                    br.close();
036            }
037    
038            @Override
039            public String getEncoding() {
040                    return charsetName;
041            }
042    
043            @Override
044            public int read() throws IOException {
045                    return br.read();
046            }
047    
048            @Override
049            public int read(char[] cbuf, int offset, int length) throws IOException {
050                    return br.read(cbuf, offset, length);
051            }
052    
053            @Override
054            public boolean ready() throws IOException {
055                    return br.ready();
056            }
057    
058            @Override
059            public void mark(int readAheadLimit) throws IOException {
060                    br.mark(readAheadLimit);
061            }
062    
063            @Override
064            public boolean markSupported() {
065                    return br.markSupported();
066            }
067    
068            @Override
069            public int read(CharBuffer target) throws IOException {
070                    return br.read(target.array());
071            }
072    
073            @Override
074            public int read(char[] cbuf) throws IOException {
075                    return br.read(cbuf);
076            }
077    
078            @Override
079            public void reset() throws IOException {
080                    br.reset();
081            }
082    
083            @Override
084            public long skip(long n) throws IOException {
085                    return br.skip(n);
086            }
087    
088    }