List of usage examples for java.nio CharBuffer toString
public String toString()
From source file:com.mymed.android.myjam.controller.HttpCall.java
/** * Given an InputStream reads the bytes as UTF8 chars and return a * String.//from w ww . jav a 2 s .c o m * @param is Input stream. * @param length Length of the stream in bytes. * @return The string * @throws InternalBackEndException Format is not correct or the length less then the real wrong. */ private static String convertStreamToString(InputStream is, long length) throws InternalClientException { String streamString; if (length > Integer.MAX_VALUE) throw new InternalClientException("Wrong Content"); int byteLength = (int) length; try { if (byteLength > 0) { ByteBuffer byteBuff = ByteBuffer.allocate(byteLength); int currByte; while ((currByte = is.read()) != -1) { byteBuff.put((byte) currByte); } byteBuff.compact(); final CharBuffer charBuf = CHARSET.newDecoder().decode(byteBuff); streamString = charBuf.toString(); return streamString; } else { BufferedReader buffRead = new BufferedReader( new InputStreamReader(is, Charset.forName(CHARSET_NAME))); StringBuilder sb = new StringBuilder(); String line; while ((line = buffRead.readLine()) != null) { sb.append(line + "\n"); } return sb.toString(); } } catch (IOException e) { throw new InternalClientException("Wrong content"); } catch (BufferOverflowException e) { throw new InternalClientException("Wrong length"); } finally { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:com.bah.culvert.data.index.Index.java
/** * Used to set a key indicating if the string value held by another * configuration key is a base64 encoded binary or not. * @param isValueBinaryEncodedSetting The key telling weather or not the other * key (setting) is base64.//from w ww. j a v a 2 s. c o m * @param potentiallyEncodedSetting The actual key that might be base64 * encoded. * @param data The data to set as base64. * @param conf The configuration to do the setting on. */ private static void setBinaryConfSetting(String isValueBinaryEncodedSetting, String potentiallyEncodedSetting, byte[] data, Configuration conf) { CharsetDecoder decoder = UTF_8.newDecoder(); decoder.onMalformedInput(CodingErrorAction.REPORT); try { CharBuffer colFamString = decoder.decode(ByteBuffer.wrap(data)); conf.setBoolean(isValueBinaryEncodedSetting, false); conf.set(potentiallyEncodedSetting, colFamString.toString()); } catch (CharacterCodingException e) { conf.setBoolean(isValueBinaryEncodedSetting, true); conf.set(potentiallyEncodedSetting, new String(Base64.encodeBase64(data), UTF_8)); } }
From source file:com.healthmarketscience.jackcess.impl.office.EncryptionHeader.java
private static String readCspName(ByteBuffer buffer) { // unicode string, must be multiple of 2 int rem = (buffer.remaining() / 2) * 2; String cspName = ""; if (rem > 0) { ByteBuffer cspNameBuf = ByteBuffer.wrap(ByteUtil.getBytes(buffer, rem)); CharBuffer tmpCspName = UNICODE_CHARSET.decode(cspNameBuf); // should be null terminated, strip that for (int i = 0; i < tmpCspName.limit(); ++i) { if (tmpCspName.charAt(i) == '\0') { tmpCspName.limit(i);//from w w w . j a va2 s . co m break; } } cspName = tmpCspName.toString(); } return cspName; }
From source file:com.odiago.flumebase.io.CharBufferUtils.java
/** * Parses a CharSequence into a value of a given expected type. * @param chars the unparsed characters representing the value * @param expectedType the expected type of the final value * @param nullStr a token indicating a null String instance. *///from w w w.ja v a 2 s. c om public static Object parseType(CharBuffer chars, Type expectedType, String nullStr, String listDelim) throws ColumnParseException { Type.TypeName primitiveTypeName = expectedType.getPrimitiveTypeName(); // TODO(aaron): Test how this handles a field that is an empty string. Object out = null; switch (primitiveTypeName) { case BINARY: try { out = ByteBuffer.wrap(chars.toString().getBytes("UTF-8")); } catch (UnsupportedEncodingException uee) { // Shouldn't ever be able to get here. // (http://download.oracle.com/javase/6/docs/api/java/nio/charset/Charset.html) LOG.error("Your JVM doesn't support UTF-8. This is really, really bad."); throw new ColumnParseException(uee); } break; case BOOLEAN: out = CharBufferUtils.parseBool(chars); break; case INT: out = CharBufferUtils.parseInt(chars); break; case BIGINT: out = CharBufferUtils.parseLong(chars); break; case FLOAT: out = CharBufferUtils.parseFloat(chars); break; case DOUBLE: out = CharBufferUtils.parseDouble(chars); break; case STRING: String asStr = chars.toString(); if (expectedType.isNullable() && asStr.equals(nullStr)) { out = null; } else { out = new Utf8(asStr); } break; case TIMESTAMP: out = CharBufferUtils.parseLong(chars); if (null != out) { out = new Timestamp((Long) out); } break; case TIMESPAN: // TODO: This should return a TimeSpan object, which is actually two // fields. We need to work on this... it should not just be a 'long' // representation. out = CharBufferUtils.parseLong(chars); break; case PRECISE: PreciseType preciseType = PreciseType.toPreciseType(expectedType); out = preciseType.parseStringInput(chars.toString()); break; case LIST: out = parseList(chars, ListType.toListType(expectedType).getElementType(), nullStr, listDelim); break; default: throw new ColumnParseException("Cannot parse recursive types"); } return out; }
From source file:com.log4ic.compressor.utils.FileUtils.java
/** * ?/*from w ww . j ava 2 s . c o m*/ * * @param fileInputStream * @return */ public static String readFile(FileInputStream fileInputStream) throws IOException { ByteBuffer buffer = ByteBuffer.allocate(1024); StringBuffer contentBuffer = new StringBuffer(); Charset charset = null; CharsetDecoder decoder = null; CharBuffer charBuffer = null; try { FileChannel channel = fileInputStream.getChannel(); while (true) { buffer.clear(); int pos = channel.read(buffer); if (pos == -1) { break; } buffer.flip(); charset = Charset.forName("UTF-8"); decoder = charset.newDecoder(); charBuffer = decoder.decode(buffer); contentBuffer.append(charBuffer.toString()); } } finally { if (fileInputStream != null) { try { fileInputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } return contentBuffer.toString(); }
From source file:org.owasp.webgoat.lessons.Encoding.java
/** * Description of the Method/*from w w w. j a v a 2s. c o m*/ * * @param str * Description of the Parameter * @return Description of the Return Value */ public static String unicodeDecode(String str) { // FIXME: TOTALLY EXPERIMENTAL try { ByteBuffer bbuf = ByteBuffer.allocate(str.length()); bbuf.put(str.getBytes()); Charset charset = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = charset.newDecoder(); CharBuffer cbuf = decoder.decode(bbuf); return (cbuf.toString()); } catch (Exception e) { return ("Encoding problem"); } }
From source file:xyz.cloudbans.client.utils.httpclient.SerializerConsumer.java
@Override protected void onCharReceived(CharBuffer charBuffer, IOControl ioControl) throws IOException { sb.append(charBuffer.toString()); }
From source file:com.clxcommunications.xms.EmptyAsyncConsumer.java
@Override protected void onCharReceived(CharBuffer buf, IOControl ioctrl) throws IOException { sb.append(buf.toString()); }
From source file:implementations.terrastoreDB.java
@Override public int updateDB(String ID, String newValue) { int ret;//ww w . j a v a2s . co m org.json.JSONObject myjson = null; try { myjson = XML.toJSONObject(newValue); } catch (JSONException e1) { e1.printStackTrace(); } try { newValue = myjson.toString(); CharsetDecoder utf8Decoder = Charset.forName("UTF-8").newDecoder(); utf8Decoder.onMalformedInput(CodingErrorAction.REPLACE); utf8Decoder.onUnmappableCharacter(CodingErrorAction.REPLACE); ByteBuffer bytes = ByteBuffer.wrap(newValue.getBytes("UTF8")); CharBuffer parsed = utf8Decoder.decode(bytes); client.bucket("test").key(ID).put(parsed.toString()); ret = 1; } catch (Exception e) { ret = -1; e.printStackTrace(); } return ret; }
From source file:org.commoncrawl.util.CharsetUtils.java
public static Pair<Pair<Integer, Charset>, String> bestEffortDecodeBytes(String headers, byte[] crawlData, int offset, int length) throws IOException { Pair<Integer, Charset> charsetTuple = bestEffortDetectCharset(headers, crawlData, offset, length); if (charsetTuple != null) { try {/* w ww.jav a 2 s . co m*/ CharBuffer ucs2Chars = charsetTuple.e1.decode(ByteBuffer.wrap(crawlData, offset, length)); return new Pair<Pair<Integer, Charset>, String>(charsetTuple, ucs2Chars.toString()); } catch (Exception e) { LOG.error(CCStringUtils.stringifyException(e)); } } return new Pair<Pair<Integer, Charset>, String>( new Pair<Integer, Charset>(CHARSET_SRC_NO_MATCH, Charset.forName("ASCII")), new String(crawlData, Charset.forName("ASCII"))); }