本文整理匯總了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);
}
}
示例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();
}
示例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) {
}
}
示例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));
}
示例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());
}
示例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;
}
示例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();
}
示例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;
}
}
示例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;
}
}
示例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;
}
示例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);
}
}
示例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);
}
}
示例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;
}
示例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;
}
}
示例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;
}