001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.reader; 020 021import java.io.BufferedReader; 022import java.io.ByteArrayInputStream; 023import java.io.IOException; 024import java.io.InputStreamReader; 025import java.nio.CharBuffer; 026import java.nio.charset.Charset; 027 028import lucee.commons.io.CharsetUtil; 029import lucee.commons.io.IOUtil; 030 031/** 032 * InputStream Reader for byte arrays, support mark 033 */ 034public final class ByteArrayInputStreamReader extends InputStreamReader { 035 036 private final BufferedReader br; 037 private final Charset charset; 038 039 040 public ByteArrayInputStreamReader(ByteArrayInputStream bais, Charset charset) throws IOException { 041 super(bais, charset); 042 this.br=IOUtil.toBufferedReader(IOUtil.getReader(bais, charset)); 043 this.charset=charset; 044 } 045 046 public ByteArrayInputStreamReader(byte[] barr, Charset charset) throws IOException { 047 this(new ByteArrayInputStream(barr), charset); 048 } 049 050 public ByteArrayInputStreamReader(String str, Charset charset) throws IOException { 051 this(new ByteArrayInputStream(str.getBytes(charset)), charset); 052 } 053 054 /** 055 * @deprecated use instead <code>{@link #ByteArrayInputStreamReader(ByteArrayInputStream, Charset)}</code> 056 * @param bais 057 * @param charsetName 058 * @throws IOException 059 */ 060 public ByteArrayInputStreamReader(ByteArrayInputStream bais, String charsetName) throws IOException { 061 this(bais, CharsetUtil.toCharset(charsetName)); 062 } 063 064 /** 065 * @deprecated use instead <code>{@link #ByteArrayInputStreamReader(byte[], Charset)}</code> 066 * @param barr 067 * @param charsetName 068 * @throws IOException 069 */ 070 public ByteArrayInputStreamReader(byte[] barr, String charsetName) throws IOException { 071 this(new ByteArrayInputStream(barr), CharsetUtil.toCharset(charsetName)); 072 } 073 074 /** 075 * @deprecated use instead <code>{@link #ByteArrayInputStreamReader(String, Charset)}</code> 076 * @param str 077 * @param charsetName 078 * @throws IOException 079 */ 080 public ByteArrayInputStreamReader(String str, String charsetName) throws IOException { 081 this(str, CharsetUtil.toCharset(charsetName)); 082 } 083 084 @Override 085 public void close() throws IOException { 086 br.close(); 087 } 088 089 @Override 090 public String getEncoding() { 091 return charset.name(); 092 } 093 public Charset getCharset() { 094 return charset; 095 } 096 097 @Override 098 public int read() throws IOException { 099 return br.read(); 100 } 101 102 @Override 103 public int read(char[] cbuf, int offset, int length) throws IOException { 104 return br.read(cbuf, offset, length); 105 } 106 107 @Override 108 public boolean ready() throws IOException { 109 return br.ready(); 110 } 111 112 @Override 113 public void mark(int readAheadLimit) throws IOException { 114 br.mark(readAheadLimit); 115 } 116 117 @Override 118 public boolean markSupported() { 119 return br.markSupported(); 120 } 121 122 @Override 123 public int read(CharBuffer target) throws IOException { 124 return br.read(target.array()); 125 } 126 127 @Override 128 public int read(char[] cbuf) throws IOException { 129 return br.read(cbuf); 130 } 131 132 @Override 133 public void reset() throws IOException { 134 br.reset(); 135 } 136 137 @Override 138 public long skip(long n) throws IOException { 139 return br.skip(n); 140 } 141 142}