Search in sources :

Example 1 with CodingErrorAction

use of java.nio.charset.CodingErrorAction in project lombok by rzwitserloot.

the class EmptyLombokFileObject method getDecoder.

@Override
public CharsetDecoder getDecoder(boolean ignoreEncodingErrors) {
    CharsetDecoder decoder = Charset.forName("UTF-8").newDecoder();
    CodingErrorAction action = ignoreEncodingErrors ? CodingErrorAction.REPLACE : CodingErrorAction.REPORT;
    return decoder.onMalformedInput(action).onUnmappableCharacter(action);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CodingErrorAction(java.nio.charset.CodingErrorAction)

Example 2 with CodingErrorAction

use of java.nio.charset.CodingErrorAction in project ceylon by eclipse.

the class BaseFileManager method getDecoder.

public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
    Charset cs = (this.charset == null) ? Charset.forName(encodingName) : this.charset;
    CharsetDecoder decoder = cs.newDecoder();
    CodingErrorAction action;
    if (ignoreEncodingErrors)
        action = CodingErrorAction.REPLACE;
    else
        action = CodingErrorAction.REPORT;
    return decoder.onMalformedInput(action).onUnmappableCharacter(action);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CodingErrorAction(java.nio.charset.CodingErrorAction) Charset(java.nio.charset.Charset)

Example 3 with CodingErrorAction

use of java.nio.charset.CodingErrorAction in project ceylon-compiler by ceylon.

the class BaseFileManager method getDecoder.

public CharsetDecoder getDecoder(String encodingName, boolean ignoreEncodingErrors) {
    Charset cs = (this.charset == null) ? Charset.forName(encodingName) : this.charset;
    CharsetDecoder decoder = cs.newDecoder();
    CodingErrorAction action;
    if (ignoreEncodingErrors)
        action = CodingErrorAction.REPLACE;
    else
        action = CodingErrorAction.REPORT;
    return decoder.onMalformedInput(action).onUnmappableCharacter(action);
}
Also used : CharsetDecoder(java.nio.charset.CharsetDecoder) CodingErrorAction(java.nio.charset.CodingErrorAction) Charset(java.nio.charset.Charset)

Example 4 with CodingErrorAction

use of java.nio.charset.CodingErrorAction in project org.alloytools.alloy by AlloyTools.

the class Util method readAll.

/**
 * Read everything into a String; throws IOException if an error occurred. (If
 * filename begins with Util.jarPrefix() then we read from the JAR instead)
 */
public static String readAll(String filename) throws FileNotFoundException, IOException {
    String JAR = jarPrefix();
    boolean fromJar = false;
    if (filename.startsWith(JAR)) {
        fromJar = true;
        filename = filename.substring(JAR.length()).replace('\\', '/');
    }
    InputStream fis = null;
    int now = 0, max = 4096;
    if (!fromJar) {
        long maxL = new File(filename).length();
        max = (int) maxL;
        if (max != maxL)
            throw new IOException("File too big to fit in memory");
    }
    byte[] buf;
    try {
        buf = new byte[max];
        fis = fromJar ? Util.class.getClassLoader().getResourceAsStream(filename) : new FileInputStream(filename);
        if (fis == null)
            throw new FileNotFoundException("File \"" + filename + "\" cannot be found");
        while (true) {
            if (now >= max) {
                max = now + 4096;
                if (max < now)
                    throw new IOException("File too big to fit in memory");
                byte[] buf2 = new byte[max];
                if (now > 0)
                    System.arraycopy(buf, 0, buf2, 0, now);
                buf = buf2;
            }
            int r = fis.read(buf, now, max - now);
            if (r < 0)
                break;
            now = now + r;
        }
    } catch (OutOfMemoryError ex) {
        System.gc();
        throw new IOException("There is insufficient memory.");
    } finally {
        close(fis);
    }
    CodingErrorAction r = CodingErrorAction.REPORT;
    CodingErrorAction i = CodingErrorAction.IGNORE;
    ByteBuffer bbuf;
    String ans = "";
    try {
        // We first try UTF-8;
        bbuf = ByteBuffer.wrap(buf, 0, now);
        ans = Charset.forName("UTF-8").newDecoder().onMalformedInput(r).onUnmappableCharacter(r).decode(bbuf).toString();
    } catch (CharacterCodingException ex) {
        try {
            // if that fails, we try using the platform's default charset
            bbuf = ByteBuffer.wrap(buf, 0, now);
            ans = Charset.defaultCharset().newDecoder().onMalformedInput(r).onUnmappableCharacter(r).decode(bbuf).toString();
        } catch (CharacterCodingException ex2) {
            // if that also fails, we try using "ISO-8859-1" which should
            // always succeed but may map some characters wrong
            bbuf = ByteBuffer.wrap(buf, 0, now);
            ans = Charset.forName("ISO-8859-1").newDecoder().onMalformedInput(i).onUnmappableCharacter(i).decode(bbuf).toString();
        }
    }
    return convertLineBreak(ans);
}
Also used : CodingErrorAction(java.nio.charset.CodingErrorAction) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CharacterCodingException(java.nio.charset.CharacterCodingException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

CodingErrorAction (java.nio.charset.CodingErrorAction)4 CharsetDecoder (java.nio.charset.CharsetDecoder)3 Charset (java.nio.charset.Charset)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 RandomAccessFile (java.io.RandomAccessFile)1 ByteBuffer (java.nio.ByteBuffer)1 CharacterCodingException (java.nio.charset.CharacterCodingException)1