Mercurial > hg > blitz_condensed
comparison src/com/go/trove/io/FastBufferedInputStream.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 /* | |
2 * @(#)BufferedInputStream.java 1.41 00/02/02 | |
3 * | |
4 * Copyright 1994-2000 Sun Microsystems, Inc. All Rights Reserved. | |
5 * | |
6 * This software is the proprietary information of Sun Microsystems, Inc. | |
7 * Use is subject to license terms. | |
8 * | |
9 */ | |
10 | |
11 package com.go.trove.io; | |
12 | |
13 import java.io.*; | |
14 | |
15 /** | |
16 * FastBufferedInputStream is just a slightly modified version of | |
17 * {@link java.io.BufferedInputStream}. The synchronization is gone, and so | |
18 * reads are faster. Refer to the original BufferedInputStream for | |
19 * documentation. | |
20 */ | |
21 /* @author Arthur van Hoff | |
22 * @version 1.41, 02/02/00 | |
23 * @since JDK1.0 | |
24 */ | |
25 public class FastBufferedInputStream extends FilterInputStream { | |
26 // These fields have been renamed and made private. In the original, they | |
27 // are protected. | |
28 private byte[] mBuffer; | |
29 private int mCount; | |
30 private int mPos; | |
31 private int mMarkPos = -1; | |
32 private int mMarkLimit; | |
33 | |
34 private void ensureOpen() throws IOException { | |
35 if (in == null) { | |
36 throw new IOException("Stream closed"); | |
37 } | |
38 } | |
39 | |
40 public FastBufferedInputStream(InputStream in) { | |
41 this(in, 2048); | |
42 } | |
43 | |
44 public FastBufferedInputStream(InputStream in, int size) { | |
45 super(in); | |
46 if (size <= 0) { | |
47 throw new IllegalArgumentException("Buffer size <= 0"); | |
48 } | |
49 mBuffer = new byte[size]; | |
50 } | |
51 | |
52 private void fill() throws IOException { | |
53 if (mMarkPos < 0) { | |
54 mPos = 0; | |
55 } | |
56 else if (mPos >= mBuffer.length) { | |
57 if (mMarkPos > 0) { | |
58 int sz = mPos - mMarkPos; | |
59 System.arraycopy(mBuffer, mMarkPos, mBuffer, 0, sz); | |
60 mPos = sz; | |
61 mMarkPos = 0; | |
62 } | |
63 else if (mBuffer.length >= mMarkLimit) { | |
64 mMarkPos = -1; | |
65 mPos = 0; | |
66 } | |
67 else { | |
68 int nsz = mPos * 2; | |
69 if (nsz > mMarkLimit) { | |
70 nsz = mMarkLimit; | |
71 } | |
72 byte nbuf[] = new byte[nsz]; | |
73 System.arraycopy(mBuffer, 0, nbuf, 0, mPos); | |
74 mBuffer = nbuf; | |
75 } | |
76 } | |
77 mCount = mPos; | |
78 int n = in.read(mBuffer, mPos, mBuffer.length - mPos); | |
79 if (n > 0) { | |
80 mCount = n + mPos; | |
81 } | |
82 } | |
83 | |
84 public int read() throws IOException { | |
85 ensureOpen(); | |
86 if (mPos >= mCount) { | |
87 fill(); | |
88 if (mPos >= mCount) { | |
89 return -1; | |
90 } | |
91 } | |
92 return mBuffer[mPos++] & 0xff; | |
93 } | |
94 | |
95 private int read1(byte[] b, int off, int len) throws IOException { | |
96 int avail = mCount - mPos; | |
97 if (avail <= 0) { | |
98 if (len >= mBuffer.length && mMarkPos < 0) { | |
99 return in.read(b, off, len); | |
100 } | |
101 fill(); | |
102 avail = mCount - mPos; | |
103 if (avail <= 0) { | |
104 return -1; | |
105 } | |
106 } | |
107 int cnt = (avail < len) ? avail : len; | |
108 System.arraycopy(mBuffer, mPos, b, off, cnt); | |
109 mPos += cnt; | |
110 return cnt; | |
111 } | |
112 | |
113 public int read(byte b[], int off, int len) throws IOException { | |
114 ensureOpen(); | |
115 if ((off | len | (off + len) | (b.length - (off + len))) < 0) { | |
116 throw new IndexOutOfBoundsException(); | |
117 } | |
118 else if (len == 0) { | |
119 return 0; | |
120 } | |
121 | |
122 int n = read1(b, off, len); | |
123 if (n <= 0) { | |
124 return n; | |
125 } | |
126 while ((n < len) && (in.available() > 0)) { | |
127 int n1 = read1(b, off + n, len - n); | |
128 if (n1 <= 0) { | |
129 break; | |
130 } | |
131 n += n1; | |
132 } | |
133 return n; | |
134 } | |
135 | |
136 public long skip(long n) throws IOException { | |
137 ensureOpen(); | |
138 if (n <= 0) { | |
139 return 0; | |
140 } | |
141 long avail = mCount - mPos; | |
142 | |
143 if (avail <= 0) { | |
144 if (mMarkPos <0) { | |
145 return in.skip(n); | |
146 } | |
147 | |
148 fill(); | |
149 avail = mCount - mPos; | |
150 if (avail <= 0) { | |
151 return 0; | |
152 } | |
153 } | |
154 | |
155 long skipped = (avail < n) ? avail : n; | |
156 mPos += skipped; | |
157 return skipped; | |
158 } | |
159 | |
160 public int available() throws IOException { | |
161 ensureOpen(); | |
162 return (mCount - mPos) + in.available(); | |
163 } | |
164 | |
165 public void mark(int readlimit) { | |
166 mMarkLimit = readlimit; | |
167 mMarkPos = mPos; | |
168 } | |
169 | |
170 public void reset() throws IOException { | |
171 ensureOpen(); | |
172 if (mMarkPos < 0) { | |
173 throw new IOException("Resetting to invalid mark"); | |
174 } | |
175 mPos = mMarkPos; | |
176 } | |
177 | |
178 public boolean markSupported() { | |
179 return true; | |
180 } | |
181 | |
182 public void close() throws IOException { | |
183 if (in != null) { | |
184 in.close(); | |
185 in = null; | |
186 mBuffer = null; | |
187 } | |
188 } | |
189 } |