| 1 | package net.minecraft.util;
|
| 2 |
|
| 3 | import java.io.IOException;
|
| 4 | import java.io.InputStream;
|
| 5 |
|
| 6 | public class FastBufferedInputStream extends InputStream {
|
| 7 | private static final int DEFAULT_BUFFER_SIZE = 8192;
|
| 8 | private final InputStream in;
|
| 9 | private final byte[] buffer;
|
| 10 | private int limit;
|
| 11 | private int position;
|
| 12 |
|
| 13 | public FastBufferedInputStream(final InputStream in) {
|
| 14 | this(in, 8192);
|
| 15 | }
|
| 16 |
|
| 17 | public FastBufferedInputStream(final InputStream in, final int bufferSize) {
|
| 18 | this.in = in;
|
| 19 | this.buffer = new byte[bufferSize];
|
| 20 | }
|
| 21 |
|
| 22 | @Override
|
| 23 | public int read() throws IOException {
|
| 24 | if (this.position >= this.limit) {
|
| 25 | this.fill();
|
| 26 | if (this.position >= this.limit) {
|
| 27 | return -1;
|
| 28 | }
|
| 29 | }
|
| 30 |
|
| 31 | return Byte.toUnsignedInt(this.buffer[this.position++]);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public int read(final byte[] output, final int offset, int length) throws IOException {
|
| 36 | int availableInBuffer = this.bytesInBuffer();
|
| 37 | if (availableInBuffer <= 0) {
|
| 38 | if (length >= this.buffer.length) {
|
| 39 | return this.in.read(output, offset, length);
|
| 40 | }
|
| 41 |
|
| 42 | this.fill();
|
| 43 | availableInBuffer = this.bytesInBuffer();
|
| 44 | if (availableInBuffer <= 0) {
|
| 45 | return -1;
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | if (length > availableInBuffer) {
|
| 50 | length = availableInBuffer;
|
| 51 | }
|
| 52 |
|
| 53 | System.arraycopy(this.buffer, this.position, output, offset, length);
|
| 54 | this.position += length;
|
| 55 | return length;
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public long skip(long count) throws IOException {
|
| 60 | if (count <= 0L) {
|
| 61 | return 0L;
|
| 62 | }
|
| 63 |
|
| 64 | long availableInBuffer = this.bytesInBuffer();
|
| 65 | if (availableInBuffer <= 0L) {
|
| 66 | return this.in.skip(count);
|
| 67 | }
|
| 68 |
|
| 69 | if (count > availableInBuffer) {
|
| 70 | count = availableInBuffer;
|
| 71 | }
|
| 72 |
|
| 73 | this.position = (int)(this.position + count);
|
| 74 | return count;
|
| 75 | }
|
| 76 |
|
| 77 | @Override
|
| 78 | public int available() throws IOException {
|
| 79 | return this.bytesInBuffer() + this.in.available();
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public void close() throws IOException {
|
| 84 | this.in.close();
|
| 85 | }
|
| 86 |
|
| 87 | private int bytesInBuffer() {
|
| 88 | return this.limit - this.position;
|
| 89 | }
|
| 90 |
|
| 91 | private void fill() throws IOException {
|
| 92 | this.limit = 0;
|
| 93 | this.position = 0;
|
| 94 | int actuallyRead = this.in.read(this.buffer, 0, this.buffer.length);
|
| 95 | if (actuallyRead > 0) {
|
| 96 | this.limit = actuallyRead;
|
| 97 | }
|
| 98 | }
|
| 99 | }
|
| 100 |
|