Java Code Examples for java.nio.CharBuffer#position()

The following examples show how to use java.nio.CharBuffer#position() . 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: StringUtils.java    From tajo with Apache License 2.0 6 votes vote down vote up
public static char[] convertBytesToChars(byte[] src, Charset charset) {
  CharsetDecoder decoder = charset.newDecoder();
  char[] resultArray = new char[(int) (src.length * decoder.maxCharsPerByte())];
  
  if (src.length != 0) {
    ByteBuffer byteBuffer = ByteBuffer.wrap(src);
    CharBuffer charBuffer = CharBuffer.wrap(resultArray);
    
    decoder.onMalformedInput(CodingErrorAction.REPLACE).onUnmappableCharacter(CodingErrorAction.REPLACE);
    decoder.reset();
    
    CoderResult coderResult = decoder.decode(byteBuffer, charBuffer, true);
    if (coderResult.isUnderflow()) {
      coderResult = decoder.flush(charBuffer);
      
      if (coderResult.isUnderflow()) {
        if (resultArray.length != charBuffer.position()) {
          resultArray = Arrays.copyOf(resultArray, charBuffer.position());
        }
      }
    }
  }
  
  return resultArray;
}
 
Example 2
Source File: SingleByte.java    From jdk8u-jdk 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 3
Source File: SingleByte.java    From jdk8u-jdk 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 4
Source File: ExpressionParser.java    From jlibs with Apache License 2.0 6 votes vote down vote up
private static String parseIdentifier(CharBuffer buffer) throws ParseException{
    if(!buffer.hasRemaining())
        throw new ParseException("Unexpected EOF", buffer.position());

    StringBuilder builder = new StringBuilder();
    char ch = buffer.get();
    if(!Character.isJavaIdentifierStart(ch))
        throw new ParseException("Invalid Identifier", buffer.position()-1);
    builder.append(ch);
    while(buffer.hasRemaining()){
        ch = buffer.get();
        if(Character.isJavaIdentifierPart(ch))
            builder.append(ch);
        else{
            buffer.position(buffer.position()-1);
            break;
        }
    }
    return builder.toString();
}
 
Example 5
Source File: TelnetCharset.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
@Override
public CharsetDecoder newDecoder() {
  return new CharsetDecoder(this, 1.0f, 1.0f) {
    private boolean prevCR;
    @Override
    protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {
      int pos = in.position();
      int limit = in.limit();
      try {
        while (pos < limit) {
          byte b = in.get(pos);
          char c;
          if (b >= 0) {
            if (prevCR && (b == '\n' || b == 0)) {
              pos++;
              prevCR = false;
              continue;
            }
            c = (char) b;
            prevCR = b == '\r';
          } else {
            c = (char)(256 + b);
          }
          if (out.position() >= out.limit()) {
            return CoderResult.OVERFLOW;
          }
          pos++;
          out.put(c);
        }
        return CoderResult.UNDERFLOW;
      } finally {
        in.position(pos);
      }
    }
  };
}
 
Example 6
Source File: CharArrayUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nullable
public static char[] fromSequenceWithoutCopying(@Nullable CharSequence seq) {
  if (seq instanceof CharSequenceBackedByArray) {
    return ((CharSequenceBackedByArray)seq).getChars();
  }

  if (seq instanceof CharBuffer) {
    final CharBuffer buffer = (CharBuffer)seq;
    if (buffer.hasArray() && !buffer.isReadOnly() && buffer.arrayOffset() == 0 && buffer.position() == 0) {
      return buffer.array();
    }
  }

  return null;
}
 
Example 7
Source File: X11KSC5601_OLD.java    From hottub 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 == replacement().charAt(0)) {
                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());
    }

}
 
Example 8
Source File: DBCS_IBM_ASCII_Encoder.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeArrayLoop(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();
    int outputSize = 0;             // size of output

    try {
        while (sp < sl) {
            int index;
            int theBytes;
            char c = sa[sp];
            if (Surrogate.is(c)) {
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);

            index = index1[((c & mask1) >> shift)]
                            + (c & mask2);
            if (index < 15000)
                theBytes = (int)(index2.charAt(index));
            else
                theBytes = (int)(index2a.charAt(index-15000));
            b1 = (byte)((theBytes & 0x0000ff00)>>8);
            b2 = (byte)(theBytes & 0x000000ff);

            if (b1 == 0x00 && b2 == 0x00
                && c != '\u0000') {
                    return CoderResult.unmappableForLength(1);
            }

            if (b1 == 0) {
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) b2;
            } else {
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) b1;
                da[dp++] = (byte) b2;
            }
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 9
Source File: DoubleByteEncoder.java    From openjdk-jdk8u with GNU General Public License v2.0 4 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 (Character.isSurrogate(c)) {
                int surr;
                if ((surr = sgp.parse(c, src)) < 0)
                    return sgp.error();
                char c2 = Surrogate.low(surr);
                byte[] outputBytes = new byte[2];
                outputBytes = encodeSurrogate(c, c2);

                if (outputBytes == null) {
                    return sgp.unmappableResult();
                } else {
                    if (dst.remaining() < 2)
                        return CoderResult.OVERFLOW;
                    mark += 2;
                    dst.put(outputBytes[0]);
                    dst.put(outputBytes[1]);
                    continue;
                }
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);
            int b = encodeSingle(c);

            if (b != -1) { // Single-byte character
                if (dst.remaining() < 1)
                    return CoderResult.OVERFLOW;
                mark++;
                dst.put((byte)b);
                continue;
            }
            // Double Byte character

            int ncode = encodeDouble(c);
            if (ncode != 0 && c != '\u0000') {
                if (dst.remaining() < 2)
                    return CoderResult.OVERFLOW;
                mark++;
                dst.put((byte) ((ncode & 0xff00) >> 8));
                dst.put((byte) ncode);
                continue;
            }
            return CoderResult.unmappableForLength(1);
        }

        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 10
Source File: DBCS_IBM_ASCII_Decoder.java    From TencentKona-8 with GNU General Public License v2.0 4 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) {
            int b1, b2;
            b1 = sa[sp];
            int inputSize = 1;
            int v = 0;
            char outputChar = REPLACE_CHAR;
            if (b1 < 0)
                b1 += 256;

            if (!leadByte[b1])
            {
              outputChar = singleByteToChar.charAt(b1);
            } else {
                if (sl - sp < 2)
                    return CoderResult.UNDERFLOW;
                b2 = sa[sp + 1];
                if (b2 < 0)
                    b2 += 256;

                inputSize++;

                // Lookup in the two level index
                v = b1 * 256 + b2;
                outputChar = index2.charAt(index1[((v & mask1) >> shift)]
                                            + (v & mask2));
            }
            if (outputChar == '\uFFFD')
                return CoderResult.unmappableForLength(inputSize);

            if (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 11
Source File: CESU_8.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
private static CoderResult overflow(CharBuffer src, int mark) {
    src.position(mark);
    return CoderResult.OVERFLOW;
}
 
Example 12
Source File: CESU_8.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeBufferLoop(CharBuffer src,
                                     ByteBuffer dst)
{
    int mark = src.position();
    while (src.hasRemaining()) {
        char c = src.get();
        if (c < 0x80) {
            // Have at most seven bits
            if (!dst.hasRemaining())
                return overflow(src, mark);
            dst.put((byte)c);
        } else if (c < 0x800) {
            // 2 bytes, 11 bits
            if (dst.remaining() < 2)
                return overflow(src, mark);
            dst.put((byte)(0xc0 | (c >> 6)));
            dst.put((byte)(0x80 | (c & 0x3f)));
        } else if (Character.isSurrogate(c)) {
            // Have a surrogate pair
            if (sgp == null)
                sgp = new Surrogate.Parser();
            int uc = sgp.parse(c, src);
            if (uc < 0) {
                src.position(mark);
                return sgp.error();
            }
            if (dst.remaining() < 6)
                return overflow(src, mark);
            to3Bytes(dst, Character.highSurrogate(uc));
            to3Bytes(dst, Character.lowSurrogate(uc));
            mark++;  // 2 chars
        } else {
            // 3 bytes, 16 bits
            if (dst.remaining() < 3)
                return overflow(src, mark);
            to3Bytes(dst, c);
        }
        mark++;
    }
    src.position(mark);
    return CoderResult.UNDERFLOW;
}
 
Example 13
Source File: DBCS_IBM_ASCII_Encoder.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeBufferLoop(CharBuffer src, ByteBuffer dst) {
    int mark = src.position();
    int outputSize = 0;             // size of output

    try {
        while (src.hasRemaining()) {
            int index;
            int theBytes;
            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);

            index = index1[((c & mask1) >> shift)]
                            + (c & mask2);
            if (index < 15000)
                theBytes = (int)(index2.charAt(index));
            else
                theBytes = (int)(index2a.charAt(index-15000));
            b1 = (byte)((theBytes & 0x0000ff00)>>8);
            b2 = (byte)(theBytes & 0x000000ff);

            if (b1 == 0x00 && b2 == 0x00
                && c != '\u0000') {
                    return CoderResult.unmappableForLength(1);
            }

            if (b1 == 0) {
                if (dst.remaining() < 1)
                    return CoderResult.OVERFLOW;
                dst.put((byte) b2);
            } else {
                if (dst.remaining() < 2)
                    return CoderResult.OVERFLOW;
                dst.put((byte) b1);
                dst.put((byte) b2);
            }
            mark++;
         }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 14
Source File: EUC_JP.java    From openjdk-jdk8u with GNU General Public License v2.0 4 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);

    int b1 = 0, b2 = 0;
    int inputSize = 0;
    char outputChar = UNMAPPABLE_DECODING;
    try {
        while (sp < sl) {
            b1 = sa[sp] & 0xff;
            inputSize = 1;

            if ((b1 & 0x80) == 0) {
                outputChar = (char)b1;
            } else {                        // Multibyte char
                if (b1 == 0x8f) {           // JIS0212
                    if (sp + 3 > sl)
                       return CoderResult.UNDERFLOW;
                    b1 = sa[sp + 1] & 0xff;
                    b2 = sa[sp + 2] & 0xff;
                    inputSize += 2;
                    if (dec0212 == null)    // JIS02012 not supported
                        return CoderResult.unmappableForLength(inputSize);
                    outputChar = dec0212.decodeDouble(b1-0x80, b2-0x80);
                } else {                     // JIS0201, JIS0208
                    if (sp + 2 > sl)
                       return CoderResult.UNDERFLOW;
                    b2 = sa[sp + 1] & 0xff;
                    inputSize++;
                    outputChar = decodeDouble(b1, b2);
                }
            }
            if (outputChar == UNMAPPABLE_DECODING) { // can't be decoded
                return CoderResult.unmappableForLength(inputSize);
            }
            if (dp + 1 > dl)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 15
Source File: DoubleByte.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
protected CoderResult encodeArrayLoop(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];
            int bb = encodeChar(c);
            if (bb == UNMAPPABLE_ENCODING) {
                if (Character.isSurrogate(c)) {
                    if (sgp().parse(c, sa, sp, sl) < 0)
                        return sgp.error();
                    return sgp.unmappableResult();
                }
                return CoderResult.unmappableForLength(1);
            }
            if (bb > MAX_SINGLEBYTE) {  // DoubleByte
                if (currentState == SBCS) {
                    if (dl - dp < 1)
                        return CoderResult.OVERFLOW;
                    currentState = DBCS;
                    da[dp++] = SO;
                }
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte)(bb >> 8);
                da[dp++] = (byte)bb;
            } else {                    // SingleByte
                if (currentState == DBCS) {
                    if (dl - dp < 1)
                        return CoderResult.OVERFLOW;
                    currentState = SBCS;
                    da[dp++] = SI;
                }
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte)bb;

            }
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 16
Source File: CESU_8.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
private static CoderResult overflow(CharBuffer src, int mark) {
    src.position(mark);
    return CoderResult.OVERFLOW;
}
 
Example 17
Source File: DoubleByte.java    From openjdk-8 with GNU General Public License v2.0 4 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 {
        // don't check dp/dl together here, it's possible to
        // decdoe a SO/SI without space in output buffer.
        while (sp < sl) {
            int b1 = sa[sp] & 0xff;
            int inSize = 1;
            if (b1 == SO) {  // Shift out
                if (currentState != SBCS)
                    return CoderResult.malformedForLength(1);
                else
                    currentState = DBCS;
            } else if (b1 == SI) {
                if (currentState != DBCS)
                    return CoderResult.malformedForLength(1);
                else
                    currentState = SBCS;
            } else {
                char c =  UNMAPPABLE_DECODING;
                if (currentState == SBCS) {
                    c = b2cSB[b1];
                    if (c == UNMAPPABLE_DECODING)
                        return CoderResult.unmappableForLength(1);
                } else {
                    if (sl - sp < 2)
                        return CoderResult.UNDERFLOW;
                    int b2 = sa[sp + 1] & 0xff;
                    if (b2 < b2Min || b2 > b2Max ||
                        (c = b2c[b1][b2 - b2Min]) == UNMAPPABLE_DECODING) {
                        if (!isDoubleByte(b1, b2))
                            return CoderResult.malformedForLength(2);
                        return CoderResult.unmappableForLength(2);
                    }
                    inSize++;
                }
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;

                da[dp++] = c;
            }
            sp += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 18
Source File: DBCS_IBM_ASCII_Encoder.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeArrayLoop(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();
    int outputSize = 0;             // size of output

    try {
        while (sp < sl) {
            int index;
            int theBytes;
            char c = sa[sp];
            if (Surrogate.is(c)) {
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);

            index = index1[((c & mask1) >> shift)]
                            + (c & mask2);
            if (index < 15000)
                theBytes = (int)(index2.charAt(index));
            else
                theBytes = (int)(index2a.charAt(index-15000));
            b1 = (byte)((theBytes & 0x0000ff00)>>8);
            b2 = (byte)(theBytes & 0x000000ff);

            if (b1 == 0x00 && b2 == 0x00
                && c != '\u0000') {
                    return CoderResult.unmappableForLength(1);
            }

            if (b1 == 0) {
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) b2;
            } else {
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) b1;
                da[dp++] = (byte) b2;
            }
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 19
Source File: EUC_JP_OLD.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
private CoderResult encodeArrayLoop(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);

    int outputSize = 0;
    byte[]  outputByte;
    int     inputSize = 0;                 // Size of input
    byte[]  tmpBuf = new byte[3];

    try {
        while (sp < sl) {
            outputByte = tmpBuf;
            char c = sa[sp];

            if (Character.isSurrogate(c)) {
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }

            outputSize = encodeSingle(c, outputByte);

            if (outputSize == 0) { // DoubleByte
                int ncode = encodeDouble(c);
                if (ncode != 0 ) {
                    if ((ncode & 0xFF0000) == 0) {
                        outputByte[0] = (byte) ((ncode & 0xff00) >> 8);
                        outputByte[1] = (byte) (ncode & 0xff);
                        outputSize = 2;
                    } else {
                        outputByte[0] = (byte) 0x8f;
                        outputByte[1] = (byte) ((ncode & 0xff00) >> 8);
                        outputByte[2] = (byte) (ncode & 0xff);
                        outputSize = 3;
                    }
                } else {
                        return CoderResult.unmappableForLength(1);
                }
            }
            if (dl - dp < outputSize)
                return CoderResult.OVERFLOW;
            // Put the byte in the output buffer
            for (int i = 0; i < outputSize; i++) {
                da[dp++] = outputByte[i];
            }
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 20
Source File: EUC_JP_OLD.java    From jdk8u_jdk with GNU General Public License v2.0 4 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);

    int b1 = 0, b2 = 0;
    int inputSize = 0;
    char outputChar = REPLACE_CHAR; // U+FFFD;

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

            if ((b1 & 0x80) == 0) {
                outputChar = (char)b1;
            }
            else {      // Multibyte char
                if ((b1 & 0xff) == 0x8f) {   // JIS0212
                    if (sp + 3 > sl)
                       return CoderResult.UNDERFLOW;
                    b1 = sa[sp + 1] & 0xff;
                    b2 = sa[sp + 2] & 0xff;
                    inputSize += 2;
                    outputChar = decode0212(b1-0x80, b2-0x80);
                } else {
                  // JIS0208
                    if (sp + 2 > sl)
                       return CoderResult.UNDERFLOW;
                    b2 = sa[sp + 1] & 0xff;
                    inputSize++;
                    outputChar = decodeDouble(b1, b2);
                }
            }
            if (outputChar == REPLACE_CHAR) { // can't be decoded
                return CoderResult.unmappableForLength(inputSize);
            }
            if (dp + 1 > dl)
                return CoderResult.OVERFLOW;
            da[dp++] = outputChar;
            sp += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}