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


Java CharBuffer类代码示例

本文整理汇总了Java中java.nio.CharBuffer的典型用法代码示例。如果您正苦于以下问题:Java CharBuffer类的具体用法?Java CharBuffer怎么用?Java CharBuffer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


CharBuffer类属于java.nio包,在下文中一共展示了CharBuffer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: decodeBufferLoop

import java.nio.CharBuffer; //导入依赖的package包/类
private CoderResult decodeBufferLoop(ByteBuffer src,
                                     CharBuffer dst)
{
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            byte b = src.get();
            if (!dst.hasRemaining())
                return CoderResult.OVERFLOW;
            dst.put((char)(b & 0xff));
            mark++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:18,代码来源:ISO_8859_1.java

示例2: isLegalReplacement

import java.nio.CharBuffer; //导入依赖的package包/类
/**
 * Tells whether or not the given byte array is a legal replacement value
 * for this encoder.
 *
 * <p> A replacement is legal if, and only if, it is a legal sequence of
 * bytes in this encoder's charset; that is, it must be possible to decode
 * the replacement into one or more sixteen-bit Unicode characters.
 *
 * <p> The default implementation of this method is not very efficient; it
 * should generally be overridden to improve performance.  </p>
 *
 * @param  repl  The byte array to be tested
 *
 * @return  <tt>true</tt> if, and only if, the given byte array
 *          is a legal replacement value for this encoder
 */
public boolean isLegalReplacement(byte[] repl) {
    WeakReference<CharsetDecoder> wr = cachedDecoder;
    CharsetDecoder dec = null;
    if ((wr == null) || ((dec = wr.get()) == null)) {
        dec = charset().newDecoder();
        dec.onMalformedInput(CodingErrorAction.REPORT);
        dec.onUnmappableCharacter(CodingErrorAction.REPORT);
        cachedDecoder = new WeakReference<CharsetDecoder>(dec);
    } else {
        dec.reset();
    }
    ByteBuffer bb = ByteBuffer.wrap(repl);
    CharBuffer cb = CharBuffer.allocate((int)(bb.remaining()
                                              * dec.maxCharsPerByte()));
    CoderResult cr = dec.decode(bb, cb, true);
    return !cr.isError();
}
 
开发者ID:SunburstApps,项目名称:OpenJSharp,代码行数:34,代码来源:CharsetEncoder.java

示例3: testGetChar

import java.nio.CharBuffer; //导入依赖的package包/类
@Test
public void testGetChar() {
  ByteBuffer bb = ByteBuffer.allocate(10);
  CharBuffer cb = bb.asCharBuffer();
  cb.put("abcde");
  byte[] bytes = bb.array();
  ByteSource bs = createByteSource(bytes);
  char c = bs.getChar();
  assertEquals('a', c);
  assertEquals(2, bs.position());
  c = bs.getChar();
  assertEquals('b', c);
  assertEquals(4, bs.position());
  bs.position(8);
  c = bs.getChar();
  assertEquals('e', c);
  assertEquals(10, bs.position());
  try {
    bs.getChar();
    fail("expected BufferUnderflowException");
  } catch (BufferUnderflowException expected) {
  }
}
 
开发者ID:ampool,项目名称:monarch,代码行数:24,代码来源:ByteSourceJUnitTest.java

示例4: testGet_io

import java.nio.CharBuffer; //导入依赖的package包/类
public void testGet_io() throws IOException {
  assertEquals(-1, ArbitraryInstances.get(InputStream.class).read());
  assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read());
  assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1)));
  assertEquals(-1, ArbitraryInstances.get(Reader.class).read());
  assertEquals(-1, ArbitraryInstances.get(StringReader.class).read());
  assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity());
  assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity());
  assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity());
  assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity());
  assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity());
  assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity());
  assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity());
  assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity());
  ArbitraryInstances.get(PrintStream.class).println("test");
  ArbitraryInstances.get(PrintWriter.class).println("test");
  assertNotNull(ArbitraryInstances.get(File.class));
  assertFreshInstanceReturned(
      ByteArrayOutputStream.class, OutputStream.class,
      Writer.class, StringWriter.class,
      PrintStream.class, PrintWriter.class);
  assertEquals(ByteSource.empty(), ArbitraryInstances.get(ByteSource.class));
  assertEquals(CharSource.empty(), ArbitraryInstances.get(CharSource.class));
  assertNotNull(ArbitraryInstances.get(ByteSink.class));
  assertNotNull(ArbitraryInstances.get(CharSink.class));
}
 
开发者ID:zugzug90,项目名称:guava-mock,代码行数:27,代码来源:ArbitraryInstancesTest.java

示例5: getBytes

import java.nio.CharBuffer; //导入依赖的package包/类
byte[] getBytes(String s) {
    CharsetEncoder ce = encoder().reset();
    char[] ca = s.toCharArray();
    int len = (int)(ca.length * ce.maxBytesPerChar());
    byte[] ba = new byte[len];
    if (len == 0)
        return ba;
    ByteBuffer bb = ByteBuffer.wrap(ba);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = ce.encode(cb, bb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = ce.flush(bb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    if (bb.position() == ba.length)  // defensive copy?
        return ba;
    else
        return Arrays.copyOf(ba, bb.position());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:21,代码来源:ZipCoder.java

示例6: implFlush

import java.nio.CharBuffer; //导入依赖的package包/类
@Override
protected CoderResult implFlush(ByteBuffer out) {
    CoderResult res;
    if (buffer != null) {
        res = handleHead(null, out);
        return res;
    }
    else if (remainder != null) {
        encoder.encode(remainder, out, true);
    }
    else {
        CharBuffer empty = (CharBuffer) CharBuffer.allocate(0).flip();
        encoder.encode(empty, out, true);
    }
    res = encoder.flush(out);
    return res;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:18,代码来源:HtmlDataObject.java

示例7: doWriteText

import java.nio.CharBuffer; //导入依赖的package包/类
private void doWriteText(CharBuffer buffer, boolean finalFragment) throws IOException {
	CharsetEncoder encoder = B2CConverter.UTF_8.newEncoder();
	do {
		CoderResult cr = encoder.encode(buffer, bb, true);
		if (cr.isError()) {
			cr.throwException();
		}
		bb.flip();
		if (buffer.hasRemaining()) {
			doWriteBytes(bb, false);
		} else {
			doWriteBytes(bb, finalFragment);
		}
	} while (buffer.hasRemaining());

	// Reset - bb will be cleared in doWriteBytes()
	cb.clear();
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:19,代码来源:WsOutbound.java

示例8: flush

import java.nio.CharBuffer; //导入依赖的package包/类
public void flush() throws IOException {
    //Log.i("PackageManager", "flush mPos=" + mPos);
    if (mPos > 0) {
        if (mOutputStream != null) {
            CharBuffer charBuffer = CharBuffer.wrap(mText, 0, mPos);
            CoderResult result = mCharset.encode(charBuffer, mBytes, true);
            while (true) {
                if (result.isError()) {
                    throw new IOException(result.toString());
                } else if (result.isOverflow()) {
                    flushBytes();
                    result = mCharset.encode(charBuffer, mBytes, true);
                    continue;
                }
                break;
            }
            flushBytes();
            mOutputStream.flush();
        } else {
            mWriter.write(mText, 0, mPos);
            mWriter.flush();
        }
        mPos = 0;
    }
}
 
开发者ID:ukanth,项目名称:XPrivacy,代码行数:26,代码来源:FastXmlSerializer.java

示例9: WsFrameBase

import java.nio.CharBuffer; //导入依赖的package包/类
public WsFrameBase(WsSession wsSession, Transformation transformation) {
    inputBuffer = new byte[Constants.DEFAULT_BUFFER_SIZE];
    messageBufferBinary =
            ByteBuffer.allocate(wsSession.getMaxBinaryMessageBufferSize());
    messageBufferText =
            CharBuffer.allocate(wsSession.getMaxTextMessageBufferSize());
    this.wsSession = wsSession;
    Transformation finalTransformation;
    if (isMasked()) {
        finalTransformation = new UnmaskTransformation();
    } else {
        finalTransformation = new NoopTransformation();
    }
    if (transformation == null) {
        this.transformation = finalTransformation;
    } else {
        transformation.setNext(finalTransformation);
        this.transformation = transformation;
    }
}
 
开发者ID:liaokailin,项目名称:tomcat7,代码行数:21,代码来源:WsFrameBase.java

示例10: getComputedBytes

import java.nio.CharBuffer; //导入依赖的package包/类
/**
 * Returns a UTF8 encoded <code>byte[]</code> representation of the 
 * <code>char[]</code> used to create this buffer.
 * 
 * @return A byte[]
 */
byte[] getComputedBytes()
{
    if ( computedBytes == null )
    {
        ByteBuffer byteBuffer = UTF8.encode(
            CharBuffer.wrap( precomputedChars, 0, precomputedChars.length ) );
        computedBytes = new byte[byteBuffer.remaining()];
        byteBuffer.get( computedBytes );

        // clear out the temporary bytebuffer
        byteBuffer.flip();
        byte[] nullifier = new byte[byteBuffer.limit()];
        Arrays.fill( nullifier, ( byte ) 0 );
        byteBuffer.put( nullifier );
    }
    return computedBytes;
}
 
开发者ID:apache,项目名称:directory-ldap-api,代码行数:24,代码来源:MemoryClearingBuffer.java

示例11: encodeBufferLoop

import java.nio.CharBuffer; //导入依赖的package包/类
private CoderResult encodeBufferLoop(CharBuffer src,
                                     ByteBuffer dst)
{
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            char c = src.get();
            if (c <= '\u00FF') {
                if (!dst.hasRemaining())
                    return CoderResult.OVERFLOW;
                dst.put((byte)c);
                mark++;
                continue;
            }
            if (sgp.parse(c, src) < 0)
                return sgp.error();
            return sgp.unmappableResult();
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:24,代码来源:ISO_8859_1.java

示例12: decodeBufferLoop

import java.nio.CharBuffer; //导入依赖的package包/类
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            char c = decode(src.get());
            if (c == UNMAPPABLE_DECODING)
                return CoderResult.unmappableForLength(1);
            if (!dst.hasRemaining())
                return CoderResult.OVERFLOW;
            dst.put(c);
            mark++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
开发者ID:lambdalab-mirror,项目名称:jdk8u-jdk,代码行数:18,代码来源:SingleByte.java

示例13: resizeCharBuffer

import java.nio.CharBuffer; //导入依赖的package包/类
private void resizeCharBuffer() throws IOException {
	int maxSize = getCharBufferMaxSize();
	if (cb.limit() >= maxSize) {
		throw new IOException(sm.getString("message.bufferTooSmall"));
	}

	long newSize = cb.limit() * 2;
	if (newSize > maxSize) {
		newSize = maxSize;
	}

	// Cast is safe. newSize < maxSize and maxSize is an int
	CharBuffer newBuffer = CharBuffer.allocate((int) newSize);
	cb.rewind();
	newBuffer.put(cb);
	cb = newBuffer;
}
 
开发者ID:how2j,项目名称:lazycat,代码行数:18,代码来源:MessageInbound.java

示例14: getTable16KeyOffsets

import java.nio.CharBuffer; //导入依赖的package包/类
private char[] getTable16KeyOffsets(int offset) {
    int length = b16BitUnits.charAt(offset++);
    if(length > 0) {
        char[] result = new char[length];
        if(length <= 16) {
            for(int i = 0; i < length; ++i) {
                result[i] = b16BitUnits.charAt(offset++);
            }
        } else {
            CharBuffer temp = b16BitUnits.duplicate();
            temp.position(offset);
            temp.get(result);
        }
        return result;
    } else {
        return emptyChars;
    }
}
 
开发者ID:abhijitvalluri,项目名称:fitnotifications,代码行数:19,代码来源:ICUResourceBundleReader.java

示例15: grow

import java.nio.CharBuffer; //导入依赖的package包/类
/**
 * Returns a new CharBuffer identical to buf, except twice the capacity.
 */
private static CharBuffer grow(CharBuffer buf) {
    char[] copy = Arrays.copyOf(buf.array(), buf.capacity() * 2);
    CharBuffer bigger = CharBuffer.wrap(copy);
    bigger.position(buf.position());
    bigger.limit(buf.limit());
    return bigger;
}
 
开发者ID:FastBootWeixin,项目名称:FastBootWeixin,代码行数:11,代码来源:ReaderInputStream.java


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