3131
3232package org .scijava .io ;
3333
34- import java .io .IOException ;
35- import java .io .InputStreamReader ;
36- import java .nio .ByteBuffer ;
3734import java .nio .ByteOrder ;
3835
3936import org .scijava .plugin .AbstractWrapperPlugin ;
@@ -47,14 +44,6 @@ public abstract class AbstractDataHandle<L extends Location> extends
4744 AbstractWrapperPlugin <L > implements DataHandle <L >
4845{
4946
50- // -- Constants --
51-
52- /** Block size to use when searching through the stream. */
53- private static final int DEFAULT_BLOCK_SIZE = 256 * 1024 ; // 256 KB
54-
55- /** Maximum number of bytes to search when searching through the stream. */
56- private static final int MAX_SEARCH_SIZE = 512 * 1024 * 1024 ; // 512 MB
57-
5847 // -- Fields --
5948
6049 private ByteOrder order = ByteOrder .BIG_ENDIAN ;
@@ -67,21 +56,11 @@ public ByteOrder getOrder() {
6756 return order ;
6857 }
6958
70- @ Override
71- public boolean isLittleEndian () {
72- return getOrder () == ByteOrder .LITTLE_ENDIAN ;
73- }
74-
7559 @ Override
7660 public void setOrder (final ByteOrder order ) {
7761 this .order = order ;
7862 }
7963
80- @ Override
81- public void setOrder (final boolean little ) {
82- setOrder (little ? ByteOrder .LITTLE_ENDIAN : ByteOrder .BIG_ENDIAN );
83- }
84-
8564 @ Override
8665 public String getEncoding () {
8766 return encoding ;
@@ -92,184 +71,4 @@ public void setEncoding(final String encoding) {
9271 this .encoding = encoding ;
9372 }
9473
95- @ Override
96- public int read (final ByteBuffer buf ) throws IOException {
97- return read (buf , buf .remaining ());
98- }
99-
100- @ Override
101- public int read (final ByteBuffer buf , final int len )
102- throws IOException
103- {
104- final int n ;
105- if (buf .hasArray ()) {
106- // read directly into the array
107- n = read (buf .array (), buf .arrayOffset (), len );
108- }
109- else {
110- // read into a temporary array, then copy
111- final byte [] b = new byte [len ];
112- n = read (b );
113- buf .put (b , 0 , n );
114- }
115- return n ;
116- }
117-
118- @ Override
119- public void write (final ByteBuffer buf ) throws IOException {
120- write (buf , buf .remaining ());
121- }
122-
123- @ Override
124- public void write (final ByteBuffer buf , final int len )
125- throws IOException
126- {
127- if (buf .hasArray ()) {
128- // write directly from the buffer's array
129- write (buf .array (), buf .arrayOffset (), len );
130- }
131- else {
132- // copy into a temporary array, then write
133- final byte [] b = new byte [len ];
134- buf .get (b );
135- write (b );
136- }
137- }
138-
139- @ Override
140- public String readCString () throws IOException {
141- final String line = findString ("\0 " );
142- return line .length () == 0 ? null : line ;
143- }
144-
145- @ Override
146- public String readString (int n ) throws IOException {
147- final long avail = length () - offset ();
148- if (n > avail ) n = (int ) avail ;
149- final byte [] b = new byte [n ];
150- readFully (b );
151- return new String (b , encoding );
152- }
153-
154- @ Override
155- public String readString (final String lastChars ) throws IOException {
156- if (lastChars .length () == 1 ) return findString (lastChars );
157- final String [] terminators = new String [lastChars .length ()];
158- for (int i = 0 ; i < terminators .length ; i ++) {
159- terminators [i ] = lastChars .substring (i , i + 1 );
160- }
161- return findString (terminators );
162- }
163-
164- @ Override
165- public String findString (final String ... terminators ) throws IOException {
166- return findString (true , DEFAULT_BLOCK_SIZE , terminators );
167- }
168-
169- @ Override
170- public String findString (final boolean saveString ,
171- final String ... terminators ) throws IOException
172- {
173- return findString (saveString , DEFAULT_BLOCK_SIZE , terminators );
174- }
175-
176- @ Override
177- public String findString (final int blockSize , final String ... terminators )
178- throws IOException
179- {
180- return findString (true , blockSize , terminators );
181- }
182-
183- @ Override
184- public String findString (final boolean saveString , final int blockSize ,
185- final String ... terminators ) throws IOException
186- {
187- final StringBuilder out = new StringBuilder ();
188- final long startPos = offset ();
189- long bytesDropped = 0 ;
190- final long inputLen = length ();
191- long maxLen = inputLen - startPos ;
192- final boolean tooLong = saveString && maxLen > MAX_SEARCH_SIZE ;
193- if (tooLong ) maxLen = MAX_SEARCH_SIZE ;
194- boolean match = false ;
195- int maxTermLen = 0 ;
196- for (final String term : terminators ) {
197- final int len = term .length ();
198- if (len > maxTermLen ) maxTermLen = len ;
199- }
200-
201- @ SuppressWarnings ("resource" )
202- final InputStreamReader in =
203- new InputStreamReader (new DataHandleInputStream <>(this ), getEncoding ());
204- final char [] buf = new char [blockSize ];
205- long loc = 0 ;
206- while (loc < maxLen && offset () < length () - 1 ) {
207- // if we're not saving the string, drop any old, unnecessary output
208- if (!saveString ) {
209- final int outLen = out .length ();
210- if (outLen >= maxTermLen ) {
211- final int dropIndex = outLen - maxTermLen + 1 ;
212- final String last = out .substring (dropIndex , outLen );
213- out .setLength (0 );
214- out .append (last );
215- bytesDropped += dropIndex ;
216- }
217- }
218-
219- // read block from stream
220- final int r = in .read (buf , 0 , blockSize );
221- if (r <= 0 ) throw new IOException ("Cannot read from stream: " + r );
222-
223- // append block to output
224- out .append (buf , 0 , r );
225-
226- // check output, returning smallest possible string
227- int min = Integer .MAX_VALUE , tagLen = 0 ;
228- for (final String t : terminators ) {
229- final int len = t .length ();
230- final int start = (int ) (loc - bytesDropped - len );
231- final int value = out .indexOf (t , start < 0 ? 0 : start );
232- if (value >= 0 && value < min ) {
233- match = true ;
234- min = value ;
235- tagLen = len ;
236- }
237- }
238-
239- if (match ) {
240- // reset stream to proper location
241- seek (startPos + bytesDropped + min + tagLen );
242-
243- // trim output string
244- if (saveString ) {
245- out .setLength (min + tagLen );
246- return out .toString ();
247- }
248- return null ;
249- }
250-
251- loc += r ;
252- }
253-
254- // no match
255- if (tooLong ) throw new IOException ("Maximum search length reached." );
256- return saveString ? out .toString () : null ;
257- }
258-
259- // -- InputStream look-alikes --
260-
261- @ Override
262- public int read (byte [] b ) throws IOException {
263- return read (b , 0 , b .length );
264- }
265-
266- @ Override
267- public long skip (final long n ) throws IOException {
268- if (n < 0 ) return 0 ;
269- final long remain = length () - offset ();
270- final long num = n < remain ? n : remain ;
271- seek (offset () + num );
272- return num ;
273- }
274-
27574}
0 commit comments