本文整理匯總了Java中java.nio.CharBuffer.remaining方法的典型用法代碼示例。如果您正苦於以下問題:Java CharBuffer.remaining方法的具體用法?Java CharBuffer.remaining怎麽用?Java CharBuffer.remaining使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.CharBuffer
的用法示例。
在下文中一共展示了CharBuffer.remaining方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: nonStandardBytes
import java.nio.CharBuffer; //導入方法依賴的package包/類
private CoderResult nonStandardBytes(short newByte, CharBuffer cb)
{
CoderResult cr = CoderResult.UNDERFLOW;
if (nonStandardDecoder != null) {
//byteBuf[0] = (byte)newByte;
inBB.put((byte)newByte);
inBB.flip();
cr = nonStandardDecoder.decode(inBB, cb, false);
if (!inBB.hasRemaining()) {
inBB.clear();
} else {
int pos = inBB.limit();
inBB.clear();
inBB.position(pos);
}
} else if (cb.remaining() < replacement().length()) {
cb.put(replacement());
} else {
return CoderResult.OVERFLOW;
}
ext_offset++;
if (ext_offset >= ext_count) {
ext_offset = ext_count = 0;
state = NORMAL_BYTES;
cr = flushDecoder(nonStandardDecoder, cb);
nonStandardDecoder = null;
}
return cr;
}
示例2: urldecode
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
*
* @param content the portion to decode
* @param charset the charset to use
* @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
* @return encoded string
*/
private static String urldecode(
final String content,
final Charset charset,
final boolean plusAsBlank) {
if (content == null) {
return null;
}
ByteBuffer bb = ByteBuffer.allocate(content.length());
CharBuffer cb = CharBuffer.wrap(content);
while (cb.hasRemaining()) {
char c = cb.get();
if (c == '%' && cb.remaining() >= 2) {
char uc = cb.get();
char lc = cb.get();
int u = Character.digit(uc, 16);
int l = Character.digit(lc, 16);
if (u != -1 && l != -1) {
bb.put((byte) ((u << 4) + l));
} else {
bb.put((byte) '%');
bb.put((byte) uc);
bb.put((byte) lc);
}
} else if (plusAsBlank && c == '+') {
bb.put((byte) ' ');
} else {
bb.put((byte) c);
}
}
bb.flip();
return charset.decode(bb).toString();
}
示例3: read
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Override
public int read(CharBuffer target) throws IOException {
int len = target.remaining();
char[] cbuf = new char[len];
int n = read(cbuf, 0, len);
if (n > 0) {
target.put(cbuf, 0, n);
}
return n;
}
示例4: implFlush
import java.nio.CharBuffer; //導入方法依賴的package包/類
protected CoderResult implFlush(CharBuffer out) {
if(needFlushing) {
if (out.remaining() < 1) {
return CoderResult.OVERFLOW;
} else {
out.put(contextChar);
}
}
contextChar = INVALID_CHAR;
needFlushing = false;
return CoderResult.UNDERFLOW;
}
示例5: urldecode
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
*
* @param content the portion to decode
* @param charset the charset to use
* @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
* @return
*/
private static String urldecode(
final String content,
final Charset charset,
final boolean plusAsBlank) {
if (content == null) {
return null;
}
ByteBuffer bb = ByteBuffer.allocate(content.length());
CharBuffer cb = CharBuffer.wrap(content);
while (cb.hasRemaining()) {
char c = cb.get();
if (c == '%' && cb.remaining() >= 2) {
char uc = cb.get();
char lc = cb.get();
int u = Character.digit(uc, 16);
int l = Character.digit(lc, 16);
if (u != -1 && l != -1) {
bb.put((byte) ((u << 4) + l));
} else {
bb.put((byte) '%');
bb.put((byte) uc);
bb.put((byte) lc);
}
} else if (plusAsBlank && c == '+') {
bb.put((byte) ' ');
} else {
bb.put((byte) c);
}
}
bb.flip();
return charset.decode(bb).toString();
}
示例6: urlDecode
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Decode/unescape a portion of a URL, to use with the query part ensure {@code plusAsBlank} is true.
*
* @param content the portion to decode
* @param charset the charset to use
* @param plusAsBlank if {@code true}, then convert '+' to space (e.g. for www-url-form-encoded content), otherwise leave as is.
* @return encoded string
*/
private static String urlDecode(
final String content,
final Charset charset,
final boolean plusAsBlank) {
if (content == null) {
return null;
}
final ByteBuffer bb = ByteBuffer.allocate(content.length());
final CharBuffer cb = CharBuffer.wrap(content);
while (cb.hasRemaining()) {
final char c = cb.get();
if (c == '%' && cb.remaining() >= 2) {
final char uc = cb.get();
final char lc = cb.get();
final int u = Character.digit(uc, 16);
final int l = Character.digit(lc, 16);
if (u != -1 && l != -1) {
bb.put((byte) ((u << 4) + l));
} else {
bb.put((byte) '%');
bb.put((byte) uc);
bb.put((byte) lc);
}
} else if (plusAsBlank && c == '+') {
bb.put((byte) ' ');
} else {
bb.put((byte) c);
}
}
bb.flip();
return charset.decode(bb).toString();
}
示例7: generate
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Generates one or two UTF-16 characters to represent the given UCS-4
* character.
*
* @param uc The UCS-4 character
* @param len The number of input bytes from which the UCS-4 value
* was constructed (used when creating result objects)
* @param dst The destination buffer, to which one or two UTF-16
* characters will be written
*
* @return Either a positive count of the number of UTF-16 characters
* written to the destination buffer, or -1, in which case
* error() will return a descriptive result object
*/
public int generate(int uc, int len, CharBuffer dst) {
if (Character.isBmpCodePoint(uc)) {
char c = (char) uc;
if (Character.isSurrogate(c)) {
error = CoderResult.malformedForLength(len);
return -1;
}
if (dst.remaining() < 1) {
error = CoderResult.OVERFLOW;
return -1;
}
dst.put(c);
error = null;
return 1;
} else if (Character.isValidCodePoint(uc)) {
if (dst.remaining() < 2) {
error = CoderResult.OVERFLOW;
return -1;
}
dst.put(Character.highSurrogate(uc));
dst.put(Character.lowSurrogate(uc));
error = null;
return 2;
} else {
error = CoderResult.unmappableForLength(len);
return -1;
}
}
示例8: generate
import java.nio.CharBuffer; //導入方法依賴的package包/類
/**
* Generates one or two UTF-16 characters to represent the given UCS-4
* character.
*
* @param uc The UCS-4 character
* @param len The number of input bytes from which the UCS-4 value
* was constructed (used when creating result objects)
* @param dst The destination buffer, to which one or two UTF-16
* characters will be written
*
* @returns Either a positive count of the number of UTF-16 characters
* written to the destination buffer, or -1, in which case
* error() will return a descriptive result object
*/
public int generate(int uc, int len, CharBuffer dst) {
if (Character.isBmpCodePoint(uc)) {
char c = (char) uc;
if (Character.isSurrogate(c)) {
error = CoderResult.malformedForLength(len);
return -1;
}
if (dst.remaining() < 1) {
error = CoderResult.OVERFLOW;
return -1;
}
dst.put(c);
error = null;
return 1;
} else if (Character.isValidCodePoint(uc)) {
if (dst.remaining() < 2) {
error = CoderResult.OVERFLOW;
return -1;
}
dst.put(Character.highSurrogate(uc));
dst.put(Character.lowSurrogate(uc));
error = null;
return 2;
} else {
error = CoderResult.unmappableForLength(len);
return -1;
}
}
示例9: onCharReceived
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Override
protected void onCharReceived(final CharBuffer buf, final IOControl ioctrl) throws IOException {
try {
char[] charArray = new char[buf.remaining()];
System.arraycopy(buf.array(), 0, charArray, 0, buf.remaining());
CharBuffer charBuffer = CharBuffer.wrap(charArray);
ParseStream.blockingQueue.put(new AbstractMap.SimpleEntry<String, CharBuffer>(clientUri, charBuffer));
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
示例10: decodeBufferLoop
import java.nio.CharBuffer; //導入方法依賴的package包/類
private CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
int mark = src.position();
try {
while (src.hasRemaining()) {
char[] cc = null;
int b1 = src.get() & 0xff;
char c = decodeSingle(b1);
int inSize = 1, outSize = 1;
if (c == UNMAPPABLE) {
if (src.remaining() < 1)
return CoderResult.UNDERFLOW;
int b2 = src.get() & 0xff;
inSize++;
c = decodeDouble(b1, b2);
if (c == UNMAPPABLE) {
cc = decodeDoubleEx(b1, b2);
if (cc == null) {
if (decodeSingle(b2) == UNMAPPABLE)
return CoderResult.unmappableForLength(2);
else
return CoderResult.unmappableForLength(1);
}
outSize++;
}
}
if (dst.remaining() < outSize)
return CoderResult.OVERFLOW;
if (outSize == 2) {
dst.put(cc[0]);
dst.put(cc[1]);
} else {
dst.put(c);
}
mark += inSize;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(mark);
}
}
示例11: normalBytes
import java.nio.CharBuffer; //導入方法依賴的package包/類
private CoderResult normalBytes(short newByte, CharBuffer cb) {
CoderResult cr = CoderResult.UNDERFLOW;
if ((newByte >= 0x00 && newByte <= 0x1F) || // C0
(newByte >= 0x80 && newByte <= 0x9F)) { // C1
char newChar;
switch (newByte) {
case 0x1B:
state = ESCAPE_SEQUENCE;
queue.write(newByte);
return cr;
case 0x9B:
state = CONTROL_SEQUENCE_PIF;
versionSequenceAllowed = false;
queue.write(newByte);
return cr;
case 0x09:
versionSequenceAllowed = false;
newChar = '\t';
break;
case 0x0A:
versionSequenceAllowed = false;
newChar = '\n';
break;
default:
versionSequenceAllowed = false;
return cr;
}
if (!cb.hasRemaining())
return CoderResult.OVERFLOW;
else
cb.put(newChar);
} else {
CharsetDecoder decoder;
boolean high;
versionSequenceAllowed = false;
if (newByte >= 0x20 && newByte <= 0x7F) {
decoder = glDecoder;
high = glHigh;
} else /* if (newByte >= 0xA0 && newByte <= 0xFF) */ {
decoder = grDecoder;
high = grHigh;
}
if (lastDecoder != null && decoder != lastDecoder) {
cr = flushDecoder(lastDecoder, cb);
}
lastDecoder = decoder;
if (decoder != null) {
byte b = (byte)newByte;
if (high) {
b |= 0x80;
} else {
b &= 0x7F;
}
inBB.put(b);
inBB.flip();
cr = decoder.decode(inBB, cb, false);
if (!inBB.hasRemaining() || cr.isMalformed()) {
inBB.clear();
} else {
int pos = inBB.limit();
inBB.clear();
inBB.position(pos);
}
} else if (cb.remaining() < replacement().length()) {
cb.put(replacement());
} else {
return CoderResult.OVERFLOW;
}
}
return cr;
}
示例12: decodeNotHasArray
import java.nio.CharBuffer; //導入方法依賴的package包/類
private CoderResult decodeNotHasArray(ByteBuffer in, CharBuffer out) {
int outRemaining = out.remaining();
int pos = in.position();
int limit = in.limit();
try {
while (pos < limit) {
if (outRemaining == 0) {
return CoderResult.OVERFLOW;
}
int jchar = in.get();
if (jchar < 0) {
jchar = jchar & 0x7F;
int tail = remainingBytes[jchar];
if (tail == -1) {
return CoderResult.malformedForLength(1);
}
if (limit - pos < 1 + tail) {
// No early test for invalid sequences here as peeking
// at the next byte is harder
return CoderResult.UNDERFLOW;
}
int nextByte;
for (int i = 0; i < tail; i++) {
nextByte = in.get() & 0xFF;
if ((nextByte & 0xC0) != 0x80) {
return CoderResult.malformedForLength(1 + i);
}
jchar = (jchar << 6) + nextByte;
}
jchar -= remainingNumbers[tail];
if (jchar < lowerEncodingLimit[tail]) {
// Should have been encoded in a fewer octets
return CoderResult.malformedForLength(1);
}
pos += tail;
}
// Apache Tomcat added test
if (jchar >= 0xD800 && jchar <= 0xDFFF) {
return CoderResult.unmappableForLength(3);
}
// Apache Tomcat added test
if (jchar > 0x10FFFF) {
return CoderResult.unmappableForLength(4);
}
if (jchar <= 0xffff) {
out.put((char) jchar);
outRemaining--;
} else {
if (outRemaining < 2) {
return CoderResult.OVERFLOW;
}
out.put((char) ((jchar >> 0xA) + 0xD7C0));
out.put((char) ((jchar & 0x3FF) + 0xDC00));
outRemaining -= 2;
}
pos++;
}
return CoderResult.UNDERFLOW;
} finally {
in.position(pos);
}
}
示例13: decodeBufferLoop
import java.nio.CharBuffer; //導入方法依賴的package包/類
protected CoderResult decodeBufferLoop(ByteBuffer src, CharBuffer dst) {
int mark = src.position();
try {
while (src.hasRemaining()) {
char[] cc = null;
int b1 = src.get() & 0xff;
int inSize = 1, outSize = 1;
char c = decodeSingle(b1);
if (c == UNMAPPABLE_DECODING) {
if (src.remaining() < 1)
return CoderResult.UNDERFLOW;
int b2 = src.get() & 0xff;
inSize++;
if (b2 < b2Min || b2 > b2Max)
return CoderResult.unmappableForLength(2);
c = decodeDouble(b1, b2); //bmp
if (c == UNMAPPABLE_DECODING) {
c = decodeDoubleEx(b1, b2); //supp
if (c == UNMAPPABLE_DECODING) {
c = decodeBig5(b1, b2); //big5
if (c == UNMAPPABLE_DECODING)
return CoderResult.unmappableForLength(2);
} else {
outSize = 2;
}
}
}
if (dst.remaining() < outSize)
return CoderResult.OVERFLOW;
if (outSize == 2) {
dst.put(Surrogate.high(0x20000 + c));
dst.put(Surrogate.low(0x20000 + c));
} else {
dst.put(c);
}
mark += inSize;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(mark);
}
}
示例14: decodeBufferLoop
import java.nio.CharBuffer; //導入方法依賴的package包/類
private CoderResult decodeBufferLoop(ByteBuffer src,
CharBuffer dst)
{
int mark = src.position();
int b1 = 0, b2 = 0;
int inputSize = 0;
char outputChar = REPLACE_CHAR; // U+FFFD;
try {
while (src.hasRemaining()) {
b1 = src.get() & 0xff;
inputSize = 1;
if ((b1 & 0x80) == 0) {
outputChar = (char)b1;
} else { // Multibyte char
if ((b1 & 0xff) == 0x8f) { // JIS0212
if (src.remaining() < 2)
return CoderResult.UNDERFLOW;
b1 = src.get() & 0xff;
b2 = src.get() & 0xff;
inputSize += 2;
outputChar = decode0212(b1-0x80, b2-0x80);
} else {
// JIS0208
if (src.remaining() < 1)
return CoderResult.UNDERFLOW;
b2 = src.get() & 0xff;
inputSize++;
outputChar = decodeDouble(b1, b2);
}
}
if (outputChar == REPLACE_CHAR) {
return CoderResult.unmappableForLength(inputSize);
}
if (dst.remaining() < 1)
return CoderResult.OVERFLOW;
dst.put(outputChar);
mark += inputSize;
}
return CoderResult.UNDERFLOW;
} finally {
src.position(mark);
}
}
示例15: run
import java.nio.CharBuffer; //導入方法依賴的package包/類
@Override
public void run() {
logger.debug("Starting connection handler");
Event event = null;
try {
Reader reader = Channels.newReader(socketChannel, sourceEncoding);
Writer writer = Channels.newWriter(socketChannel, sourceEncoding);
CharBuffer buffer = CharBuffer.allocate(maxLineLength);
buffer.flip(); // flip() so fill() sees buffer as initially empty
while (true) {
// this method blocks until new data is available in the socket
int charsRead = fill(buffer, reader);
logger.debug("Chars read = {}", charsRead);
// attempt to process all the events in the buffer
int eventsProcessed = processEvents(buffer, writer);
logger.debug("Events processed = {}", eventsProcessed);
if (charsRead == -1) {
// if we received EOF before last event processing attempt, then we
// have done everything we can
break;
} else if (charsRead == 0 && eventsProcessed == 0) {
if (buffer.remaining() == buffer.capacity()) {
// If we get here it means:
// 1. Last time we called fill(), no new chars were buffered
// 2. After that, we failed to process any events => no newlines
// 3. The unread data in the buffer == the size of the buffer
// Therefore, we are stuck because the client sent a line longer
// than the size of the buffer. Response: Drop the connection.
logger.warn("Client sent event exceeding the maximum length");
counterGroup.incrementAndGet("events.failed");
writer.write("FAILED: Event exceeds the maximum length (" +
buffer.capacity() + " chars, including newline)\n");
writer.flush();
break;
}
}
}
socketChannel.close();
counterGroup.incrementAndGet("sessions.completed");
} catch (IOException e) {
counterGroup.incrementAndGet("sessions.broken");
try {
socketChannel.close();
} catch (IOException ex) {
logger.error("Unable to close socket channel. Exception follows.", ex);
}
}
logger.debug("Connection handler exiting");
}