| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package org.apache.commons.io.input; |
| 19 | |
|
| 20 | |
import java.io.IOException; |
| 21 | |
import java.io.InputStream; |
| 22 | |
import java.nio.ByteBuffer; |
| 23 | |
import java.nio.CharBuffer; |
| 24 | |
import java.nio.charset.CharacterCodingException; |
| 25 | |
import java.nio.charset.Charset; |
| 26 | |
import java.nio.charset.CharsetEncoder; |
| 27 | |
import java.nio.charset.CoderResult; |
| 28 | |
import java.nio.charset.CodingErrorAction; |
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
public class CharSequenceInputStream extends InputStream { |
| 39 | |
|
| 40 | |
private static final int BUFFER_SIZE = 2048; |
| 41 | |
|
| 42 | |
private static final int EOS = -1; |
| 43 | |
|
| 44 | |
private static final int NO_MARK = -1; |
| 45 | |
|
| 46 | |
private final CharsetEncoder encoder; |
| 47 | |
private final CharBuffer cbuf; |
| 48 | |
private final ByteBuffer bbuf; |
| 49 | |
|
| 50 | |
private int mark_cbuf; |
| 51 | |
private int mark_bbuf; |
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public CharSequenceInputStream(final CharSequence cs, final Charset charset, final int bufferSize) { |
| 62 | 826 | super(); |
| 63 | 826 | this.encoder = charset.newEncoder() |
| 64 | |
.onMalformedInput(CodingErrorAction.REPLACE) |
| 65 | |
.onUnmappableCharacter(CodingErrorAction.REPLACE); |
| 66 | |
|
| 67 | 826 | final float maxBytesPerChar = encoder.maxBytesPerChar(); |
| 68 | 826 | if (bufferSize < maxBytesPerChar) { |
| 69 | 0 | throw new IllegalArgumentException("Buffer size " + bufferSize + " is less than maxBytesPerChar " + maxBytesPerChar); |
| 70 | |
} |
| 71 | 826 | this.bbuf = ByteBuffer.allocate(bufferSize); |
| 72 | 826 | this.bbuf.flip(); |
| 73 | 826 | this.cbuf = CharBuffer.wrap(cs); |
| 74 | 826 | this.mark_cbuf = NO_MARK; |
| 75 | 826 | this.mark_bbuf = NO_MARK; |
| 76 | 826 | } |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
public CharSequenceInputStream(final CharSequence cs, final String charset, final int bufferSize) { |
| 87 | 820 | this(cs, Charset.forName(charset), bufferSize); |
| 88 | 820 | } |
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
public CharSequenceInputStream(final CharSequence cs, final Charset charset) { |
| 99 | 0 | this(cs, charset, BUFFER_SIZE); |
| 100 | 0 | } |
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
public CharSequenceInputStream(final CharSequence cs, final String charset) { |
| 111 | 340 | this(cs, charset, BUFFER_SIZE); |
| 112 | 340 | } |
| 113 | |
|
| 114 | |
|
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | |
|
| 120 | |
private void fillBuffer() throws CharacterCodingException { |
| 121 | 2124 | this.bbuf.compact(); |
| 122 | 2124 | final CoderResult result = this.encoder.encode(this.cbuf, this.bbuf, true); |
| 123 | 2124 | if (result.isError()) { |
| 124 | 0 | result.throwException(); |
| 125 | |
} |
| 126 | 2124 | this.bbuf.flip(); |
| 127 | 2124 | } |
| 128 | |
|
| 129 | |
@Override |
| 130 | |
public int read(final byte[] b, int off, int len) throws IOException { |
| 131 | 2814 | if (b == null) { |
| 132 | 0 | throw new NullPointerException("Byte array is null"); |
| 133 | |
} |
| 134 | 2814 | if (len < 0 || (off + len) > b.length) { |
| 135 | 0 | throw new IndexOutOfBoundsException("Array Size=" + b.length + |
| 136 | |
", offset=" + off + ", length=" + len); |
| 137 | |
} |
| 138 | 2814 | if (len == 0) { |
| 139 | 53 | return 0; |
| 140 | |
} |
| 141 | 2761 | if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { |
| 142 | 178 | return EOS; |
| 143 | |
} |
| 144 | 2583 | int bytesRead = 0; |
| 145 | 7389 | while (len > 0) { |
| 146 | 4972 | if (this.bbuf.hasRemaining()) { |
| 147 | 3321 | final int chunk = Math.min(this.bbuf.remaining(), len); |
| 148 | 3321 | this.bbuf.get(b, off, chunk); |
| 149 | 3321 | off += chunk; |
| 150 | 3321 | len -= chunk; |
| 151 | 3321 | bytesRead += chunk; |
| 152 | 3321 | } else { |
| 153 | 1651 | fillBuffer(); |
| 154 | 1651 | if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { |
| 155 | 166 | break; |
| 156 | |
} |
| 157 | |
} |
| 158 | |
} |
| 159 | 2583 | return bytesRead == 0 && !this.cbuf.hasRemaining() ? EOS : bytesRead; |
| 160 | |
} |
| 161 | |
|
| 162 | |
@Override |
| 163 | |
public int read() throws IOException { |
| 164 | |
for (;;) { |
| 165 | 47824 | if (this.bbuf.hasRemaining()) { |
| 166 | 47356 | return this.bbuf.get() & 0xFF; |
| 167 | |
} else { |
| 168 | 468 | fillBuffer(); |
| 169 | 468 | if (!this.bbuf.hasRemaining() && !this.cbuf.hasRemaining()) { |
| 170 | 27 | return EOS; |
| 171 | |
} |
| 172 | |
} |
| 173 | |
} |
| 174 | |
} |
| 175 | |
|
| 176 | |
@Override |
| 177 | |
public int read(final byte[] b) throws IOException { |
| 178 | 574 | return read(b, 0, b.length); |
| 179 | |
} |
| 180 | |
|
| 181 | |
@Override |
| 182 | |
public long skip(long n) throws IOException { |
| 183 | |
|
| 184 | |
|
| 185 | |
|
| 186 | 500 | long skipped = 0; |
| 187 | 1658 | while (n > 0 && available() > 0) { |
| 188 | 1158 | this.read(); |
| 189 | 1158 | n--; |
| 190 | 1158 | skipped++; |
| 191 | |
} |
| 192 | 500 | return skipped; |
| 193 | |
} |
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
|
| 198 | |
|
| 199 | |
|
| 200 | |
|
| 201 | |
@Override |
| 202 | |
public int available() throws IOException { |
| 203 | |
|
| 204 | |
|
| 205 | |
|
| 206 | |
|
| 207 | 1980 | return this.bbuf.remaining() + this.cbuf.remaining(); |
| 208 | |
} |
| 209 | |
|
| 210 | |
@Override |
| 211 | |
public void close() throws IOException { |
| 212 | 826 | } |
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
@Override |
| 219 | |
public synchronized void mark(final int readlimit) { |
| 220 | 289 | this.mark_cbuf = this.cbuf.position(); |
| 221 | 289 | this.mark_bbuf = this.bbuf.position(); |
| 222 | 289 | this.cbuf.mark(); |
| 223 | 289 | this.bbuf.mark(); |
| 224 | |
|
| 225 | |
|
| 226 | 289 | } |
| 227 | |
|
| 228 | |
@Override |
| 229 | |
public synchronized void reset() throws IOException { |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
|
| 237 | |
|
| 238 | |
|
| 239 | |
|
| 240 | 293 | if (this.mark_cbuf != NO_MARK) { |
| 241 | |
|
| 242 | 289 | if (this.cbuf.position() != 0) { |
| 243 | 289 | this.encoder.reset(); |
| 244 | 289 | this.cbuf.rewind(); |
| 245 | 289 | this.bbuf.rewind(); |
| 246 | 289 | this.bbuf.limit(0); |
| 247 | 294 | while(this.cbuf.position() < this.mark_cbuf) { |
| 248 | 5 | this.bbuf.rewind(); |
| 249 | 5 | this.bbuf.limit(0); |
| 250 | 5 | fillBuffer(); |
| 251 | |
} |
| 252 | |
} |
| 253 | 289 | if (this.cbuf.position() != this.mark_cbuf) { |
| 254 | 0 | throw new IllegalStateException("Unexpected CharBuffer postion: actual="+cbuf.position() + " expected=" + this.mark_cbuf); |
| 255 | |
} |
| 256 | 289 | this.bbuf.position(this.mark_bbuf); |
| 257 | 289 | this.mark_cbuf = NO_MARK; |
| 258 | 289 | this.mark_bbuf = NO_MARK; |
| 259 | |
} |
| 260 | 293 | } |
| 261 | |
|
| 262 | |
@Override |
| 263 | |
public boolean markSupported() { |
| 264 | 1 | return true; |
| 265 | |
} |
| 266 | |
|
| 267 | |
} |