List of usage examples for java.nio CharBuffer toString
public String toString()
From source file:de.intranda.goobi.plugins.utils.SRUClient.java
/** * Converts a <code>String</code> from one given encoding to the other. * /*from w ww .ja va 2 s.co m*/ * @param string The string to convert. * @param from Source encoding. * @param to Destination encoding. * @return The converted string. */ public static String convertStringEncoding(String string, String from, String to) { try { Charset charsetFrom = Charset.forName(from); Charset charsetTo = Charset.forName(to); CharsetEncoder encoder = charsetFrom.newEncoder(); CharsetDecoder decoder = charsetTo.newDecoder(); ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(string)); CharBuffer cbuf = decoder.decode(bbuf); return cbuf.toString(); } catch (CharacterCodingException e) { logger.error(e.getMessage(), e); } return string; }
From source file:org.colombbus.tangara.io.ScriptReader.java
private static String extractScriptWithCharset(ByteBuffer content, Charset charset) { CharBuffer contentBuffer = charset.decode(content); return contentBuffer.toString().trim(); }
From source file:Main.java
/** * Returns the {@code String} instance with detecting the Japanese encoding. * @throws UnsupportedEncodingException if it fails to detect the encoding. *//*w ww. j a v a 2 s . c o m*/ static String toStringWithEncodingDetection(ByteBuffer buffer) throws UnsupportedEncodingException { for (String encoding : JAPANESE_ENCODING_LIST) { buffer.position(0); try { Charset charset = Charset.forName(encoding); CharBuffer result = charset.newDecoder().onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT).decode(buffer); String str = result.toString(); if (str.length() > 0 && str.charAt(0) == 0xFEFF) { // Remove leading BOM if necessary. str = str.substring(1); } return str; } catch (Exception e) { // Ignore exceptions, and retry next encoding. } } throw new UnsupportedEncodingException("Failed to detect encoding"); }
From source file:Main.java
public static String byteBufferToString(ByteBuffer buffer) { CharBuffer charBuffer = null; try {/*w w w . j av a 2s . c om*/ Charset charset = Charset.forName("UTF-8"); CharsetDecoder decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer); buffer.flip(); return charBuffer.toString(); } catch (Exception ex) { ex.printStackTrace(); return ""; } }
From source file:com.zimbra.cs.util.ZipUtil.java
private static String convertBytesIfPossible(byte[] rawBytes, Charset cset) { CharsetDecoder charsetDecoder = reportingDecoder(cset); try {//from ww w . j av a 2 s . co m CharBuffer cb = charsetDecoder.decode(ByteBuffer.wrap(rawBytes)); String val = cb.toString(); ZimbraLog.misc.debug("ZipUtil name '%s' decoded from Charset='%s'", val, cset.name()); return val; } catch (Exception ex) { ZimbraLog.misc.trace("ZipUtil failed decode from Charset='%s' %s", cset.name(), ex.getMessage()); } return null; }
From source file:com.sunchenbin.store.feilong.core.io.IOReaderUtil.java
/** * ?./*from ww w. jav a2 s .c o m*/ * * @param file * * @param charsetName * ?,isNullOrEmpty, {@link CharsetType#UTF8} * @return the file content * @see org.apache.commons.io.FileUtils#readFileToString(File, Charset) */ public static String getFileContent(File file, String charsetName) { if (Validator.isNullOrEmpty(file)) { throw new NullPointerException("the file is null or empty!"); } // ? final int capacity = 186140; ByteBuffer byteBuffer = ByteBuffer.allocateDirect(capacity); StringBuilder sb = new StringBuilder(capacity); FileInputStream fileInputStream = null; try { fileInputStream = new FileInputStream(file); // ?????. FileChannel fileChannel = fileInputStream.getChannel(); String useCharsetName = Validator.isNullOrEmpty(charsetName) ? DEFAULT_CHARSET_NAME : charsetName; Charset charset = Charset.forName(useCharsetName); while (fileChannel.read(byteBuffer) != -1) { // ?? byteBuffer.flip(); CharBuffer charBuffer = charset.decode(byteBuffer); sb.append(charBuffer.toString()); byteBuffer.clear(); } return sb.toString(); } catch (FileNotFoundException e) { throw new UncheckedIOException(e); } catch (IOException e) { throw new UncheckedIOException(e); } finally { // ? ,^_^ IOUtils.closeQuietly(fileInputStream); } }
From source file:com.odiago.flumebase.io.CharBufferUtils.java
public static String parseString(CharBuffer chars) throws ColumnParseException { return chars.toString(); }
From source file:fi.johannes.kata.ocr.utils.files.CFileOperations.java
public static void writeToFile(ByteBuffer bb, String outputFile) throws IOException { bb.flip();//from w w w . j a v a2 s . co m try (BufferedWriter bw = new BufferedWriter(new PrintWriter(outputFile, "UTF-8"))) { CharBuffer charBuffer = StandardCharsets.UTF_8.decode(bb); bw.write(charBuffer.toString()); charBuffer.clear(); } }
From source file:FileUtil.java
/** * Reads in file contents./*w ww .j a v a 2s .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:com.chiorichan.util.StringUtil.java
public static String bytesToStringUTFNIO(byte[] bytes) { if (bytes == null) return null; CharBuffer cBuffer = ByteBuffer.wrap(bytes).asCharBuffer(); return cBuffer.toString(); }