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

The following examples show how to use java.nio.ByteBuffer#arrayOffset() . 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: Base64.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Decodes all bytes from the input byte buffer using the {@link Base64}
 * encoding scheme, writing the results into a newly-allocated ByteBuffer.
 *
 * <p> Upon return, the source buffer's position will be updated to
 * its limit; its limit will not have been changed. The returned
 * output buffer's position will be zero and its limit will be the
 * number of resulting decoded bytes
 *
 * <p> {@code IllegalArgumentException} is thrown if the input buffer
 * is not in valid Base64 encoding scheme. The position of the input
 * buffer will not be advanced in this case.
 *
 * @param   buffer
 *          the ByteBuffer to decode
 *
 * @return  A newly-allocated byte buffer containing the decoded bytes
 *
 * @throws  IllegalArgumentException
 *          if {@code src} is not in valid Base64 scheme.
 */
public ByteBuffer decode(ByteBuffer buffer) {
    int pos0 = buffer.position();
    try {
        byte[] src;
        int sp, sl;
        if (buffer.hasArray()) {
            src = buffer.array();
            sp = buffer.arrayOffset() + buffer.position();
            sl = buffer.arrayOffset() + buffer.limit();
            buffer.position(buffer.limit());
        } else {
            src = new byte[buffer.remaining()];
            buffer.get(src);
            sp = 0;
            sl = src.length;
        }
        byte[] dst = new byte[outLength(src, sp, sl)];
        return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
    } catch (IllegalArgumentException iae) {
        buffer.position(pos0);
        throw iae;
    }
}
 
Example 2
Source File: MessageDigestSpi.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Update the digest using the specified ByteBuffer. The digest is
 * updated using the {@code input.remaining()} bytes starting
 * at {@code input.position()}.
 * Upon return, the buffer's position will be equal to its limit;
 * its limit will not have changed.
 *
 * @param input the ByteBuffer
 * @since 1.5
 */
protected void engineUpdate(ByteBuffer input) {
    if (input.hasRemaining() == false) {
        return;
    }
    if (input.hasArray()) {
        byte[] b = input.array();
        int ofs = input.arrayOffset();
        int pos = input.position();
        int lim = input.limit();
        engineUpdate(b, ofs + pos, lim - pos);
        input.position(lim);
    } else {
        int len = input.remaining();
        int n = JCAUtil.getTempArraySize(len);
        if ((tempArray == null) || (n > tempArray.length)) {
            tempArray = new byte[n];
        }
        while (len > 0) {
            int chunk = Math.min(len, tempArray.length);
            input.get(tempArray, 0, chunk);
            engineUpdate(tempArray, 0, chunk);
            len -= chunk;
        }
    }
}
 
Example 3
Source File: FastByteOperations.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public void copy(ByteBuffer srcBuf, int srcPosition, ByteBuffer trgBuf, int trgPosition, int length)
{
    Object src;
    long srcOffset;
    if (srcBuf.hasArray())
    {
        src = srcBuf.array();
        srcOffset = BYTE_ARRAY_BASE_OFFSET + srcBuf.arrayOffset();
    }
    else
    {
        src = null;
        srcOffset = theUnsafe.getLong(srcBuf, DIRECT_BUFFER_ADDRESS_OFFSET);
    }
    copy(src, srcOffset + srcPosition, trgBuf, trgPosition, length);
}
 
Example 4
Source File: ByteUtil.java    From activemq-artemis with Apache License 2.0 6 votes vote down vote up
private static void uncheckedZeros(final ByteBuffer buffer, int offset, int bytes) {
   if (buffer.isReadOnly()) {
      throw new ReadOnlyBufferException();
   }
   final byte zero = (byte) 0;
   if (buffer.isDirect() && PlatformDependent.hasUnsafe()) {
      PlatformDependent.setMemory(PlatformDependent.directBufferAddress(buffer) + offset, bytes, zero);
   } else if (buffer.hasArray()) {
      //SIMD OPTIMIZATION
      final int arrayOffset = buffer.arrayOffset();
      final int start = arrayOffset + offset;
      Arrays.fill(buffer.array(), start, start + bytes, zero);
   } else {
      //slow path
      for (int i = 0; i < bytes; i++) {
         buffer.put(i + offset, zero);
      }
   }
}
 
Example 5
Source File: Base64.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes all bytes from the input byte buffer using the {@link Base64}
 * encoding scheme, writing the results into a newly-allocated ByteBuffer.
 *
 * <p> Upon return, the source buffer's position will be updated to
 * its limit; its limit will not have been changed. The returned
 * output buffer's position will be zero and its limit will be the
 * number of resulting decoded bytes
 *
 * <p> {@code IllegalArgumentException} is thrown if the input buffer
 * is not in valid Base64 encoding scheme. The position of the input
 * buffer will not be advanced in this case.
 *
 * @param   buffer
 *          the ByteBuffer to decode
 *
 * @return  A newly-allocated byte buffer containing the decoded bytes
 *
 * @throws  IllegalArgumentException
 *          if {@code buffer} is not in valid Base64 scheme
 */
public ByteBuffer decode(ByteBuffer buffer) {
    int pos0 = buffer.position();
    try {
        byte[] src;
        int sp, sl;
        if (buffer.hasArray()) {
            src = buffer.array();
            sp = buffer.arrayOffset() + buffer.position();
            sl = buffer.arrayOffset() + buffer.limit();
            buffer.position(buffer.limit());
        } else {
            src = new byte[buffer.remaining()];
            buffer.get(src);
            sp = 0;
            sl = src.length;
        }
        byte[] dst = new byte[decodedOutLength(src, sp, sl)];
        return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
    } catch (IllegalArgumentException iae) {
        buffer.position(pos0);
        throw iae;
    }
}
 
Example 6
Source File: Base64.java    From mpush-client-java with Apache License 2.0 6 votes vote down vote up
/**
 * Decodes all bytes from the input byte buffer using the {@link Base64}
 * encoding scheme, writing the results into a newly-allocated ByteBuffer.
 * <p> Upon return, the source buffer's position will be updated to
 * its limit; its limit will not have been changed. The returned
 * output buffer's position will be zero and its limit will be the
 * number of resulting decoded bytes
 * <p> {@code IllegalArgumentException} is thrown if the input buffer
 * is not in valid Base64 encoding scheme. The position of the input
 * buffer will not be advanced in this case.
 *
 * @param buffer the ByteBuffer to decode
 * @return A newly-allocated byte buffer containing the decoded bytes
 * @throws IllegalArgumentException if {@code src} is not in valid Base64 scheme.
 */
public ByteBuffer decode(ByteBuffer buffer) {
    int pos0 = buffer.position();
    try {
        byte[] src;
        int sp, sl;
        if (buffer.hasArray()) {
            src = buffer.array();
            sp = buffer.arrayOffset() + buffer.position();
            sl = buffer.arrayOffset() + buffer.limit();
            buffer.position(buffer.limit());
        } else {
            src = new byte[buffer.remaining()];
            buffer.get(src);
            sp = 0;
            sl = src.length;
        }
        byte[] dst = new byte[outLength(src, sp, sl)];
        return ByteBuffer.wrap(dst, 0, decode0(src, sp, sl, dst));
    } catch (IllegalArgumentException iae) {
        buffer.position(pos0);
        throw iae;
    }
}
 
Example 7
Source File: WingDings.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
@Override
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++;
            da[dp++] = table[c - 0x2700];
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 8
Source File: SingleByteDecoder.java    From openjdk-jdk9 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) {
            int b = sa[sp];

            char c = decode(b);
            if (c == '\uFFFD')
                return CoderResult.unmappableForLength(1);
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = c;
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 9
Source File: SingleByteDecoder.java    From shortyz with GNU General Public License v3.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();
    sp = (sp <= sl ? sp : sl);
    char[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            int b = sa[sp];

            char c = decode(b);
            if (c == '\uFFFD')
                return CoderResult.unmappableForLength(1);
            if (dl - dp < 1)
                return CoderResult.OVERFLOW;
            da[dp++] = c;
            sp++;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 10
Source File: ZipUtils.java    From Xpatch with Apache License 2.0 5 votes vote down vote up
public static DeflateResult deflate(ByteBuffer input) {
    byte[] inputBuf;
    int inputOffset;
    int inputLength = input.remaining();
    if (input.hasArray()) {
        inputBuf = input.array();
        inputOffset = input.arrayOffset() + input.position();
        input.position(input.limit());
    } else {
        inputBuf = new byte[inputLength];
        inputOffset = 0;
        input.get(inputBuf);
    }
    CRC32 crc32 = new CRC32();
    crc32.update(inputBuf, inputOffset, inputLength);
    long crc32Value = crc32.getValue();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Deflater deflater = new Deflater(9, true);
    deflater.setInput(inputBuf, inputOffset, inputLength);
    deflater.finish();
    byte[] buf = new byte[65536];
    while (!deflater.finished()) {
        int chunkSize = deflater.deflate(buf);
        out.write(buf, 0, chunkSize);
    }
    return new DeflateResult(inputLength, crc32Value, out.toByteArray());
}
 
Example 11
Source File: X11JIS0201.java    From hottub 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();
    CoderResult cr = CoderResult.UNDERFLOW;
    if ((dl - dp) < (sl - sp)) {
        sl = sp + (dl - dp);
        cr = CoderResult.OVERFLOW;
    }
    try {
        while (sp < sl) {
            char c = sa[sp];
            int b = enc.encode(c);
            if (b == UNMAPPABLE_ENCODING) {
                if (Character.isSurrogate(c)) {
                    if (sgp == null)
                        sgp = new Surrogate.Parser();
                    if (sgp.parse(c, sa, sp, sl) >= 0)
                        return CoderResult.unmappableForLength(2);
                }
                return CoderResult.unmappableForLength(1);
            }
            da[dp++] = (byte)b;
            sp++;
        }
        return cr;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 12
Source File: X11GB2312.java    From jdk8u-jdk 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 <= '\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 13
Source File: UDPTransport.java    From mpegts-streamer with Apache License 2.0 5 votes vote down vote up
@Override
public void send(MTSPacket packet) throws IOException {
	ByteBuffer buffer = packet.getBuffer();
	Preconditions.checkArgument(buffer.hasArray());
	DatagramPacket datagramPacket = new DatagramPacket(buffer.array(), buffer.arrayOffset(), buffer.limit(), inetSocketAddress);
	multicastSocket.send(datagramPacket);
}
 
Example 14
Source File: SingleByteEncoder.java    From TencentKona-8 with GNU General Public License v2.0 5 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();
    sp = (sp <= sl ? sp : sl);
    byte[] da = dst.array();
    int dp = dst.arrayOffset() + dst.position();
    int dl = dst.arrayOffset() + dst.limit();
    dp = (dp <= dl ? dp : dl);

    try {
        while (sp < sl) {
            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);
            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 15
Source File: UTF_8.java    From jdk8u-jdk 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 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
Source File: ISCII91.java    From hottub 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;

    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
Source File: DBCS_IBM_EBCDIC_Decoder.java    From openjdk-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 (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 18
Source File: SJIS_0213.java    From openjdk-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();

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

    try {
        while (sp < sl) {
            int b1 = sa[sp] & 0xff;
            char c = decodeSingle(b1);
            int inSize = 1, outSize = 1;
            char[] cc = null;
            if (c == UNMAPPABLE) {
                if (sl - sp < 2)
                    return CoderResult.UNDERFLOW;
                int b2 = sa[sp + 1] & 0xff;
                c = decodeDouble(b1, b2);
                inSize++;
                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 (dl - dp < outSize)
                return CoderResult.OVERFLOW;
            if (outSize == 2) {
                da[dp++] = cc[0];
                da[dp++] = cc[1];
            } else {
                da[dp++] = c;
            }
            sp += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}
 
Example 19
Source File: DBCS_IBM_ASCII_Encoder.java    From openjdk-jdk8u-backup 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 20
Source File: DoubleByteEncoder.java    From jdk8u_jdk 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();

    try {
        while (sp < sl) {
            char c = sa[sp];
            if (Character.isSurrogate(c)) {
                if (sgp.parse(c, sa, sp, sl) < 0)
                    return sgp.error();
                if (sl - sp < 2)
                    return CoderResult.UNDERFLOW;
                char c2 = sa[sp + 1];

                byte[] outputBytes = new byte[2];
                outputBytes = encodeSurrogate(c, c2);

                if (outputBytes == null) {
                    return sgp.unmappableResult();
                }
                else {
                    if (dl - dp < 2)
                        return CoderResult.OVERFLOW;
                    da[dp++] = outputBytes[0];
                    da[dp++] = outputBytes[1];
                    sp += 2;
                    continue;
                }
            }
            if (c >= '\uFFFE')
                return CoderResult.unmappableForLength(1);

            int b = encodeSingle(c);
            if (b != -1) { // Single Byte
                if (dl - dp < 1)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte)b;
                sp++;
                continue;
            }

            int ncode  = encodeDouble(c);
            if (ncode != 0 && c != '\u0000' ) {
                if (dl - dp < 2)
                    return CoderResult.OVERFLOW;
                da[dp++] = (byte) ((ncode & 0xff00) >> 8);
                da[dp++] = (byte) (ncode & 0xff);
                sp++;
                continue;
            }
            return CoderResult.unmappableForLength(1);
            }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
    }
}