java.nio.CharBuffer Java Examples

The following examples show how to use java.nio.CharBuffer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Surrogate.java    From shortyz with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Parses a UCS-4 character from the given source buffer, handling
 * surrogates.
 *
 * @param  c    The first character
 * @param  in   The source buffer, from which one more character
 *              will be consumed if c is a high surrogate
 *
 * @returns  Either a parsed UCS-4 character, in which case the isPair()
 *           and increment() methods will return meaningful values, or
 *           -1, in which case error() will return a descriptive result
 *           object
 */
public int parse(char c, CharBuffer in) {
    if (Surrogate.isHigh(c)) {
        if (!in.hasRemaining()) {
            error = CoderResult.UNDERFLOW;
            return -1;
        }
        char d = in.get();
        if (Surrogate.isLow(d)) {
            character = toUCS4(c, d);
            isPair = true;
            error = null;
            return character;
        }
        error = CoderResult.malformedForLength(1);
        return -1;
    }
    if (Surrogate.isLow(c)) {
        error = CoderResult.malformedForLength(1);
        return -1;
    }
    character = c;
    isPair = false;
    error = null;
    return character;
}
 
Example #2
Source File: SingleByte.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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);
    }
}
 
Example #3
Source File: OutputBuffer.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
/**
 * Convert the chars to bytes, then send the data to the client.
 *
 * @param from Char buffer to be written to the response
 *
 * @throws IOException An underlying IOException occurred
 */
public void realWriteChars(CharBuffer from) throws IOException {

    while (from.remaining() > 0) {
        conv.convert(from, bb);
        if (bb.remaining() == 0) {
            // Break out of the loop if more chars are needed to produce any output
            break;
        }
        if (from.remaining() > 0) {
            flushByteBuffer();
        } else if (conv.isUndeflow() && bb.limit() > bb.capacity() - 4) {
            // Handle an edge case. There are no more chars to write at the
            // moment but there is a leftover character in the converter
            // which must be part of a surrogate pair. The byte buffer does
            // not have enough space left to output the bytes for this pair
            // once it is complete )it will require 4 bytes) so flush now to
            // prevent the bytes for the leftover char and the rest of the
            // surrogate pair yet to be written from being lost.
            // See TestOutputBuffer#testUtf8SurrogateBody()
            flushByteBuffer();
        }
    }

}
 
Example #4
Source File: WrapProcess.java    From AndroidRipper with GNU Affero General Public License v3.0 6 votes vote down vote up
private void connect(final Readable source, final Appendable sink) {
	Thread thread = new Thread(new Runnable() {
		public void run() {
			CharBuffer cb = CharBuffer.wrap(new char [256]);
			try {
				while (source.read(cb) != -1) {
					cb.flip();
					sink.append(cb);
					cb.clear();
				}

				if (sink instanceof Flushable) {
					((Flushable)sink).flush();
				}
			} catch (IOException e) { /* prolly broken pipe, just die */ }
		}
	});
	thread.setDaemon(true);
	thread.start();
}
 
Example #5
Source File: BURL.java    From BUbiNG with Apache License 2.0 6 votes vote down vote up
/** If the argument string does not contain non-ASCII characters, returns the string itself;
 * otherwise, encodes non-ASCII characters by %XX-encoded UTF-8 sequences.
 *
 * @param s a string.
 * @return <code>c</code> with non-ASCII characters replaced by %XX-encoded UTF-8 sequences.
 */
private static String sanitize(final String s) {
   	int i = s.length();
       for(i = s.length(); i-- != 0;) if (s.charAt(i) >= (char)128) break;
       if (i == -1) return s;

	final ByteBuffer byteBuffer = Charsets.UTF_8.encode(CharBuffer.wrap(s));
	final StringBuilder stringBuilder = new StringBuilder();

	while (byteBuffer.hasRemaining()) {
		final int b = byteBuffer.get() & 0xff;
		if (b >= 0x80) stringBuilder.append('%').append(HEX_DIGIT[b >> 4 & 0xf]).append(HEX_DIGIT[b & 0xf]);
		else stringBuilder.append((char)b);
	}

	return stringBuilder.toString();
}
 
Example #6
Source File: SingleByte.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            char c = src.get();
            int b = encode(c);
            if (b == UNMAPPABLE_ENCODING) {
                if (Character.isSurrogate(c)) {
                    if (sgp == null)
                        sgp = new Surrogate.Parser();
                    if (sgp.parse(c, src) < 0)
                        return sgp.error();
                    return sgp.unmappableResult();
                }
                return CoderResult.unmappableForLength(1);
            }
            if (!dst.hasRemaining())
                return CoderResult.OVERFLOW;
            dst.put((byte)b);
            mark++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example #7
Source File: ZipCoder.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
String toString(byte[] ba, int length) {
    CharsetDecoder cd = decoder().reset();
    int len = (int)(length * cd.maxCharsPerByte());
    char[] ca = new char[len];
    if (len == 0)
        return new String(ca);
    // UTF-8 only for now. Other ArrayDeocder only handles
    // CodingErrorAction.REPLACE mode. ZipCoder uses
    // REPORT mode.
    if (isUTF8 && cd instanceof ArrayDecoder) {
        int clen = ((ArrayDecoder)cd).decode(ba, 0, length, ca);
        if (clen == -1)    // malformed
            throw new IllegalArgumentException("MALFORMED");
        return new String(ca, 0, clen);
    }
    ByteBuffer bb = ByteBuffer.wrap(ba, 0, length);
    CharBuffer cb = CharBuffer.wrap(ca);
    CoderResult cr = cd.decode(bb, cb, true);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    cr = cd.flush(cb);
    if (!cr.isUnderflow())
        throw new IllegalArgumentException(cr.toString());
    return new String(ca, 0, cb.position());
}
 
Example #8
Source File: AppCacheManifestTransformer.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public Mono<Resource> transform(ServerWebExchange exchange, Resource inputResource,
		ResourceTransformerChain chain) {

	return chain.transform(exchange, inputResource)
			.flatMap(outputResource -> {
				String name = outputResource.getFilename();
				if (!this.fileExtension.equals(StringUtils.getFilenameExtension(name))) {
					return Mono.just(outputResource);
				}
				DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
				Flux<DataBuffer> flux = DataBufferUtils
						.read(outputResource, bufferFactory, StreamUtils.BUFFER_SIZE);
				return DataBufferUtils.join(flux)
						.flatMap(dataBuffer -> {
							CharBuffer charBuffer = DEFAULT_CHARSET.decode(dataBuffer.asByteBuffer());
							DataBufferUtils.release(dataBuffer);
							String content = charBuffer.toString();
							return transform(content, outputResource, chain, exchange);
						});
			});
}
 
Example #9
Source File: OsglConfig.java    From java-tool with Apache License 2.0 6 votes vote down vote up
@Override
public boolean test(Readable readable) {
    CharBuffer buf = CharBuffer.allocate(100);
    try {
        int n = readable.read(buf);
        if (n < 0) {
            return false;
        }
        buf.flip();
        for (int i = 0; i < n; ++i) {
            char c = buf.charAt(i);
            if (Character.isISOControl(c)) {
                if (c != '\n' && c != '\r') {
                    return true;
                }
            }
        }
        return false;
    } catch (IOException e) {
        throw E.ioException(e);
    } finally {
        buf.clear();
    }
}
 
Example #10
Source File: ContentEquals.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void testCharSequence() throws Exception {
    for (int i=0; i<ITERATIONS; i++) {
        int length = rnd.nextInt(STR_LEN) + 1;
        StringBuilder testStringBuilder = new StringBuilder();
        for(int x=0; x<length; x++) {
            char aChar = (char)rnd.nextInt();
            testStringBuilder.append(aChar);
        }
        String testString = testStringBuilder.toString();
        char c = testStringBuilder.charAt(0);
        testStringBuilder.setCharAt(0, 'c');
        testStringBuilder.setCharAt(0, c);
        CharBuffer buf = CharBuffer.wrap(testStringBuilder.toString());
        if (!testString.contentEquals(buf))
            throw new RuntimeException("ContentsEqual failure");
    }
}
 
Example #11
Source File: SingleByteEncoder.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
    int mark = src.position();
    try {
        while (src.hasRemaining()) {
            char c = src.get();
            if (Surrogate.is(c)) {
                if (sgp.parse(c, src) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);
            if (!dst.hasRemaining())
                return CoderResult.OVERFLOW;

            char e = index2.charAt(index1[(c & mask1) >> shift]
                                   + (c & mask2));

            // If output byte is zero because input char is zero
            // then character is mappable, o.w. fail
            if (e == '\u0000' && c != '\u0000')
                return CoderResult.unmappableForLength(1);

            mark++;
            dst.put((byte)e);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example #12
Source File: StorageCommand.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
private ByteBuffer processAsciiCommand(ByteBuffer buffer, Cache cache) {
  CharBuffer flb = getFirstLineBuffer();
  getAsciiDecoder().decode(buffer, flb, false);
  flb.flip();
  String firstLine = getFirstLine();
  String[] firstLineElements = firstLine.split(" ");
  String key = firstLineElements[1];
  int flags = Integer.parseInt(firstLineElements[2]);
  long expTime = Long.parseLong(firstLineElements[3]);
  int numBytes = Integer.parseInt(stripNewline(firstLineElements[4]));
  boolean noReply = false;
  if (firstLineElements.length > 5) {
    noReply = true;
  }
  byte[] value = new byte[numBytes];
  buffer.position(firstLine.length());
  try {
    for (int i=0; i<numBytes; i++) {
      value[i] = buffer.get();
    }
  } catch (BufferUnderflowException e) {
    throw new ClientError("error reading value");
  }
  if (getLogger().fineEnabled()) {
    getLogger().fine("key:"+key);
    getLogger().fine("value:"+Arrays.toString(value));
  }
  ByteBuffer retVal = processStorageCommand(key, value, flags, cache);
  if (expTime > 0) {
    scheduleExpiration(key, expTime, cache);
  }
  return noReply ? null : retVal;
}
 
Example #13
Source File: X11Dingbats.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;

            if (!canEncode(c))
                return CoderResult.unmappableForLength(1);
            sp++;
            if (c >= 0x2761){
                da[dp++] = table[c - 0x2761]; // table lookup
            } else {
                da[dp++] = (byte)(c + 0x20 - 0x2700); // direct map
            }
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example #14
Source File: StaEDIStreamReader.java    From staedi with Apache License 2.0 5 votes vote down vote up
@Override
public String getText() {
    ensureOpen();
    checkTextState();
    final CharBuffer buffer = getBuffer();

    return buffer.toString();
}
 
Example #15
Source File: DBCS_ONLY_IBM_EBCDIC_Decoder.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp + 1 < sl) {
            int b1 = sa[sp] & 0xff;
            int b2 = sa[sp + 1] & 0xff;

            if (!isValidDoubleByte(b1, b2)) {
                return CoderResult.malformedForLength(2);
            }
            // Lookup in the two level index
            int v = b1 * 256 + b2;
            char outputChar = index2.charAt(index1[((v & mask1) >> shift)]
                                            + (v & mask2));
            if (outputChar == REPLACE_CHAR)
                return CoderResult.unmappableForLength(2);
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += 2;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example #16
Source File: StreamEncoder.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
public void write(CharBuffer cb) throws IOException {
    int position = cb.position();
    try {
        synchronized (lock) {
            ensureOpen();
            implWrite(cb);
        }
    } finally {
        cb.position(position);
    }
}
 
Example #17
Source File: ISO_8859_1.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private CoderResult decodeArrayLoop(ByteBuffer src,
                                    CharBuffer dst)
{
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            byte b = sa[sp];
            if (dp >= dl)
                return CoderResult.OVERFLOW;
            da[dp++] = (char)(b & 0xff);
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example #18
Source File: CodeSetConversion.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private void convertCharArray() {
    try {

        // Possible optimization of directly converting into the CDR buffer.
        // However, that means the CDR code would have to reserve
        // a 4 byte string length ahead of time, and we'd need a
        // confusing partial conversion scheme for when we couldn't
        // fit everything in the buffer but needed to know the
        // converted length before proceeding due to fragmentation.
        // Then there's the issue of the chunking code.
        //
        // For right now, this is less messy and basic tests don't
        // show more than a 1 ms penalty worst case.  Less than a
        // factor of 2 increase.

        // Convert the characters
        buffer = ctb.encode(CharBuffer.wrap(chars,0,numChars));

        // ByteBuffer returned by the encoder will set its limit
        // to byte immediately after the last written byte.
        numBytes = buffer.limit();

    } catch (IllegalStateException ise) {
        // an encoding operation is already in progress
        throw wrapper.ctbConverterFailure( ise ) ;
    } catch (MalformedInputException mie) {
        // There were illegal Unicode char pairs
        throw wrapper.badUnicodePair( mie ) ;
    } catch (UnmappableCharacterException uce) {
        // A character doesn't map to the desired code set
        // CORBA formal 00-11-03.
        throw omgWrapper.charNotInCodeset( uce ) ;
    } catch (CharacterCodingException cce) {
        // If this happens, then some other encoding error occured
        throw wrapper.ctbConverterFailure( cce ) ;
    }
}
 
Example #19
Source File: ISO_8859_1.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src,
                                 ByteBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return encodeArrayLoop(src, dst);
    else
        return encodeBufferLoop(src, dst);
}
 
Example #20
Source File: ColumnImpl.java    From jackcess with Apache License 2.0 5 votes vote down vote up
/**
 * @param text Text to encode
 * @param charset database charset
 * @return A buffer with the text encoded
 * @usage _advanced_method_
 */
public static ByteBuffer encodeUncompressedText(CharSequence text,
                                                Charset charset)
{
  CharBuffer cb = ((text instanceof CharBuffer) ?
                   (CharBuffer)text : CharBuffer.wrap(text));
  return charset.encode(cb);
}
 
Example #21
Source File: DkCrypto.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
static byte[] charToUtf8(char[] chars) {
    Charset utf8 = Charset.forName("UTF-8");

    CharBuffer cb = CharBuffer.wrap(chars);
    ByteBuffer bb = utf8.encode(cb);
    int len = bb.limit();
    byte[] answer = new byte[len];
    bb.get(answer, 0, len);
    return answer;
}
 
Example #22
Source File: StringEqualsFactory.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Matcher<T> create(List<Pair<String, T>> pairs, Pair<String, T> defaultPattern) {
    Map<CharBuffer, T> map = pairs.stream().sequential().collect(Collectors.toMap(k -> CharBuffer.wrap(k.getKey()), v -> v.getValue()));
    return (buffer, context) -> {
        T t1 = map.get(buffer);
        if (t1 != null) {
            return Collections.singletonList(t1);
        }
        return Collections.singletonList(defaultPattern.getValue());
    };
}
 
Example #23
Source File: BuiltInEncodingAlgorithm.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void matchWhiteSpaceDelimnatedWords(CharBuffer cb, WordListener wl) {
    Matcher m = SPACE_PATTERN.matcher(cb);
    int i = 0;
    int s = 0;
    while(m.find()) {
        s = m.start();
        if (s != i) {
            wl.word(i, s);
        }
        i = m.end();
    }
    if (i != cb.length())
        wl.word(i, cb.length());
}
 
Example #24
Source File: X11CNS11643.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    char[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (c >= '\uFFFE' || c <= '\u007f')
                return CoderResult.unmappableForLength(1);
            int cns = getNative(c);
            int p = cns >> 16;
            if (p == 1 && plane == 0 ||
                p == 2 && plane == 2 ||
                p == 3 && plane == 3) {
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) ((cns  >> 8) & 0x7f);
                da[dp++] = (byte) (cns & 0x7f);
                sp++;
                continue;
            }
            return CoderResult.unmappableForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example #25
Source File: DoubleByte.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();

    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();

    try {
        while (sp < sl && dp < dl) {
            // inline the decodeSingle/Double() for better performance
            int inSize = 1;
            int b1 = sa[sp] & 0xff;
            char c = b2cSB[b1];
            if (c == UNMAPPABLE_DECODING) {
                if (sl - sp < 2)
                    return crMalformedOrUnderFlow(b1);
                int b2 = sa[sp + 1] & 0xff;
                if (b2 < b2Min || b2 > b2Max ||
                    (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
                    return crMalformedOrUnmappable(b1, b2);
                }
                inSize++;
            }
            da[dp++] = c;
            sp += inSize;
        }
        return (sp >= sl) ? CoderResult.UNDERFLOW
                          : CoderResult.OVERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example #26
Source File: ISCII91.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src,
                                 CharBuffer dst)
{
    if (src.hasArray() && dst.hasArray())
        return decodeArrayLoop(src, dst);
    else
        return decodeBufferLoop(src, dst);
}
 
Example #27
Source File: FixLenCharDataParser.java    From CloverETL-Engine with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Create instance for specified charset.
 * @param charset
 */
public FixLenCharDataParser(DataRecordMetadata metadata, String charset) {		
	super(metadata, charset);
	this.metadata = metadata;
	charBuffer = CharBuffer.allocate(Defaults.DEFAULT_INTERNAL_IO_BUFFER_SIZE);
	setRecordDelimiters(null);
}
 
Example #28
Source File: Snake.java    From tomcatsrc with Apache License 2.0 5 votes vote down vote up
private synchronized void kill() {
    resetState();
    try {
        CharBuffer response = CharBuffer.wrap("{'type': 'dead'}");
        outbound.writeTextMessage(response);
    } catch (IOException ioe) {
        // Ignore
    }
}
 
Example #29
Source File: EncoderTestSuiteBuilder.java    From owasp-java-encoder with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Checks boundary conditions of CharBuffer based encodes.
 *
 * @param expected the expected output
 * @param input the input to encode
 */
private void checkBoundaryEncodes(String expected, String input) {
    final CharBuffer in = CharBuffer.wrap(input.toCharArray());
    final int n = expected.length();
    final CharBuffer out = CharBuffer.allocate(n);
    for (int i=0 ; i<n ; ++i) {
        out.clear();
        out.position(n - i);
        in.clear();

        CoderResult cr = _encoder.encode(in, out, true);
        out.limit(out.position()).position(n - i);
        out.compact();
        if (cr.isOverflow()) {
            CoderResult cr2 = _encoder.encode(in, out, true);
            if (!cr2.isUnderflow()) {
                Assert.fail("second encode should finish at offset = "+i);
            }
        }
        out.flip();

        String actual = out.toString();
        if (!expected.equals(actual)) {
            Assert.assertEquals("offset = "+i, expected, actual);
        }
    }
}
 
Example #30
Source File: X11KSC5601.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
    byte[] sa = src.array();
    int sp = src.arrayOffset() + src.position();
    int sl = src.arrayOffset() + src.limit();
    assert (sp <= sl);
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    assert (dp <= dl);
    dp = (dp <= dl ? dp : dl);


    try {
        while (sp < sl) {
            if ( sl - sp < 2) {
                return CoderResult.UNDERFLOW;
            }
            int b1 = sa[sp] & 0xFF | 0x80;
            int b2 = sa[sp + 1] & 0xFF | 0x80;
            char c = decodeDouble(b1, b2);
            if (c == UNMAPPABLE_DECODING) {
                return CoderResult.unmappableForLength(2);
            }
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = c;
            sp +=2;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }

}