Java Code Examples for java.nio.charset.CharsetDecoder#charset()

The following examples show how to use java.nio.charset.CharsetDecoder#charset() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StreamDecoder.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
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
}
 
Example 2
Source File: FastMatcher.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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();
        }
    }
}
 
Example 3
Source File: StreamDecoder.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: MultiLineMappedMatcherSmall.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@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);
    }
}
 
Example 5
Source File: FastMatcher.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * 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);
    }
}