当前位置: 首页>>代码示例>>Java>>正文


Java CharBuffer.toString方法代码示例

本文整理汇总了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();
    }
 
开发者ID:RestComm,项目名称:phone-simulator,代码行数:19,代码来源:CommandDataImpl.java

示例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;
}
 
开发者ID:shenan4321,项目名称:BIMplatform,代码行数:22,代码来源:X2Pass.java

示例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";
    }
}
 
开发者ID:BriData,项目名称:DBus,代码行数:18,代码来源:MessageScheme.java

示例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();
}
 
开发者ID:zhangjunfang,项目名称:alluxio,代码行数:27,代码来源:KeyValueReader001.java

示例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();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:9,代码来源:CslTestBase.java

示例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);

}
 
开发者ID:openaudible,项目名称:openaudible,代码行数:54,代码来源:TextEncodedStringSizeTerminated.java

示例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);
}
 
开发者ID:openaudible,项目名称:openaudible,代码行数:41,代码来源:PartOfSet.java

示例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();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:12,代码来源:PropertiesProviderTest.java

示例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();
}
 
开发者ID:future-architect,项目名称:uroborosql,代码行数:42,代码来源:DumpResultSqlFilter.java

示例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;
}
 
开发者ID:Bibliome,项目名称:bibliome-java-utils,代码行数:8,代码来源:StringCache.java

示例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();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:StockName.java

示例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();
}
 
开发者ID:Alfresco,项目名称:alfresco-repository,代码行数:18,代码来源:Utf7.java

示例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);
}
 
开发者ID:GlennioTech,项目名称:MetadataEditor,代码行数:42,代码来源:PartOfSet.java

示例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();
}
 
开发者ID:fengchen8086,项目名称:ditb,代码行数:7,代码来源:ByteIterator.java

示例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 );
	}
}
 
开发者ID:BiglySoftware,项目名称:BiglyBT,代码行数:63,代码来源:LocaleUtilDecoderReal.java


注:本文中的java.nio.CharBuffer.toString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。