| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.store; |
| 7 |
|
| 8 |
import java.io.IOException; |
| 9 |
import java.io.InputStream; |
| 10 |
import java.io.Reader; |
| 11 |
import java.nio.ByteBuffer; |
| 12 |
import java.nio.CharBuffer; |
| 13 |
import java.nio.charset.CharsetEncoder; |
| 14 |
import java.nio.charset.CodingErrorAction; |
| 15 |
import java.nio.charset.StandardCharsets; |
| 16 |
|
| 17 |
import org.h2.engine.Constants; |
| 18 |
|
| 19 |
/** |
| 20 |
* An input stream that reads the data from a reader and limits the number of |
| 21 |
* bytes that can be read. |
| 22 |
*/ |
| 23 |
public class CountingReaderInputStream extends InputStream { |
| 24 |
|
| 25 |
private final Reader reader; |
| 26 |
|
| 27 |
private final CharBuffer charBuffer = |
| 28 |
CharBuffer.allocate(Constants.IO_BUFFER_SIZE); |
| 29 |
|
| 30 |
private final CharsetEncoder encoder = StandardCharsets.UTF_8.newEncoder(). |
| 31 |
onMalformedInput(CodingErrorAction.REPLACE). |
| 32 |
onUnmappableCharacter(CodingErrorAction.REPLACE); |
| 33 |
|
| 34 |
private ByteBuffer byteBuffer = ByteBuffer.allocate(0); |
| 35 |
private long length; |
| 36 |
private long remaining; |
| 37 |
|
| 38 |
public CountingReaderInputStream(Reader reader, long maxLength) { |
| 39 |
this.reader = reader; |
| 40 |
this.remaining = maxLength; |
| 41 |
} |
| 42 |
|
| 43 |
@Override |
| 44 |
public int read(byte[] buff, int offset, int len) throws IOException { |
| 45 |
if (!fetch()) { |
| 46 |
return -1; |
| 47 |
} |
| 48 |
len = Math.min(len, byteBuffer.remaining()); |
| 49 |
byteBuffer.get(buff, offset, len); |
| 50 |
return len; |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public int read() throws IOException { |
| 55 |
if (!fetch()) { |
| 56 |
return -1; |
| 57 |
} |
| 58 |
return byteBuffer.get() & 255; |
| 59 |
} |
| 60 |
|
| 61 |
private boolean fetch() throws IOException { |
| 62 |
if (byteBuffer != null && byteBuffer.remaining() == 0) { |
| 63 |
fillBuffer(); |
| 64 |
} |
| 65 |
return byteBuffer != null; |
| 66 |
} |
| 67 |
|
| 68 |
private void fillBuffer() throws IOException { |
| 69 |
int len = (int) Math.min(charBuffer.capacity() - charBuffer.position(), |
| 70 |
remaining); |
| 71 |
if (len > 0) { |
| 72 |
len = reader.read(charBuffer.array(), charBuffer.position(), len); |
| 73 |
} |
| 74 |
if (len > 0) { |
| 75 |
remaining -= len; |
| 76 |
} else { |
| 77 |
len = 0; |
| 78 |
remaining = 0; |
| 79 |
} |
| 80 |
length += len; |
| 81 |
charBuffer.limit(charBuffer.position() + len); |
| 82 |
charBuffer.rewind(); |
| 83 |
byteBuffer = ByteBuffer.allocate(Constants.IO_BUFFER_SIZE); |
| 84 |
boolean end = remaining == 0; |
| 85 |
encoder.encode(charBuffer, byteBuffer, end); |
| 86 |
if (end && byteBuffer.position() == 0) { |
| 87 |
// EOF |
| 88 |
byteBuffer = null; |
| 89 |
return; |
| 90 |
} |
| 91 |
byteBuffer.flip(); |
| 92 |
charBuffer.compact(); |
| 93 |
charBuffer.flip(); |
| 94 |
charBuffer.position(charBuffer.limit()); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* The number of characters read so far (but there might still be some bytes |
| 99 |
* in the buffer). |
| 100 |
* |
| 101 |
* @return the number of characters |
| 102 |
*/ |
| 103 |
public long getLength() { |
| 104 |
return length; |
| 105 |
} |
| 106 |
|
| 107 |
@Override |
| 108 |
public void close() throws IOException { |
| 109 |
reader.close(); |
| 110 |
} |
| 111 |
|
| 112 |
} |