Java Code Examples for java.nio.ByteBuffer.arrayOffset()

The following are Jave code examples for showing how to use arrayOffset() of the java.nio.ByteBuffer class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: kafka-0.11.0.0-src-with-comment   File: Utils.java   View Source Code Vote up 7 votes
/**
 * Read data from the input stream to the given byte buffer until there are no bytes remaining in the buffer or the
 * end of the stream has been reached.
 *
 * @param inputStream Input stream to read from
 * @param destinationBuffer The buffer into which bytes are to be transferred (it must be backed by an array)
 *
 * @throws IOException If an I/O error occurs
 */
public static final void readFully(InputStream inputStream, ByteBuffer destinationBuffer) throws IOException {
    if (!destinationBuffer.hasArray())
        throw new IllegalArgumentException("destinationBuffer must be backed by an array");
    int initialOffset = destinationBuffer.arrayOffset() + destinationBuffer.position();
    byte[] array = destinationBuffer.array();
    int length = destinationBuffer.remaining();
    int totalBytesRead = 0;
    do {
        int bytesRead = inputStream.read(array, initialOffset + totalBytesRead, length - totalBytesRead);
        if (bytesRead == -1)
            break;
        totalBytesRead += bytesRead;
    } while (length > totalBytesRead);
    destinationBuffer.position(destinationBuffer.position() + totalBytesRead);
}
 
Example 2
Project: OpenJSharp   File: SingleByte.java   View Source Code Vote up 6 votes
private 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();

    CoderResult cr = CoderResult.UNDERFLOW;
    if ((dl - dp) < (sl - sp)) {
        sl = sp + (dl - dp);
        cr = CoderResult.OVERFLOW;
    }

    while (sp < sl) {
        char c = decode(sa[sp]);
        if (c == UNMAPPABLE_DECODING) {
            return withResult(CoderResult.unmappableForLength(1),
                       src, sp, dst, dp);
        }
        da[dp++] = c;
        sp++;
    }
    return withResult(cr, src, sp, dst, dp);
}
 
Example 3
Project: mycat-src-1.6.1-RELEASE   File: FastByteOperations.java   View Source Code Vote up 6 votes
public int compare(ByteBuffer buffer1, byte[] buffer2, int offset2, int length2)
{
    Object obj1;
    long offset1;
    if (buffer1.hasArray())
    {
        obj1 = buffer1.array();
        offset1 = BYTE_ARRAY_BASE_OFFSET + buffer1.arrayOffset();
    }
    else
    {
        obj1 = null;
        offset1 = theUnsafe.getLong(buffer1, DIRECT_BUFFER_ADDRESS_OFFSET);
    }
    int length1;
    {
        int position = buffer1.position();
        int limit = buffer1.limit();
        length1 = limit - position;
        offset1 += position;
    }
    return compareTo(obj1, offset1, length1, buffer2, BYTE_ARRAY_BASE_OFFSET + offset2, length2);
}
 
Example 4
Project: openjdk-jdk10   File: ISO_8859_1.java   View Source Code Vote up 5 votes
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 5
Project: flink-connectors   File: FlinkPravegaUtils.java   View Source Code Vote up 5 votes
@Override
@SneakyThrows
public T deserialize(ByteBuffer buffer) {
    byte[] array;
    if (buffer.hasArray() && buffer.arrayOffset() == 0 && buffer.position() == 0 && buffer.limit() == buffer.capacity()) {
        array = buffer.array();
    } else {
        array = new byte[buffer.remaining()];
        buffer.get(array);
    }

    return deserializationSchema.deserialize(array);
}
 
Example 6
Project: OpenJSharp   File: DoubleByte.java   View Source Code Vote up 5 votes
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 7
Project: lams   File: IdentityEncoder.java   View Source Code Vote up 5 votes
@Override
public BytesRef encode(char[] buffer, int offset, int length) {
  final ByteBuffer bb = charset.encode(CharBuffer.wrap(buffer, offset, length));
  if (bb.hasArray()) {
    return new BytesRef(bb.array(), bb.arrayOffset() + bb.position(), bb.remaining());
  } else {
    // normally it should always have an array, but who knows?
    final byte[] b = new byte[bb.remaining()];
    bb.get(b);
    return new BytesRef(b);
  }
}
 
Example 8
Project: openjdk-jdk10   File: X11GB2312.java   View Source Code Vote up 5 votes
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 <= '\u007f')
                return CoderResult.unmappableForLength(1);
            int ncode = encodeDouble(c);
            if (ncode != 0 && c != '\u0000' ) {
                da[dp++] = (byte) ((ncode  >> 8) & 0x7f);
                da[dp++] = (byte) (ncode & 0x7f);
                sp++;
                continue;
            }
            return CoderResult.unmappableForLength(1);
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 9
Project: openjdk-jdk10   File: Http2TestServerConnection.java   View Source Code Vote up 5 votes
private void writeFrame(Http2Frame frame) throws IOException {
    ByteBufferReference[] refs = new FramesEncoder().encodeFrame(frame);
    //System.err.println("TestServer: Writing frame " + frame.toString());
    int c = 0;
    for (ByteBufferReference ref : refs) {
        ByteBuffer buf = ref.get();
        byte[] ba = buf.array();
        int start = buf.arrayOffset() + buf.position();
        c += buf.remaining();
        os.write(ba, start, buf.remaining());
    }
    os.flush();
    //System.err.printf("TestServer: wrote %d bytes\n", c);
}
 
Example 10
Project: monarch   File: HeapDataOutputStream.java   View Source Code Vote up 5 votes
/**
 * gets the contents of this stream as a byte[]. The stream should not be written to past this
 * point until it has been reset.
 */
public final byte[] toByteArray() {
  ByteBuffer bb = toByteBuffer();
  if (bb.hasArray() && bb.arrayOffset() == 0 && bb.limit() == bb.capacity()) {
    return bb.array();
  } else {
    // create a new buffer of just the right size and copy the old buffer into it
    ByteBuffer tmp = ByteBuffer.allocate(bb.remaining());
    tmp.put(bb);
    tmp.flip();
    this.buffer = tmp;
    return this.buffer.array();
  }
}
 
Example 11
Project: jdk8u-jdk   File: X11Dingbats.java   View Source Code Vote up 5 votes
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 12
Project: jdk8u-jdk   File: SingleByteEncoder.java   View Source Code Vote up 4 votes
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);

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (Character.isSurrogate(c)) {
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return sgp.error();
                return sgp.unmappableResult();
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);
            if (dl - dp < 1)
                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);

            sp++;
            da[dp++] = (byte)e;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 13
Project: openjdk-jdk10   File: DBCS_IBM_ASCII_Encoder.java   View Source Code Vote up 4 votes
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 14
Project: openjdk-jdk10   File: DBCS_IBM_EBCDIC_Decoder.java   View Source Code Vote up 4 votes
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 (b1 == SO) {  // Shift out
                // For SO characters - simply validate the state and if OK
                //    update the state and go to the next byte

                if (currentState != SBCS)
                    return CoderResult.malformedForLength(1);
                else
                    currentState = DBCS;
            } else if (b1 == SI) {
                // For SI characters - simply validate the state and if OK
                //    update the state and go to the next byte

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

                inputSize++;

                // Check validity of dbcs ebcdic byte pair values
                if ((b1 != 0x40 || b2 != 0x40) &&
                  (b2 < 0x41 || b2 > 0xfe)) {
                  return CoderResult.malformedForLength(2);
                }

                // 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 15
Project: jdk8u-jdk   File: UTF_8.java   View Source Code Vote up 4 votes
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 dlASCII = dp + Math.min(sl - sp, dl - dp);

    // ASCII only loop
    while (dp < dlASCII && sa[sp] < '\u0080')
        da[dp++] = (byte) sa[sp++];
    while (sp < sl) {
        char c = sa[sp];
        if (c < 0x80) {
            // Have at most seven bits
            if (dp >= dl)
                return overflow(src, sp, dst, dp);
            da[dp++] = (byte)c;
        } else if (c < 0x800) {
            // 2 bytes, 11 bits
            if (dl - dp < 2)
                return overflow(src, sp, dst, dp);
            da[dp++] = (byte)(0xc0 | (c >> 6));
            da[dp++] = (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, sa, sp, sl);
            if (uc < 0) {
                updatePositions(src, sp, dst, dp);
                return sgp.error();
            }
            if (dl - dp < 4)
                return overflow(src, sp, dst, dp);
            da[dp++] = (byte)(0xf0 | ((uc >> 18)));
            da[dp++] = (byte)(0x80 | ((uc >> 12) & 0x3f));
            da[dp++] = (byte)(0x80 | ((uc >>  6) & 0x3f));
            da[dp++] = (byte)(0x80 | (uc & 0x3f));
            sp++;  // 2 chars
        } else {
            // 3 bytes, 16 bits
            if (dl - dp < 3)
                return overflow(src, sp, dst, dp);
            da[dp++] = (byte)(0xe0 | ((c >> 12)));
            da[dp++] = (byte)(0x80 | ((c >>  6) & 0x3f));
            da[dp++] = (byte)(0x80 | (c & 0x3f));
        }
        sp++;
    }
    updatePositions(src, sp, dst, dp);
    return CoderResult.UNDERFLOW;
}
 
Example 16
Project: jdk8u-jdk   File: ISCII91.java   View Source Code Vote up 4 votes
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;

    try {
        char inputChar;
        while (sp < sl) {
            int index = Integer.MIN_VALUE;
            inputChar = sa[sp];

            if (inputChar >= 0x0000 && inputChar <= 0x007f) {
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) inputChar;
                sp++;
                continue;
            }

            // if inputChar == ZWJ replace it with halant
            // if inputChar == ZWNJ replace it with Nukta

            if (inputChar == 0x200c) {
                inputChar = HALANT_CHAR;
            }
            else if (inputChar == 0x200d) {
                inputChar = NUKTA_CHAR;
            }

            if (inputChar >= 0x0900 && inputChar <= 0x097f) {
                index = ((int)(inputChar) - 0x0900)*2;
            }

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

            if (index == Integer.MIN_VALUE ||
                encoderMappingTable[index] == NO_CHAR) {
                return CoderResult.unmappableForLength(1);
            } else {
                if(encoderMappingTable[index + 1] == NO_CHAR) {
                    if(dl - dp < 1)
                        return CoderResult.OVERFLOW;
                    da[dp++] = encoderMappingTable[index];
                } else {
                    if(dl - dp < 2)
                        return CoderResult.OVERFLOW;
                    da[dp++] = encoderMappingTable[index];
                    da[dp++] = encoderMappingTable[index + 1];
                }
                sp++;
            }
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 17
Project: openjdk-jdk10   File: DoubleByteDecoder.java   View Source Code Vote up 4 votes
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 outputSize = 1;
            highSurrogate = lowSurrogate = 0;
            char c = decodeSingle(b1);
            if (c == REPLACE_CHAR) {
                b1 &= 0xff;
                if (sl - sp < 2)
                    return CoderResult.UNDERFLOW;
                b2 = sa[sp + 1] & 0xff;
                c = decodeDouble(b1, b2);
                inputSize = 2;
                if (c == REPLACE_CHAR)
                    return CoderResult.unmappableForLength(inputSize);
                outputSize = (highSurrogate > 0) ? 2: 1;
            }

            if (dl - dp < outputSize)
                return CoderResult.OVERFLOW;
            if (outputSize == 2) {
                da[dp++] = highSurrogate;
                da[dp++] = lowSurrogate;
            } else {
                da[dp++] = c;
            }
            sp += inputSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 18
Project: OpenJSharp   File: CESU_8.java   View Source Code Vote up 4 votes
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 dlASCII = dp + Math.min(sl - sp, dl - dp);

    // ASCII only loop
    while (dp < dlASCII && sa[sp] < '\u0080')
        da[dp++] = (byte) sa[sp++];
    while (sp < sl) {
        char c = sa[sp];
        if (c < 0x80) {
            // Have at most seven bits
            if (dp >= dl)
                return overflow(src, sp, dst, dp);
            da[dp++] = (byte)c;
        } else if (c < 0x800) {
            // 2 bytes, 11 bits
            if (dl - dp < 2)
                return overflow(src, sp, dst, dp);
            da[dp++] = (byte)(0xc0 | (c >> 6));
            da[dp++] = (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, sa, sp, sl);
            if (uc < 0) {
                updatePositions(src, sp, dst, dp);
                return sgp.error();
            }
            if (dl - dp < 6)
                return overflow(src, sp, dst, dp);
            to3Bytes(da, dp, Character.highSurrogate(uc));
            dp += 3;
            to3Bytes(da, dp, Character.lowSurrogate(uc));
            dp += 3;
            sp++;  // 2 chars
        } else {
            // 3 bytes, 16 bits
            if (dl - dp < 3)
                return overflow(src, sp, dst, dp);
            to3Bytes(da, dp, c);
            dp += 3;
        }
        sp++;
    }
    updatePositions(src, sp, dst, dp);
    return CoderResult.UNDERFLOW;
}
 
Example 19
Project: openjdk-jdk10   File: IBM964.java   View Source Code Vote up 4 votes
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 byte1, byte2;
        int inputSize = 1;
        char outputChar = '\uFFFD';
        byte1 = sa[sp] & 0xff;

        if (byte1 == SS2) {
            if (sl - sp < 4) {
                return CoderResult.UNDERFLOW;
            }
            byte1 = sa[sp + 1] & 0xff;
            inputSize = 2;
            if ( byte1 == 0xa2)
                mappingTableG2 = mappingTableG2a2;
            else if ( byte1 == 0xac)
                mappingTableG2 = mappingTableG2ac;
            else if ( byte1 == 0xad)
                mappingTableG2 = mappingTableG2ad;
            else
                return CoderResult.malformedForLength(2);
            byte1 = sa[sp + 2] & 0xff;
            if ( byte1 < 0xa1 || byte1 > 0xfe) {
                return CoderResult.malformedForLength(3);
            }
            byte2 = sa[sp + 3] & 0xff;
            if ( byte2 < 0xa1 || byte2 > 0xfe) {
              return CoderResult.malformedForLength(4);
            }
            inputSize = 4;
            outputChar = mappingTableG2.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);
        } else if(byte1 == SS3 ) {
            return CoderResult.malformedForLength(1);
        } else if ( byte1 <= 0x9f ) {                // valid single byte
            outputChar = byteToCharTable.charAt(byte1);
        } else if (byte1 < 0xa1 || byte1 > 0xfe) {   // invalid range?
            return CoderResult.malformedForLength(1);
        } else {                                     // G1
            if (sl - sp < 2) {
                return CoderResult.UNDERFLOW;
            }
            byte2 = sa[sp + 1] & 0xff;
            inputSize = 2;
            if ( byte2 < 0xa1 || byte2 > 0xfe) {
                return CoderResult.malformedForLength(2);
            }
            outputChar = mappingTableG1.charAt(((byte1 - 0xa1) * 94) + byte2 - 0xa1);
        }
        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 20
Project: jdk8u-jdk   File: DBCS_IBM_ASCII_Encoder.java   View Source Code Vote up 4 votes
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());
    }
}