List of usage examples for java.nio CharBuffer toString
public String toString()
From source file:com.all.shared.util.SyncUtils.java
@SuppressWarnings("unchecked") public static List<SyncEventEntity> decodeAndUnzip(String encodedEvents) { byte[] unzippedBytes = unzip(Base64.decode(encodedEvents.getBytes())); String json = null;/*from www . java2 s.c om*/ try { CharBuffer charBuff = UTF_DECODER.decode(ByteBuffer.wrap(unzippedBytes)); json = charBuff.toString(); } catch (Exception e) { LOGGER.warn("Could not decode message with UTF-8."); json = new String(unzippedBytes); } return JsonConverter.toTypedCollection(json, ArrayList.class, SyncEventEntity.class); }
From source file:com.odiago.flumebase.io.CharBufferUtils.java
/** * Parses a CharSequence into a list of values, all of some other type. */// www .j a v a 2 s . c o m public static List<Object> parseList(CharBuffer chars, Type listItemType, String nullStr, String listDelim) throws ColumnParseException { StrTokenizer tokenizer = new StrTokenizer(chars.toString(), listDelim.charAt(0)); List<Object> out = new ArrayList<Object>(); while (tokenizer.hasNext()) { String part = (String) tokenizer.next(); out.add(parseType(CharBuffer.wrap(part), listItemType, nullStr, listDelim)); } return Collections.unmodifiableList(out); }
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 . co 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:parquet.tools.command.DumpCommand.java
public static String binaryToString(Binary value) { byte[] data = value.getBytes(); if (data == null) return null; try {//from w ww . j av a 2 s. com CharBuffer buffer = UTF8_DECODER.decode(value.toByteBuffer()); return buffer.toString(); } catch (Throwable th) { } return "<bytes...>"; }
From source file:modnlp.capte.AlignerUtils.java
public static String removeLongWhiteSpace(String s) { s = s.trim();/*w w w .j a va2 s. c o m*/ while (s.contains(" ")) // two white spaces { s = s.replaceAll(" ", " "); // the first arg should contain 2 spaces, the second only 1 } //System.out.println("Length before UTF8 cleaning " + s.length()); CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); utf8Decoder.onMalformedInput(CodingErrorAction.IGNORE); utf8Decoder.onUnmappableCharacter(CodingErrorAction.IGNORE); try { ByteBuffer bytes; bytes = ByteBuffer.wrap(s.getBytes()); CharBuffer parsed = utf8Decoder.decode(bytes); s = parsed.toString(); } catch (Exception e) { e.printStackTrace(); } //This code removes all of the bytes with value of zero // Gerard 21st March 2012: Solved, bug with random spaces in segments byte[] b = s.getBytes(); Vector<Byte> vb = new Vector(); for (int i = 0; i < b.length; i++) { if (b[i] != 0) { vb.add(b[i]); } } Object[] ba = vb.toArray(); Byte[] bya = new Byte[ba.length]; byte[] byta = new byte[ba.length]; for (int i = 0; i < ba.length; i++) { bya[i] = (Byte) ba[i]; byta[i] = bya[i].byteValue(); } s = new String(byta); return s; }
From source file:fi.johannes.kata.ocr.utils.files.CFileOperations.java
public static void writeChunk(CharBuffer cb, String outputFile) throws IOException { cb.flip();/* w w w . j av a 2 s . c o m*/ try (BufferedWriter bw = new BufferedWriter(new PrintWriter(outputFile))) { bw.write(cb.toString()); cb.clear(); } }
From source file:org.nuxeo.ecm.platform.filemanager.service.extension.NoteImporter.java
protected static String guessEncoding(Blob blob) throws IOException { // encoding already known? if (blob.getEncoding() != null) { return null; }//from www . j av a2 s . co m // bad mime type? String mimeType = blob.getMimeType(); if (mimeType == null) { return null; } if (!mimeType.startsWith("text/") && !mimeType.startsWith("application/xhtml")) { // not a text file, we shouldn't be in the Note importer return null; } byte[] bytes = blob.getByteArray(); List<String> charsets = new ArrayList<>(Arrays.asList("utf-8", "iso-8859-1")); String CSEQ = "charset="; int i = mimeType.indexOf(CSEQ); if (i > 0) { // charset specified in MIME type String onlyMimeType = mimeType.substring(0, i).replace(";", "").trim(); blob.setMimeType(onlyMimeType); String charset = mimeType.substring(i + CSEQ.length()); i = charset.indexOf(";"); if (i > 0) { charset = charset.substring(0, i); } charset = charset.trim().replace("\"", ""); charsets.add(0, charset); } else { // charset detected from the actual bytes CharsetMatch charsetMatch = new CharsetDetector().setText(bytes).detect(); if (charsetMatch != null) { String charset = charsetMatch.getName(); charsets.add(0, charset); } } // now convert the string according to the charset, and fallback on others if not possible for (String charset : charsets) { try { Charset cs = Charset.forName(charset); CharsetDecoder d = cs.newDecoder().onMalformedInput(CodingErrorAction.REPORT) .onUnmappableCharacter(CodingErrorAction.REPORT); CharBuffer cb = d.decode(ByteBuffer.wrap(bytes)); if (cb.length() != 0 && cb.charAt(0) == '\ufeff') { // remove BOM cb = cb.subSequence(1, cb.length()); } return cb.toString(); } catch (IllegalArgumentException e) { // illegal charset } catch (CharacterCodingException e) { // could not decode } } // nothing worked, use platform return null; }
From source file:org.apache.parquet.tools.command.DumpCommand.java
public static String binaryToString(Binary value) { byte[] data = value.getBytesUnsafe(); if (data == null) return null; try {/*from w w w. j a v a 2 s .co m*/ CharBuffer buffer = UTF8_DECODER.decode(value.toByteBuffer()); return buffer.toString(); } catch (Exception ex) { } return "<bytes...>"; }
From source file:org.colombbus.tangara.io.ScriptReader.java
/** * Try to decode a byte buffer with a charset */*from w ww. ja va 2 s. com*/ * @param content * the bute buffer * @param cs * the charset * @return <code>null</code> if the charset is not supported, or the decoded * string */ private static String tryDecodeBufferWithCharset(ByteBuffer content, Charset cs) { CharBuffer buffer = CharBuffer.allocate(content.capacity() * 2); CharsetDecoder decoder = createDecoder(cs); content.rewind(); CoderResult coderRes = decoder.decode(content, buffer, true); if (coderRes.isError() == false) { buffer.rewind(); return buffer.toString().trim(); } return null; }
From source file:net.fenyo.mail4hotspot.service.Browser.java
public static String getHtml(final String target_url, final Cookie[] cookies) throws IOException { // log.debug("RETRIEVING_URL=" + target_url); final URL url = new URL(target_url); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.69.60.6", 3128)); // final HttpURLConnection conn = (HttpURLConnection) url.openConnection(proxy); // HttpURLConnection.setFollowRedirects(true); // conn.setRequestProperty("User-agent", "my agent name"); conn.setRequestProperty("Accept-Language", "en-US"); // conn.setRequestProperty(key, value); // allow both GZip and Deflate (ZLib) encodings conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); final String encoding = conn.getContentEncoding(); InputStream is = null;/* w w w .j ava 2s . c o m*/ // create the appropriate stream wrapper based on the encoding type if (encoding != null && encoding.equalsIgnoreCase("gzip")) is = new GZIPInputStream(conn.getInputStream()); else if (encoding != null && encoding.equalsIgnoreCase("deflate")) is = new InflaterInputStream(conn.getInputStream(), new Inflater(true)); else is = conn.getInputStream(); final InputStreamReader reader = new InputStreamReader(new BufferedInputStream(is)); final CharBuffer cb = CharBuffer.allocate(1024 * 1024); int ret; do { ret = reader.read(cb); } while (ret > 0); cb.flip(); return cb.toString(); }