Example usage for java.nio.charset Charset newDecoder

List of usage examples for java.nio.charset Charset newDecoder

Introduction

In this page you can find the example usage for java.nio.charset Charset newDecoder.

Prototype

public abstract CharsetDecoder newDecoder();

Source Link

Document

Constructs a new decoder for this charset.

Usage

From source file:io.wcm.caravan.io.http.response.ByteArrayBody.java

private static String decodeOrDefault(byte[] data, Charset charset, String defaultValue) {
    if (data == null) {
        return defaultValue;
    }// w  ww. j  a  v  a2  s. c  o m
    checkNotNull(charset, "charset");
    try {
        return charset.newDecoder().decode(ByteBuffer.wrap(data)).toString();
    } catch (CharacterCodingException ex) {
        return defaultValue;
    }
}

From source file:gov.nasa.ensemble.dictionary.nddl.ModelGenerator.java

private static StringBuilder readModelFile(File file) throws IOException {
    StringBuilder result = new StringBuilder();
    FileInputStream fis = null;/*w w  w  .  j a  va2  s.c  o m*/
    InputStreamReader reader = null;
    int size;
    try {
        char[] buffer = new char[1024];
        fis = new FileInputStream(file);

        FileWriter filewriter = new FileWriter("out");
        String encname = filewriter.getEncoding();
        filewriter.close();

        Charset cs = Charset.forName(encname);
        CharsetDecoder csd = cs.newDecoder();
        csd.onMalformedInput(CodingErrorAction.REPLACE);

        reader = new InputStreamReader(fis, csd);
        while ((size = reader.read(buffer, 0, 1024)) > 0) {
            result.append(buffer, 0, size);
        }
    } catch (Exception e) {
        // System.out.println(encoding);
        e.printStackTrace();
    } finally {
        if (fis != null) {
            fis.close();
        }

        if (reader != null) {
            reader.close();
        }
    }

    return result;
}

From source file:FileUtil.java

/**
 *  Reads in file contents./* w ww  .  j a  va  2  s  .c  o m*/
 *  <P>
 *  This method is smart and falls back to ISO-8859-1 if the input stream does not
 *  seem to be in the specified encoding.
 *
 *  @param input The InputStream to read from.
 *  @param encoding The encoding to assume at first.
 *  @return A String, interpreted in the "encoding", or, if it fails, in Latin1.
 *  @throws IOException If the stream cannot be read or the stream cannot be
 *          decoded (even) in Latin1
 */
public static String readContents(InputStream input, String encoding) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    FileUtil.copyContents(input, out);

    ByteBuffer bbuf = ByteBuffer.wrap(out.toByteArray());

    Charset cset = Charset.forName(encoding);
    CharsetDecoder csetdecoder = cset.newDecoder();

    csetdecoder.onMalformedInput(CodingErrorAction.REPORT);
    csetdecoder.onUnmappableCharacter(CodingErrorAction.REPORT);

    try {
        CharBuffer cbuf = csetdecoder.decode(bbuf);

        return cbuf.toString();
    } catch (CharacterCodingException e) {
        Charset latin1 = Charset.forName("ISO-8859-1");
        CharsetDecoder l1decoder = latin1.newDecoder();

        l1decoder.onMalformedInput(CodingErrorAction.REPORT);
        l1decoder.onUnmappableCharacter(CodingErrorAction.REPORT);

        try {
            bbuf = ByteBuffer.wrap(out.toByteArray());

            CharBuffer cbuf = l1decoder.decode(bbuf);

            return cbuf.toString();
        } catch (CharacterCodingException ex) {
            throw (CharacterCodingException) ex.fillInStackTrace();
        }
    }
}

From source file:org.glite.slcs.util.Utils.java

/**
 * Converts a Java unicode string in a ISO-8859-1 (ISO Latin1) string.
 * //  ww w.  j  a v  a 2s  . c  o m
 * @param unicode
 *            The string to convert
 * @return The ISO-8859-1 string or <code>null</code> if the convertion
 *         failed.
 */
static public String convertUnicodeToISOLatin1(String unicode) {
    if (unicode == null) {
        return null;
    }
    String iso_8859_1 = null;
    try {
        Charset latin1 = Charset.forName("ISO-8859-1");
        CharsetDecoder decoder = latin1.newDecoder();
        CharsetEncoder encoder = latin1.newEncoder();
        ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(unicode));
        CharBuffer cbuf = decoder.decode(bbuf);
        iso_8859_1 = cbuf.toString();
    } catch (CharacterCodingException e) {
        // ignored: e.printStackTrace();
        LOG.error("Failed to convert Unicode: " + unicode + " to ISO-8859-1", e);
    }
    return iso_8859_1;
}

From source file:edu.cmu.cs.lti.util.general.BasicConvenience.java

/**
 * Returns a reader for the specified file using the specified encoding. Any characters in the
 * input that are malformed or invalid for that encoding are replaced with the specified
 * substitution string./*from  w ww.j  ava  2  s .  co  m*/
 * 
 * @param f
 * @param encoding
 * @param substitution
 * @return
 * @throws FileNotFoundException
 */
public static BufferedReader getEncodingSafeBufferedReader(File f, String encoding, String substitution)
        throws FileNotFoundException {
    BufferedReader cin;
    Charset cs = Charset.forName(encoding);
    CharsetDecoder decoder = cs.newDecoder();
    decoder.onMalformedInput(CodingErrorAction.REPLACE);
    decoder.onUnmappableCharacter(CodingErrorAction.REPLACE);
    decoder.replaceWith(substitution);

    InputStream is = new FileInputStream(f);
    if (f.toString().endsWith(".zip"))
        is = new ZipInputStream(is);

    cin = new BufferedReader(new InputStreamReader(is, decoder));
    return cin;
}

From source file:StreamUtil.java

/**
 * Reads the stream and generates a String content using the charset specified.
 * Stream will be closed at the end of the operation.
 * @param in InputStream as the input./*from w  ww .jav a2s  . c om*/
 * @param charset The charset to use to create the String object.
 * @return The output String.
 * @throws IOException
 */
public static String inputStream2String(final InputStream is, final Charset charset) throws IOException {
    try {
        StringBuilder out = new StringBuilder();
        byte[] b = new byte[4096];
        byte[] savedBytes = new byte[1];
        boolean hasSavedBytes = false;
        CharsetDecoder decoder = charset.newDecoder();
        for (int n; (n = is.read(b)) != -1;) {
            if (hasSavedBytes) {
                byte[] bTmp = new byte[savedBytes.length + b.length];
                System.arraycopy(savedBytes, 0, bTmp, 0, savedBytes.length);
                System.arraycopy(b, 0, bTmp, savedBytes.length, b.length);
                b = bTmp;
                hasSavedBytes = false;
                n = n + savedBytes.length;
            }

            CharBuffer charBuffer = decodeHelper(b, n, charset);
            if (charBuffer == null) {
                int nrOfChars = 0;
                while (charBuffer == null) {
                    nrOfChars++;
                    charBuffer = decodeHelper(b, n - nrOfChars, charset);
                    if (nrOfChars > 10 && nrOfChars < n) {
                        try {
                            charBuffer = decoder.decode(ByteBuffer.wrap(b, 0, n));
                        } catch (MalformedInputException ex) {
                            throw new IOException(
                                    "File not in supported encoding (" + charset.displayName() + ")", ex);
                        }
                    }
                }
                savedBytes = new byte[nrOfChars];
                hasSavedBytes = true;
                for (int i = 0; i < nrOfChars; i++) {
                    savedBytes[i] = b[n - nrOfChars + i];
                }
            }

            charBuffer.rewind(); // Bring the buffer's pointer to 0
            out.append(charBuffer.toString());
        }
        if (hasSavedBytes) {
            try {
                CharBuffer charBuffer = decoder.decode(ByteBuffer.wrap(savedBytes, 0, savedBytes.length));
            } catch (MalformedInputException ex) {
                throw new IOException("File not in supported encoding (" + charset.displayName() + ")", ex);
            }
        }
        return out.toString();
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:Main.java

/**
 * Returns a cached thread-local {@link CharsetDecoder} for the specified
 * <tt>charset</tt>.// ww  w. j a v a  2 s.  c  o  m
 */
public static CharsetDecoder getDecoder(Charset charset) {
    if (charset == null) {
        throw new NullPointerException("charset");
    }

    Map<Charset, CharsetDecoder> map = decoders.get();
    CharsetDecoder d = map.get(charset);
    if (d != null) {
        d.reset();
        d.onMalformedInput(CodingErrorAction.REPLACE);
        d.onUnmappableCharacter(CodingErrorAction.REPLACE);
        return d;
    }

    d = charset.newDecoder();
    d.onMalformedInput(CodingErrorAction.REPLACE);
    d.onUnmappableCharacter(CodingErrorAction.REPLACE);
    map.put(charset, d);
    return d;
}

From source file:com.silverpeas.ical.StringUtils.java

static char[] decodeToArray(byte[] bytes, Charset encoding) {
    if (CharEncoding.US_ASCII.equals(encoding.name())) {
        char[] array = new char[bytes.length];
        for (int i = 0; i < array.length; i++) {
            array[i] = (char) bytes[i];
        }/*from   w  w w.  ja  v a  2 s. c om*/
        return array;
    }
    try {
        CharBuffer buffer = encoding.newDecoder().decode(ByteBuffer.wrap(bytes));
        char[] array = new char[buffer.limit()];
        System.arraycopy(buffer.array(), 0, array, 0, array.length);
        return array;
    } catch (Exception nioException) {
        return (new String(bytes, encoding)).toCharArray();
    }
}

From source file:org.apache.tajo.util.StringUtils.java

public static char[] convertBytesToChars(byte[] src, Charset charset) {
    CharsetDecoder decoder = charset.newDecoder();
    char[] resultArray = new char[(int) (src.length * decoder.maxCharsPerByte())];

    if (src.length != 0) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(src);
        CharBuffer charBuffer = CharBuffer.wrap(resultArray);

        decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
        decoder.reset();/*from ww w .ja va  2  s .c  o m*/

        CoderResult coderResult = decoder.decode(byteBuffer, charBuffer, true);
        if (coderResult.isUnderflow()) {
            coderResult = decoder.flush(charBuffer);

            if (coderResult.isUnderflow()) {
                if (resultArray.length != charBuffer.position()) {
                    resultArray = Arrays.copyOf(resultArray, charBuffer.position());
                }
            }
        }
    }

    return resultArray;
}

From source file:com.microsoft.tfs.core.util.FileEncodingDetector.java

/**
 * Tests whether the given byte array looks like an ANSI text file with the
 * default text encoding, i.e. can be decoded with the current ANSI
 * character set. In multi-byte character sets (like Japanese, for example)
 * the entire byte array might not be converted entirely, because at the end
 * of array it might contain a broken multi-byte character. We still accept
 * this kind of files as ANSI ones if the not converted reminder of the
 * array is short enough.//from   w  w  w  . jav a2s.  c  o m
 *
 * @param bytes
 *        the bytes to check for ANSI-ness (must not be <code>null</code>)
 * @param limit
 *        the maximum number of bytes to read.
 * @return true if the given bytes look like part of an ANSI text file,
 *         false if they do not (because they contain control characters or
 *         other patterns).
 */
protected static boolean looksLikeANSI(final byte[] bytes, final int limit) {
    final Charset charSet = CodePageMapping.getCharset(FileEncoding.getDefaultTextEncoding().getCodePage());

    final ByteBuffer byteBuffer = ByteBuffer.wrap(bytes, 0, limit);
    final CharBuffer charBuffer = CharBuffer.allocate(limit);

    final CharsetDecoder decoder = charSet.newDecoder();
    decoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    decoder.onMalformedInput(CodingErrorAction.REPORT);

    final CoderResult rc = decoder.decode(byteBuffer, charBuffer, true);

    if (!rc.isError()) {
        return true;
    } else {
        return byteBuffer.position() > limit - 5;
    }
}