Lines 42-57
Link Here
|
42 |
|
42 |
|
43 |
package org.netbeans.modules.search; |
43 |
package org.netbeans.modules.search; |
44 |
|
44 |
|
45 |
import java.io.FileInputStream; |
45 |
import java.io.BufferedInputStream; |
46 |
import java.io.IOException; |
46 |
import java.io.IOException; |
|
|
47 |
import java.io.InputStream; |
47 |
import java.nio.ByteBuffer; |
48 |
import java.nio.ByteBuffer; |
48 |
import java.nio.CharBuffer; |
49 |
import java.nio.CharBuffer; |
49 |
import java.nio.channels.FileChannel; |
|
|
50 |
import java.nio.charset.Charset; |
50 |
import java.nio.charset.Charset; |
51 |
import java.nio.charset.CharsetDecoder; |
51 |
import java.nio.charset.CharsetDecoder; |
52 |
import java.nio.charset.CoderResult; |
52 |
import java.nio.charset.CoderResult; |
53 |
import java.nio.charset.CodingErrorAction; |
53 |
import java.nio.charset.CodingErrorAction; |
54 |
import java.util.logging.Level; |
|
|
55 |
import java.util.logging.Logger; |
54 |
import java.util.logging.Logger; |
56 |
import sun.nio.cs.ThreadLocalCoders; |
55 |
import sun.nio.cs.ThreadLocalCoders; |
57 |
|
56 |
|
Lines 138-180
Link Here
|
138 |
|
137 |
|
139 |
/** |
138 |
/** |
140 |
* Creates {@code BufferedCharSequence} for the specified {@code stream}. |
139 |
* Creates {@code BufferedCharSequence} for the specified {@code stream}. |
141 |
* @param stream is a stream that will be buffered and represented as a |
140 |
* @param stream is a stream that will be buffered and represented as a |
142 |
* {@code CharSequence}. |
141 |
* {@code CharSequence}. |
143 |
* @param charset is a named mapping that will be used to decode a sequence |
142 |
* @param charset is a named mapping that will be used to decode a sequence |
144 |
* of bytes from the {@code stream}. |
143 |
* of bytes from the {@code stream}. |
|
|
144 |
* @param size is the size of the file. |
145 |
*/ |
145 |
*/ |
146 |
public BufferedCharSequence(final FileInputStream stream, Charset charset) { |
146 |
public BufferedCharSequence(final InputStream stream, Charset charset, long size) { |
147 |
this(stream.getChannel(), charset); |
|
|
148 |
} |
149 |
|
150 |
/** |
151 |
* Creates {@code BufferedCharSequence} for the specified {@code channel}. |
152 |
* @param channel is a channel that will be buffered and represented as a |
153 |
* {@code CharSequence}. |
154 |
* @param charset is a named mapping that will be used to decode a sequence |
155 |
* of bytes from the {@code channel}. |
156 |
*/ |
157 |
public BufferedCharSequence(final FileChannel channel, Charset charset) { |
158 |
// TODO charset.name() is used instead of charset due to a bug in the |
147 |
// TODO charset.name() is used instead of charset due to a bug in the |
159 |
// org.netbeans.api.queries.FileEncodingQuery.ProxyCharset.ProxyDecoder |
148 |
// org.netbeans.api.queries.FileEncodingQuery.ProxyCharset.ProxyDecoder |
160 |
// The IllegalStateException may be thrown after correct actions. |
149 |
// The IllegalStateException may be thrown after correct actions. |
161 |
// See #169804 |
150 |
// See #169804 |
162 |
this(channel, |
151 |
this(stream, |
163 |
ThreadLocalCoders.decoderFor(charset.name()) |
152 |
ThreadLocalCoders.decoderFor(charset.name()) |
164 |
.onMalformedInput(CodingErrorAction.REPLACE) |
153 |
.onMalformedInput(CodingErrorAction.REPLACE) |
165 |
.onUnmappableCharacter(CodingErrorAction.REPLACE)); |
154 |
.onUnmappableCharacter(CodingErrorAction.REPLACE), size); |
166 |
} |
155 |
} |
167 |
|
156 |
|
168 |
/** |
157 |
|
169 |
* Creates {@code BufferedCharSequence} for the specified {@code channel}. |
158 |
public BufferedCharSequence(final InputStream stream, |
170 |
* @param channel is a channel that will be buffered and represented as a |
159 |
CharsetDecoder decoder, long size) { |
171 |
* {@code CharSequence}. |
160 |
this.source = new Source(stream, size); |
172 |
* @param decoder is a decoder that will be used to decode a sequence |
|
|
173 |
* of bytes from the {@code channel}. |
174 |
*/ |
175 |
public BufferedCharSequence(final FileChannel channel, |
176 |
CharsetDecoder decoder) { |
177 |
this.source = new Source(channel); |
178 |
this.decoder = decoder; |
161 |
this.decoder = decoder; |
179 |
this.sink = new Sink(this.source); |
162 |
this.sink = new Sink(this.source); |
180 |
LOG.finer("<init> " + this.source + "; decoder = " + this.decoder + |
163 |
LOG.finer("<init> " + this.source + "; decoder = " + this.decoder + |
Lines 232-238
Link Here
|
232 |
* @return the underlying instance of this class. |
215 |
* @return the underlying instance of this class. |
233 |
* @throws SourceIOException |
216 |
* @throws SourceIOException |
234 |
*/ |
217 |
*/ |
235 |
public BufferedCharSequence reset() throws SourceIOException { |
218 |
public BufferedCharSequence reset() throws SourceIOException { |
236 |
source.reset(); |
219 |
source.reset(); |
237 |
sink.reset(); |
220 |
sink.reset(); |
238 |
decoder.reset(); |
221 |
decoder.reset(); |
Lines 274-280
Link Here
|
274 |
reset(); |
257 |
reset(); |
275 |
int length = 0; |
258 |
int length = 0; |
276 |
while(sink.next()) { } |
259 |
while(sink.next()) { } |
277 |
length = sink.buffer.scope.end; |
260 |
length = sink.buffer.scope.end; |
278 |
return length; |
261 |
return length; |
279 |
} |
262 |
} |
280 |
|
263 |
|
Lines 409-421
Link Here
|
409 |
*/ |
392 |
*/ |
410 |
public String getLineText(int start) { |
393 |
public String getLineText(int start) { |
411 |
int oldPosition = changePosition(start); |
394 |
int oldPosition = changePosition(start); |
412 |
String text = nextLineText(); |
395 |
String text = nextLineText(); |
413 |
changePosition(oldPosition); |
396 |
changePosition(oldPosition); |
414 |
return text; |
397 |
return text; |
415 |
} |
398 |
} |
416 |
|
399 |
|
417 |
private char getCharAt(int index) throws IndexOutOfBoundsException { |
400 |
private char getCharAt(int index) throws IndexOutOfBoundsException { |
418 |
if(sink.buffer.scope.isBefore(index)) { |
401 |
if(sink.buffer.scope.isBefore(index)) { |
419 |
reset(); |
402 |
reset(); |
420 |
} |
403 |
} |
421 |
while(!sink.buffer.scope.isInside(index)) { |
404 |
while(!sink.buffer.scope.isInside(index)) { |
Lines 424-430
Link Here
|
424 |
throw new IndexOutOfBoundsException("index is " + |
407 |
throw new IndexOutOfBoundsException("index is " + |
425 |
index + " > lenght"); // NOI18N |
408 |
index + " > lenght"); // NOI18N |
426 |
} |
409 |
} |
427 |
} |
410 |
} |
428 |
return sink.charAt(index); |
411 |
return sink.charAt(index); |
429 |
} |
412 |
} |
430 |
|
413 |
|
Lines 466-535
Link Here
|
466 |
/** |
449 |
/** |
467 |
* The source buffer. |
450 |
* The source buffer. |
468 |
*/ |
451 |
*/ |
469 |
private class Source { |
452 |
private class Source { |
470 |
private int maxBufferSize = MAX_SOURCE_BUFFER_SIZE; |
453 |
private int maxBufferSize = MAX_SOURCE_BUFFER_SIZE; |
471 |
|
454 |
|
472 |
private ByteBuffer buffer; |
455 |
private ByteBuffer buffer; |
473 |
private FileChannel channel; |
456 |
private BufferedInputStream bstream; |
|
|
457 |
private int bufferSize; |
474 |
|
458 |
|
475 |
public Source(FileChannel channel) { |
459 |
public Source(InputStream inputStream, long bufferSize) { |
476 |
this.channel = channel; |
460 |
this.bstream = new BufferedInputStream(inputStream); |
|
|
461 |
this.bstream.mark(Integer.MAX_VALUE); |
462 |
this.bufferSize = getBufferSize(bufferSize); |
477 |
buffer = newBuffer(); |
463 |
buffer = newBuffer(); |
478 |
} |
464 |
} |
479 |
|
465 |
|
480 |
@Override |
466 |
@Override |
481 |
public String toString() { |
467 |
public String toString() { |
482 |
return "source=[channel = "+channel+", buffer = "+buffer+"]"; |
468 |
return "source=[stream = " + bstream.toString() + ", buffer = " + buffer + "]"; |
483 |
} |
469 |
} |
484 |
|
470 |
|
485 |
private ByteBuffer newBuffer() { |
471 |
private ByteBuffer newBuffer() { |
486 |
return ByteBuffer.allocate(getBufferSize()); |
472 |
return ByteBuffer.allocate(bufferSize); |
487 |
} |
473 |
} |
488 |
|
474 |
|
489 |
public void reset() { |
475 |
public void reset() { |
490 |
try { |
476 |
try { |
491 |
channel.position(0); |
477 |
bstream.reset(); |
492 |
} catch (IOException ex) { |
478 |
} catch (IOException ex) { |
493 |
throw new SourceIOException(ex); |
479 |
throw new SourceIOException(ex); |
494 |
} |
480 |
} |
495 |
buffer.clear(); |
481 |
buffer.clear(); |
496 |
} |
482 |
} |
497 |
|
483 |
|
498 |
/** |
484 |
/** |
499 |
* Reads a sequence of bytes from the source channel. Bytes are read |
485 |
* Reads a sequence of bytes from the source stream. Bytes are read |
500 |
* starting at the channel's current file position, and then the |
486 |
* starting at the stream's current position, and then the |
501 |
* position is updated with the number of bytes actually read. |
487 |
* position is updated with the number of bytes actually read. |
502 |
* |
488 |
* |
503 |
* @return The number of bytes read, possibly zero, or -1 if the channel |
489 |
* @return The number of bytes read, possibly zero, or -1 if |
504 |
* has reached end-of-stream |
490 |
* the end-of-stream is reached |
505 |
* |
491 |
* |
506 |
* @throws ProcessException If some other I/O error occurs |
492 |
* @throws ProcessException If some other I/O error occurs |
507 |
*/ |
493 |
*/ |
508 |
private int read() { |
494 |
private int read() { |
509 |
try { |
495 |
try { |
510 |
return channel.read(buffer); |
496 |
if(buffer.hasArray()) { |
|
|
497 |
int res = bstream.read(buffer.array()); |
498 |
if(res > 0) { |
499 |
buffer.position(res); |
500 |
} |
501 |
return res; |
502 |
} |
503 |
throw new IOException("No byte array"); |
511 |
} catch (IOException ex) { |
504 |
} catch (IOException ex) { |
512 |
throw new SourceIOException(ex); |
505 |
throw new SourceIOException(ex); |
513 |
} |
506 |
} |
514 |
} |
507 |
} |
515 |
|
508 |
|
516 |
public int position() throws IOException { |
509 |
public void close() throws IOException { |
517 |
return (int) channel.position(); |
510 |
bstream.close(); |
518 |
} |
511 |
} |
519 |
|
512 |
|
520 |
public void close() throws IOException { |
513 |
public int getSize(long size) { |
521 |
channel.close(); |
|
|
522 |
} |
523 |
|
524 |
public int size() { |
525 |
try { |
526 |
return getSize(channel.size()); |
527 |
} catch (IOException ioe) { |
528 |
throw new SourceIOException(ioe); |
529 |
} |
530 |
} |
531 |
|
532 |
private int getSize(long size) { |
533 |
if (size > Integer.MAX_VALUE) { |
514 |
if (size > Integer.MAX_VALUE) { |
534 |
LOG.warning("File size is " + size + "bytes. " + |
515 |
LOG.warning("File size is " + size + "bytes. " + |
535 |
"Only first " + MAX_FILE_SIZE + |
516 |
"Only first " + MAX_FILE_SIZE + |
Lines 539-546
Link Here
|
539 |
return (int) size; |
520 |
return (int) size; |
540 |
} |
521 |
} |
541 |
|
522 |
|
542 |
private int getBufferSize() { |
523 |
private int getBufferSize(long bufferSize) { |
543 |
int size = Math.min(size(), maxBufferSize); |
524 |
int size = Math.min(getSize(bufferSize), maxBufferSize); |
544 |
return size; |
525 |
return size; |
545 |
} |
526 |
} |
546 |
|
527 |
|
Lines 548-557
Link Here
|
548 |
* |
529 |
* |
549 |
* @return {@code true} if EOF, otherwise {@code false}. |
530 |
* @return {@code true} if EOF, otherwise {@code false}. |
550 |
*/ |
531 |
*/ |
551 |
public boolean readNext() { |
532 |
public boolean readNext() { |
552 |
buffer.clear(); |
533 |
buffer.clear(); |
553 |
int status = read(); |
534 |
int status = read(); |
554 |
buffer.flip(); |
535 |
buffer.flip(); |
555 |
return status == -1; |
536 |
return status == -1; |
556 |
} |
537 |
} |
557 |
|
538 |
|
Lines 578-584
Link Here
|
578 |
return "sink = [" + buffer + "]"; |
559 |
return "sink = [" + buffer + "]"; |
579 |
} |
560 |
} |
580 |
|
561 |
|
581 |
public void reset() { |
562 |
public void reset() { |
582 |
buffer.reset(); |
563 |
buffer.reset(); |
583 |
} |
564 |
} |
584 |
|
565 |
|
Lines 595-601
Link Here
|
595 |
* @return {@code true} is successful, otherwise {@code false}. |
576 |
* @return {@code true} is successful, otherwise {@code false}. |
596 |
*/ |
577 |
*/ |
597 |
private boolean next() { |
578 |
private boolean next() { |
598 |
CharBuffer out = buffer.clear(); |
579 |
CharBuffer out = buffer.clear(); |
599 |
boolean endOfInput = false; |
580 |
boolean endOfInput = false; |
600 |
if(coderResult == CoderResult.UNDERFLOW) { |
581 |
if(coderResult == CoderResult.UNDERFLOW) { |
601 |
endOfInput = source.readNext(); |
582 |
endOfInput = source.readNext(); |
Lines 603-609
Link Here
|
603 |
while((coderResult = |
584 |
while((coderResult = |
604 |
decoder.decode(source.buffer, out, endOfInput)) |
585 |
decoder.decode(source.buffer, out, endOfInput)) |
605 |
!= CoderResult.UNDERFLOW) { |
586 |
!= CoderResult.UNDERFLOW) { |
606 |
out = buffer.growBuffer(); |
587 |
out = buffer.growBuffer(); |
607 |
} |
588 |
} |
608 |
if(endOfInput) { |
589 |
if(endOfInput) { |
609 |
while((coderResult = decoder.flush(out)) |
590 |
while((coderResult = decoder.flush(out)) |
Lines 676-682
Link Here
|
676 |
return c; |
657 |
return c; |
677 |
} |
658 |
} |
678 |
|
659 |
|
679 |
private void reset() { |
660 |
private void reset() { |
680 |
scope.reset(); |
661 |
scope.reset(); |
681 |
charBuffer.clear(); |
662 |
charBuffer.clear(); |
682 |
} |
663 |
} |
Lines 690-699
Link Here
|
690 |
return charBuffer; |
671 |
return charBuffer; |
691 |
} |
672 |
} |
692 |
|
673 |
|
693 |
private void adjustScope() { |
674 |
private void adjustScope() { |
694 |
scope.start = scope.end == -1 ? 0 : scope.end; |
675 |
scope.start = scope.end == -1 ? 0 : scope.end; |
695 |
flip(); |
676 |
flip(); |
696 |
scope.end = scope.start + charBuffer.limit(); |
677 |
scope.end = scope.start + charBuffer.limit(); |
697 |
} |
678 |
} |
698 |
|
679 |
|
699 |
/** |
680 |
/** |