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

The following examples show how to use java.nio.CharBuffer#get() . 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: ISO_8859_1.java    From hottub 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();
            if (c <= '\u00FF') {
                if (!dst.hasRemaining())
                    return CoderResult.OVERFLOW;
                dst.put((byte)c);
                mark++;
                continue;
            }
            if (sgp.parse(c, src) < 0)
                return sgp.error();
            return sgp.unmappableResult();
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 2
Source File: Surrogate.java    From hottub with GNU General Public License v2.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
 *
 * @return   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 (isHigh(c)) {
        if (!in.hasRemaining()) {
            error = CoderResult.UNDERFLOW;
            return -1;
        }
        char d = in.get();
        if (isLow(d)) {
            character = toUCS4(c, d);
            isPair = true;
            error = null;
            return character;
        }
        error = CoderResult.malformedForLength(1);
        return -1;
    }
    if (isLow(c)) {
        error = CoderResult.malformedForLength(1);
        return -1;
    }
    character = c;
    isPair = false;
    error = null;
    return character;
}
 
Example 3
Source File: MappeableRunContainer.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
private static int branchyBufferedUnsignedInterleavedBinarySearch(final CharBuffer sb,
    final int begin, final int end, final char k) {
  int low = begin;
  int high = end - 1;
  while (low <= high) {
    final int middleIndex = (low + high) >>> 1;
    final int middleValue = (sb.get(2 * middleIndex));
    if (middleValue < (int) (k)) {
      low = middleIndex + 1;
    } else if (middleValue > (int) (k)) {
      high = middleIndex - 1;
    } else {
      return middleIndex;
    }
  }
  return -(low + 1);
}
 
Example 4
Source File: ISO_8859_1.java    From TencentKona-8 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();
            if (c <= '\u00FF') {
                if (!dst.hasRemaining())
                    return CoderResult.OVERFLOW;
                dst.put((byte)c);
                mark++;
                continue;
            }
            if (sgp.parse(c, src) < 0)
                return sgp.error();
            return sgp.unmappableResult();
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 5
Source File: CMap.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat2(ByteBuffer buffer, int offset, char[] xlat) {

            this.xlat = xlat;

            int tableLen = buffer.getChar(offset+2);
            buffer.position(offset+6);
            CharBuffer cBuffer = buffer.asCharBuffer();
            char maxSubHeader = 0;
            for (int i=0;i<256;i++) {
                subHeaderKey[i] = cBuffer.get();
                if (subHeaderKey[i] > maxSubHeader) {
                    maxSubHeader = subHeaderKey[i];
                }
            }
            /* The value of the subHeaderKey is 8 * the subHeader index,
             * so the number of subHeaders can be obtained by dividing
             * this value bv 8 and adding 1.
             */
            int numSubHeaders = (maxSubHeader >> 3) +1;
            firstCodeArray = new char[numSubHeaders];
            entryCountArray = new char[numSubHeaders];
            idDeltaArray  = new short[numSubHeaders];
            idRangeOffSetArray  = new char[numSubHeaders];
            for (int i=0; i<numSubHeaders; i++) {
                firstCodeArray[i] = cBuffer.get();
                entryCountArray[i] = cBuffer.get();
                idDeltaArray[i] = (short)cBuffer.get();
                idRangeOffSetArray[i] = cBuffer.get();
//              System.out.println("sh["+i+"]:fc="+(int)firstCodeArray[i]+
//                                 " ec="+(int)entryCountArray[i]+
//                                 " delta="+(int)idDeltaArray[i]+
//                                 " offset="+(int)idRangeOffSetArray[i]);
            }

            int glyphIndexArrSize = (tableLen-518-numSubHeaders*8)/2;
            glyphIndexArray = new char[glyphIndexArrSize];
            for (int i=0; i<glyphIndexArrSize;i++) {
                glyphIndexArray[i] = cBuffer.get();
            }
        }
 
Example 6
Source File: ISO2022.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
private char decode(byte byte1, byte byte2, byte shiftFlag)
{
    byte1 |= MSB;
    byte2 |= MSB;

    byte[] tmpByte = { byte1,byte2 };
    char[] tmpChar = new char[1];
    int     i = 0,
            tmpIndex = 0;

    switch(shiftFlag) {
    case SOFlag:
        tmpIndex = curSODes;
        tmpDecoder = SODecoder;
        break;
    case SS2Flag:
        tmpIndex = curSS2Des;
        tmpDecoder = SS2Decoder;
        break;
    case SS3Flag:
        tmpIndex = curSS3Des;
        tmpDecoder = SS3Decoder;
        break;
    }

    if (tmpDecoder != null) {
        for(i = 0; i < tmpDecoder.length; i++) {
            if(tmpIndex == i) {
                try {
                    ByteBuffer bb = ByteBuffer.wrap(tmpByte,0,2);
                    CharBuffer cc = CharBuffer.wrap(tmpChar,0,1);
                    tmpDecoder[i].decode(bb, cc, true);
                    cc.flip();
                    return cc.get();
                } catch (Exception e) {}
            }
        }
    }
    return REPLACE_CHAR;
}
 
Example 7
Source File: SingleByteEncoder.java    From hottub 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 8
Source File: Sniffer.java    From ttt with BSD 2-Clause "Simplified" License 5 votes vote down vote up
private static boolean match(CharBuffer cb, String matchString) {
    int restore = cb.position();
    int i = 0;
    int n = matchString.length();
    for (; i < n; ++i) {
        if (cb.get() != matchString.charAt(i))
            break;
    }
    if (i == n)
        return true;
    else {
        cb.position(restore);
        return false;
    }
}
 
Example 9
Source File: JISAutoDetect.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
private static boolean looksLikeJapanese(CharBuffer cb) {
    int hiragana = 0;       // Fullwidth Hiragana
    int katakana = 0;       // Halfwidth Katakana
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (0x3040 <= c && c <= 0x309f && ++hiragana > 1) return true;
        if (0xff65 <= c && c <= 0xff9f && ++katakana > 1) return true;
    }
    return false;
}
 
Example 10
Source File: UTF_32Coder.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    int mark = src.position();
    if (!doneBOM && src.hasRemaining()) {
        if (dst.remaining() < 4)
            return CoderResult.OVERFLOW;
        put(BOM_BIG, dst);
        doneBOM = true;
    }
    try {
        while (src.hasRemaining()) {
            char c = src.get();
            if (!Character.isSurrogate(c)) {
                if (dst.remaining() < 4)
                    return CoderResult.OVERFLOW;
                mark++;
                put(c, dst);
            } else if (Character.isHighSurrogate(c)) {
                if (!src.hasRemaining())
                    return CoderResult.UNDERFLOW;
                char low = src.get();
                if (Character.isLowSurrogate(low)) {
                    if (dst.remaining() < 4)
                        return CoderResult.OVERFLOW;
                    mark += 2;
                    put(Character.toCodePoint(c, low), dst);
                } else {
                    return CoderResult.malformedForLength(1);
                }
            } else {
                // assert Character.isLowSurrogate(c);
                return CoderResult.malformedForLength(1);
            }
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 11
Source File: CMap.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat2(ByteBuffer buffer, int offset, char[] xlat) {

            this.xlat = xlat;

            int tableLen = buffer.getChar(offset+2);
            buffer.position(offset+6);
            CharBuffer cBuffer = buffer.asCharBuffer();
            char maxSubHeader = 0;
            for (int i=0;i<256;i++) {
                subHeaderKey[i] = cBuffer.get();
                if (subHeaderKey[i] > maxSubHeader) {
                    maxSubHeader = subHeaderKey[i];
                }
            }
            /* The value of the subHeaderKey is 8 * the subHeader index,
             * so the number of subHeaders can be obtained by dividing
             * this value bv 8 and adding 1.
             */
            int numSubHeaders = (maxSubHeader >> 3) +1;
            firstCodeArray = new char[numSubHeaders];
            entryCountArray = new char[numSubHeaders];
            idDeltaArray  = new short[numSubHeaders];
            idRangeOffSetArray  = new char[numSubHeaders];
            for (int i=0; i<numSubHeaders; i++) {
                firstCodeArray[i] = cBuffer.get();
                entryCountArray[i] = cBuffer.get();
                idDeltaArray[i] = (short)cBuffer.get();
                idRangeOffSetArray[i] = cBuffer.get();
//              System.out.println("sh["+i+"]:fc="+(int)firstCodeArray[i]+
//                                 " ec="+(int)entryCountArray[i]+
//                                 " delta="+(int)idDeltaArray[i]+
//                                 " offset="+(int)idRangeOffSetArray[i]);
            }

            int glyphIndexArrSize = (tableLen-518-numSubHeaders*8)/2;
            glyphIndexArray = new char[glyphIndexArrSize];
            for (int i=0; i<glyphIndexArrSize;i++) {
                glyphIndexArray[i] = cBuffer.get();
            }
        }
 
Example 12
Source File: UTF_32Coder.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
protected CoderResult encodeLoop(CharBuffer src, ByteBuffer dst) {
    int mark = src.position();
    if (!doneBOM && src.hasRemaining()) {
        if (dst.remaining() < 4)
            return CoderResult.OVERFLOW;
        put(BOM_BIG, dst);
        doneBOM = true;
    }
    try {
        while (src.hasRemaining()) {
            char c = src.get();
            if (!Character.isSurrogate(c)) {
                if (dst.remaining() < 4)
                    return CoderResult.OVERFLOW;
                mark++;
                put(c, dst);
            } else if (Character.isHighSurrogate(c)) {
                if (!src.hasRemaining())
                    return CoderResult.UNDERFLOW;
                char low = src.get();
                if (Character.isLowSurrogate(low)) {
                    if (dst.remaining() < 4)
                        return CoderResult.OVERFLOW;
                    mark += 2;
                    put(Character.toCodePoint(c, low), dst);
                } else {
                    return CoderResult.malformedForLength(1);
                }
            } else {
                // assert Character.isLowSurrogate(c);
                return CoderResult.malformedForLength(1);
            }
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 13
Source File: CMap.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat2(ByteBuffer buffer, int offset, char[] xlat) {

            this.xlat = xlat;

            int tableLen = buffer.getChar(offset+2);
            buffer.position(offset+6);
            CharBuffer cBuffer = buffer.asCharBuffer();
            char maxSubHeader = 0;
            for (int i=0;i<256;i++) {
                subHeaderKey[i] = cBuffer.get();
                if (subHeaderKey[i] > maxSubHeader) {
                    maxSubHeader = subHeaderKey[i];
                }
            }
            /* The value of the subHeaderKey is 8 * the subHeader index,
             * so the number of subHeaders can be obtained by dividing
             * this value bv 8 and adding 1.
             */
            int numSubHeaders = (maxSubHeader >> 3) +1;
            firstCodeArray = new char[numSubHeaders];
            entryCountArray = new char[numSubHeaders];
            idDeltaArray  = new short[numSubHeaders];
            idRangeOffSetArray  = new char[numSubHeaders];
            for (int i=0; i<numSubHeaders; i++) {
                firstCodeArray[i] = cBuffer.get();
                entryCountArray[i] = cBuffer.get();
                idDeltaArray[i] = (short)cBuffer.get();
                idRangeOffSetArray[i] = cBuffer.get();
//              System.out.println("sh["+i+"]:fc="+(int)firstCodeArray[i]+
//                                 " ec="+(int)entryCountArray[i]+
//                                 " delta="+(int)idDeltaArray[i]+
//                                 " offset="+(int)idRangeOffSetArray[i]);
            }

            int glyphIndexArrSize = (tableLen-518-numSubHeaders*8)/2;
            glyphIndexArray = new char[glyphIndexArrSize];
            for (int i=0; i<glyphIndexArrSize;i++) {
                glyphIndexArray[i] = cBuffer.get();
            }
        }
 
Example 14
Source File: JISAutoDetect.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
private static boolean looksLikeJapanese(CharBuffer cb) {
    int hiragana = 0;       // Fullwidth Hiragana
    int katakana = 0;       // Halfwidth Katakana
    while (cb.hasRemaining()) {
        char c = cb.get();
        if (0x3040 <= c && c <= 0x309f && ++hiragana > 1) return true;
        if (0xff65 <= c && c <= 0xff9f && ++katakana > 1) return true;
    }
    return false;
}
 
Example 15
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 16
Source File: EUC_TW.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 outSize;
    int inSize;
    int mark = src.position();

    try {
        while (src.hasRemaining()) {
            inSize = 1;
            char c = src.get();
            if (c < 0x80) {   // ASCII
                outSize = 1;
                bb[0] = (byte)c;
            } else {
                outSize = toEUC(c, bb);
                if (outSize == -1) {
                    if (Character.isHighSurrogate(c)) {
                        if (!src.hasRemaining())
                            return CoderResult.UNDERFLOW;
                        char c2 = src.get();
                        if (!Character.isLowSurrogate(c2))
                            return CoderResult.malformedForLength(1);
                        outSize = toEUC(c, c2, bb);
                        inSize = 2;
                    } else if (Character.isLowSurrogate(c)) {
                        return CoderResult.malformedForLength(1);
                    }
                }
            }
            if (outSize == -1)
                return CoderResult.unmappableForLength(inSize);
            if (dst.remaining() < outSize)
                return CoderResult.OVERFLOW;
            for (int i = 0; i < outSize; i++)
                dst.put(bb[i]);
            mark += inSize;
        }
        return CoderResult.UNDERFLOW;
    } finally {
        src.position(mark);
    }
}
 
Example 17
Source File: CESU_8.java    From hottub 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 18
Source File: CESU_8.java    From jdk8u-dev-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 19
Source File: CMap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
CMapFormat4(ByteBuffer bbuffer, int offset, char[] xlat) {

            this.xlat = xlat;

            bbuffer.position(offset);
            CharBuffer buffer = bbuffer.asCharBuffer();
            buffer.get(); // skip, we already know format=4
            int subtableLength = buffer.get();
            /* Try to recover from some bad fonts which specify a subtable
             * length that would overflow the byte buffer holding the whole
             * cmap table. If this isn't a recoverable situation an exception
             * may be thrown which is caught higher up the call stack.
             * Whilst this may seem lenient, in practice, unless the "bad"
             * subtable we are using is the last one in the cmap table we
             * would have no way of knowing about this problem anyway.
             */
            if (offset+subtableLength > bbuffer.capacity()) {
                subtableLength = bbuffer.capacity() - offset;
            }
            buffer.get(); // skip language
            segCount = buffer.get()/2;
            int searchRange = buffer.get();
            entrySelector = buffer.get();
            rangeShift    = buffer.get()/2;
            startCount = new char[segCount];
            endCount = new char[segCount];
            idDelta = new short[segCount];
            idRangeOffset = new char[segCount];

            for (int i=0; i<segCount; i++) {
                endCount[i] = buffer.get();
            }
            buffer.get(); // 2 bytes for reserved pad
            for (int i=0; i<segCount; i++) {
                startCount[i] = buffer.get();
            }

            for (int i=0; i<segCount; i++) {
                idDelta[i] = (short)buffer.get();
            }

            for (int i=0; i<segCount; i++) {
                char ctmp = buffer.get();
                idRangeOffset[i] = (char)((ctmp>>1)&0xffff);
            }
            /* Can calculate the number of glyph IDs by subtracting
             * "pos" from the length of the cmap
             */
            int pos = (segCount*8+16)/2;
            buffer.position(pos);
            int numGlyphIds = (subtableLength/2 - pos);
            glyphIds = new char[numGlyphIds];
            for (int i=0;i<numGlyphIds;i++) {
                glyphIds[i] = buffer.get();
            }
/*
            System.err.println("segcount="+segCount);
            System.err.println("entrySelector="+entrySelector);
            System.err.println("rangeShift="+rangeShift);
            for (int j=0;j<segCount;j++) {
              System.err.println("j="+j+ " sc="+(int)(startCount[j]&0xffff)+
                                 " ec="+(int)(endCount[j]&0xffff)+
                                 " delta="+idDelta[j] +
                                 " ro="+(int)idRangeOffset[j]);
            }

            //System.err.println("numglyphs="+glyphIds.length);
            for (int i=0;i<numGlyphIds;i++) {
                  System.err.println("gid["+i+"]="+(int)glyphIds[i]);
            }
*/
        }
 
Example 20
Source File: ByteBufferTest.java    From openjdk-jdk9 with GNU General Public License v2.0 votes vote down vote up
char getChar(CharBuffer v) { ck(v.get(v.position()), (char)getShortX(pos)); char x = v.get(); pos += 2; return x; }