本文整理匯總了Java中java.nio.CharBuffer.toString方法的典型用法代碼示例。如果您正苦於以下問題:Java CharBuffer.toString方法的具體用法?Java CharBuffer.toString怎麽用?Java CharBuffer.toString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.CharBuffer
的用法示例。
在下文中一共展示了CharBuffer.toString方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: decode
import java.nio.CharBuffer; //導入方法依賴的package包/類
public void decode() throws MAPException {
if (this.isDecoded)
return;
this.isDecoded = true;
this.decodedMessage = null;
if (this.encodedData == null)
throw new MAPException("Error decoding a text from Sms CommandData: encodedData field is null");
// TODO: what is an encoding algorithm ?
Charset chs = Charset.forName("US-ASCII");
byte[] buf = this.encodedData;
ByteBuffer bb = ByteBuffer.wrap(buf);
CharBuffer bf = chs.decode(bb);
this.decodedMessage = bf.toString();
}
示例2: process
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Override
public String process(int lineNumber, String result) throws DeserializeException {
while (result.contains("\\X2\\")) {
int index = result.indexOf("\\X2\\");
int indexOfEnd = result.indexOf("\\X0\\", index);
if (indexOfEnd == -1) {
throw new DeserializeException(lineNumber, "\\X2\\ not closed with \\X0\\");
}
if ((indexOfEnd - index) % 4 != 0) {
throw new DeserializeException(lineNumber, "Number of hex chars in \\X2\\ definition not divisible by 4");
}
try {
ByteBuffer buffer = ByteBuffer.wrap(Hex.decodeHex(result.substring(index + 4, indexOfEnd).toCharArray()));
CharBuffer decode = Charsets.UTF_16BE.decode(buffer);
result = result.substring(0, index) + decode.toString() + result.substring(indexOfEnd + 4);
} catch (DecoderException e) {
throw new DeserializeException(lineNumber, e);
}
}
return result;
}
示例3: getString
import java.nio.CharBuffer; //導入方法依賴的package包/類
private static String getString(ByteBuffer buffer) {
Charset charset = null;
CharsetDecoder decoder = null;
CharBuffer charBuffer = null;
try {
charset = Charset.forName("UTF-8");
decoder = charset.newDecoder();
// 用這個的話,隻能輸出來一次結果,第二次顯示為空
// charBuffer = decoder.decode(buffer);
charBuffer = decoder.decode(buffer.asReadOnlyBuffer());
return charBuffer.toString();
}
catch (Exception ex) {
ex.printStackTrace();
return "error";
}
}
示例4: main
import java.nio.CharBuffer; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception, AlluxioException {
// 1.創建鍵值對係統實例
KeyValueSystem kvs = KeyValueSystem.Factory.create();
// 2.開啟鍵值對reader
KeyValueStoreReader reader = kvs.openStore(new AlluxioURI("/alluxiotest/kvstore"));
// 3.根據鍵讀取值,byte[]類型數據
byte[] test1 = reader.get("100".getBytes());
String test1Str = new String(test1);
System.out.println(test1Str);
// 4.根據鍵讀取值,ByteBuffer類型數據
ByteBuffer test2 = reader.get(ByteBuffer.wrap("200".getBytes()));
Charset charset = Charset.forName("UTF-8");
CharsetDecoder decoder = charset.newDecoder();
CharBuffer charBuffer = decoder.decode(test2.asReadOnlyBuffer());
String test2Str = charBuffer.toString();
System.out.println(test2Str);
// 5.獲取鍵值對存儲的實際大小
System.out.println("鍵值對存儲的大小=" + reader.size());
// 6.關閉鍵值對reader
reader.close();
}
示例5: readFile
import java.nio.CharBuffer; //導入方法依賴的package包/類
public static String readFile(File f) throws IOException {
FileReader r = new FileReader(f);
int fileLen = (int)f.length();
CharBuffer cb = CharBuffer.allocate(fileLen);
r.read(cb);
cb.rewind();
return cb.toString();
}
示例6: readByteArray
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Read a 'n' bytes from buffer into a String where n is the framesize - offset
* so thefore cannot use this if there are other objects after it because it has no
* delimiter.
* <p/>
* Must take into account the text encoding defined in the Encoding Object
* ID3 Text Frames often allow multiple strings seperated by the null char
* appropriate for the encoding.
*
* @param arr this is the buffer for the frame
* @param offset this is where to start reading in the buffer for this field
* @throws NullPointerException
* @throws IndexOutOfBoundsException
*/
public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException {
logger.finest("Reading from array from offset:" + offset);
//Get the Specified Decoder
String charSetName = getTextEncodingCharSet();
CharsetDecoder decoder = Charset.forName(charSetName).newDecoder();
decoder.reset();
//Decode sliced inBuffer
ByteBuffer inBuffer;
if (TagOptionSingleton.getInstance().isAndroid()) {
//#302 [dallen] truncating array manually since the decoder.decode() does not honor the offset in the in buffer
byte[] truncArr = new byte[arr.length - offset];
System.arraycopy(arr, offset, truncArr, 0, truncArr.length);
inBuffer = ByteBuffer.wrap(truncArr);
} else {
inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
}
CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
if (coderResult.isError()) {
logger.warning("Decoding error:" + coderResult.toString());
}
decoder.flush(outBuffer);
outBuffer.flip();
//If using UTF16 with BOM we then search through the text removing any BOMs that could exist
//for multiple values, BOM could be Big Endian or Little Endian
if (charSetName.equals(TextEncoding.CHARSET_UTF_16)) {
value = outBuffer.toString().replace("\ufeff", "").replace("\ufffe", "");
} else {
value = outBuffer.toString();
}
//SetSize, important this is correct for finding the next datatype
setSize(arr.length - offset);
logger.config("Read SizeTerminatedString:" + value + " size:" + size);
}
示例7: readByteArray
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Read a 'n' bytes from buffer into a String where n is the frameSize - offset
* so therefore cannot use this if there are other objects after it because it has no
* delimiter.
* <p/>
* Must take into account the text encoding defined in the Encoding Object
* ID3 Text Frames often allow multiple strings separated by the null char
* appropriate for the encoding.
*
* @param arr this is the buffer for the frame
* @param offset this is where to start reading in the buffer for this field
* @throws NullPointerException
* @throws IndexOutOfBoundsException
*/
public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException {
logger.finest("Reading from array from offset:" + offset);
//Get the Specified Decoder
String charSetName = getTextEncodingCharSet();
CharsetDecoder decoder = Charset.forName(charSetName).newDecoder();
//Decode sliced inBuffer
ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
decoder.reset();
CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
if (coderResult.isError()) {
logger.warning("Decoding error:" + coderResult.toString());
}
decoder.flush(outBuffer);
outBuffer.flip();
//Store value
String stringValue = outBuffer.toString();
value = new PartOfSetValue(stringValue);
//SetSize, important this is correct for finding the next datatype
setSize(arr.length - offset);
logger.config("Read SizeTerminatedString:" + value + " size:" + size);
}
示例8: readChars
import java.nio.CharBuffer; //導入方法依賴的package包/類
private static String readChars(FileObject fo, Charset set) throws IOException {
CharBuffer arr = CharBuffer.allocate((int)fo.getSize() * 2);
BufferedReader r = new BufferedReader(new InputStreamReader(fo.getInputStream(), set));
while (r.read(arr) != -1) {
// again
}
r.close();
arr.flip();
return arr.toString();
}
示例9: getSubstringByte
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* 指定したバイト數で文字列をカットする
*
* @param obj 対象オブジェクト
* @param capacity カットするバイト數
* @return String
* @throws CharacterCodingException
* @throws UnsupportedEncodingException
*/
private String getSubstringByte(final Object obj, final int capacity) throws CharacterCodingException,
UnsupportedEncodingException {
String str = obj == null ? "null" : obj.toString();
if (capacity < 1) {
return str;
}
CharsetEncoder ce = Charset.forName(ENCODING_SHIFT_JIS).newEncoder()
.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE).reset();
if (capacity >= ce.maxBytesPerChar() * str.length()) {
return str;
}
CharBuffer cb = CharBuffer.wrap(new char[Math.min(str.length(), capacity)]);
str.getChars(0, Math.min(str.length(), cb.length()), cb.array(), 0);
if (capacity >= ce.maxBytesPerChar() * cb.limit()) {
return cb.toString();
}
ByteBuffer out = ByteBuffer.allocate(capacity);
ce.reset();
CoderResult cr = null;
if (cb.hasRemaining()) {
cr = ce.encode(cb, out, true);
} else {
cr = CoderResult.UNDERFLOW;
}
if (cr.isUnderflow()) {
cr = ce.flush(out);
}
return cb.flip().toString();
}
示例10: get
import java.nio.CharBuffer; //導入方法依賴的package包/類
private String get(CharBuffer key) {
if (map.containsKey(key))
return map.get(key);
String value = new String(key.toString());
map.put(key, value);
return value;
}
示例11: test
import java.nio.CharBuffer; //導入方法依賴的package包/類
private static void test(CharBuffer cb, String exp) {
cb.limit(cb.position());
cb.rewind();
if (!cb.toString().equals(exp))
throw new RuntimeException("expect: '" + exp + "'; got: '"
+ cb.toString() + "'");
cb.clear();
}
示例12: decode
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Convert string from UTF-7 characters
*
* @param string Input string for decoding
* @return Decoded string
*/
public static String decode(String string, String charsetName)
{
if (string.length() <= 1)
{
return string;
}
CharsetProvider provider = new CharsetProvider();
Charset charset = provider.charsetForName(charsetName);
CharBuffer charBuffer = charset.decode(ByteBuffer.wrap(string.getBytes()));
return charBuffer.toString();
}
示例13: readByteArray
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Read a 'n' bytes from buffer into a String where n is the frameSize - offset
* so therefore cannot use this if there are other objects after it because it has no
* delimiter.
*
* Must take into account the text encoding defined in the Encoding Object
* ID3 Text Frames often allow multiple strings separated by the null char
* appropriate for the encoding.
*
* @param arr this is the buffer for the frame
* @param offset this is where to start reading in the buffer for this field
* @throws NullPointerException
* @throws IndexOutOfBoundsException
*/
public void readByteArray(byte[] arr, int offset) throws InvalidDataTypeException
{
logger.finest("Reading from array from offset:" + offset);
//Get the Specified Decoder
CharsetDecoder decoder = getTextEncodingCharSet().newDecoder();
//Decode sliced inBuffer
ByteBuffer inBuffer = ByteBuffer.wrap(arr, offset, arr.length - offset).slice();
CharBuffer outBuffer = CharBuffer.allocate(arr.length - offset);
decoder.reset();
CoderResult coderResult = decoder.decode(inBuffer, outBuffer, true);
if (coderResult.isError())
{
logger.warning("Decoding error:" + coderResult.toString());
}
decoder.flush(outBuffer);
outBuffer.flip();
//Store value
String stringValue = outBuffer.toString();
value = new PartOfSetValue(stringValue);
//SetSize, important this is correct for finding the next datatype
setSize(arr.length - offset);
logger.config("Read SizeTerminatedString:" + value + " size:" + size);
}
示例14: toString
import java.nio.CharBuffer; //導入方法依賴的package包/類
/** Consumes remaining contents of this object, and returns them as a string. */
public String toString() {
Charset cset = Charset.forName("UTF-8");
CharBuffer cb = cset.decode(ByteBuffer.wrap(this.toArray()));
return cb.toString();
}
示例15: tryDecode
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Override
public String
tryDecode(
byte[] array,
boolean lax )
{
try{
ByteBuffer bb = ByteBuffer.wrap(array);
CharBuffer cb = CharBuffer.allocate(array.length);
CoderResult cr;
this_mon.enter();
try {
cr = decoder.decode(bb, cb, true);
} finally {
this_mon.exit();
}
if ( !cr.isError() ){
cb.flip();
String str = cb.toString();
// lax means that as long as the conversion works we consider it usable
// as opposed to strict which requires reverse-conversion equivalence
if ( lax ){
return( str );
}
byte[] b2 = str.getBytes( getName() );
// make sure the conversion is symetric (there are cases where it appears
// to work but in fact converting back to bytes leads to a different
// result
/*
for (int k=0;k<str.length();k++){
System.out.print( Integer.toHexString(str.charAt(k)));
}
System.out.println("");
*/
if ( Arrays.equals( array, b2 )){
return( str );
}
}
return( null );
}catch( Throwable e ){
// Throwable here as we can get "classdefnotfound" + others if the decoder
// isn't available
return( null );
}
}