下面列出了java.nio.charset.CharsetDecoder# charset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
StreamDecoder(InputStream in, Object lock, CharsetDecoder dec) {
super(lock);
this.cs = dec.charset();
this.decoder = dec;
// This path disabled until direct buffers are faster
if (false && in instanceof FileInputStream) {
ch = getChannel((FileInputStream)in);
if (ch != null)
bb = ByteBuffer.allocateDirect(DEFAULT_BYTE_BUFFER_SIZE);
}
if (ch == null) {
this.in = in;
this.ch = null;
bb = ByteBuffer.allocate(DEFAULT_BYTE_BUFFER_SIZE);
}
bb.flip(); // So that bb is initially empty
}
private Def checkBig(FileObject fileObject, File file,
SearchListener listener) {
Charset charset = FileEncodingQuery.getEncoding(fileObject);
CharsetDecoder decoder = prepareDecoder(charset);
LongCharSequence longSequence = null;
try {
longSequence = new LongCharSequence(file, charset);
List<TextDetail> textDetails = multiline
? matchWholeFile(longSequence, fileObject)
: matchLines(longSequence, fileObject);
if (textDetails == null) {
return null;
} else {
Def def = new Def(fileObject, decoder.charset(), textDetails);
return def;
}
} catch (Exception ex) {
listener.generalError(ex);
return null;
} finally {
if (longSequence != null) {
longSequence.close();
}
}
}
StreamDecoder(ReadableByteChannel ch, CharsetDecoder dec, int mbc) {
this.in = null;
this.ch = ch;
this.decoder = dec;
this.cs = dec.charset();
this.bb = ByteBuffer.allocate(mbc < 0
? DEFAULT_BYTE_BUFFER_SIZE
: (mbc < MIN_BYTE_BUFFER_SIZE
? MIN_BYTE_BUFFER_SIZE
: mbc));
bb.flip();
}
@Override
protected Def checkMeasuredInternal(FileObject fo,
SearchListener listener) {
MappedByteBuffer bb = null;
FileChannel fc = null;
try {
listener.fileContentMatchingStarted(fo.getPath());
File file = FileUtil.toFile(fo);
// Open the file and then get a channel from the stream
FileInputStream fis = new FileInputStream(file);
fc = fis.getChannel();
// Get the file's size and then map it into memory
int sz = (int) fc.size();
bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
// if (asciiPattern && !matchesIgnoringEncoding(bb)) {
// return null;
//}
// Decode the file into a char buffer
Charset charset = FileEncodingQuery.getEncoding(fo);
CharsetDecoder decoder = prepareDecoder(charset);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
CharBuffer cb = decoder.decode(bb);
List<TextDetail> textDetails = matchWholeFile(cb, fo);
if (textDetails == null) {
return null;
} else {
Def def = new Def(fo, decoder.charset(), textDetails);
return def;
}
} catch (Exception e) {
listener.generalError(e);
return null;
} finally {
if (fc != null) {
try {
fc.close();
} catch (IOException ex) {
listener.generalError(ex);
}
}
MatcherUtils.unmap(bb);
}
}
/**
* Check file content using Java NIO API.
*/
private Def checkSmall(FileObject fo, File file,
SearchListener listener) {
MappedByteBuffer bb = null;
FileChannel fc = null;
try {
// Open the file and then get a channel from the stream
FileInputStream fis = new FileInputStream(file);
fc = fis.getChannel();
// Get the file's size and then map it into memory
int sz = (int) fc.size();
bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
if (asciiPattern && !matchesIgnoringEncoding(bb)) {
return null;
}
// Decode the file into a char buffer
Charset charset = FileEncodingQuery.getEncoding(fo);
CharsetDecoder decoder = prepareDecoder(charset);
decoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
CharBuffer cb = decoder.decode(bb);
List<TextDetail> textDetails = multiline
? matchWholeFile(cb, fo)
: matchLines(cb, fo);
if (textDetails == null) {
return null;
} else {
Def def = new Def(fo, decoder.charset(), textDetails);
return def;
}
} catch (Exception e) {
listener.generalError(e);
return null;
} finally {
if (fc != null) {
try {
fc.close();
} catch (IOException ex) {
listener.generalError(ex);
}
}
unmap(bb);
}
}