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

The following examples show how to use java.nio.ByteBuffer#asCharBuffer() . 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: ChannelHelper.java    From libcommon with Apache License 2.0 6 votes vote down vote up
/**
 * ByteChannelからchar配列を読み込む
 * @param channel
 * @return
 * @throws IOException
 */
public static char[] readCharArray(@NonNull final ByteChannel channel)
	throws IOException {
	
	final int n = readInt(channel);
	final ByteBuffer buf = ByteBuffer.allocate(n * 2).order(ByteOrder.BIG_ENDIAN);
	final int readBytes = channel.read(buf);
	if (readBytes != n * 2) throw new IOException();
	buf.clear();
	final CharBuffer result = buf.asCharBuffer();
	if (result.hasArray()) {
		return result.array();
	}  else {
		final char[] b = new char[n];
		result.get(b);
		return b;
	}
}
 
Example 2
Source File: CMap.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {

             bbuffer.position(offset+12);
             firstCode = bbuffer.getInt() & INTMASK;
             entryCount = bbuffer.getInt() & INTMASK;
             // each glyph is a uint16, so 2 bytes per value.
             if (bbuffer.remaining() < (2 * (long)entryCount)) {
                 throw new RuntimeException("Format 10 table exceeded");
             }
             CharBuffer buffer = bbuffer.asCharBuffer();
             glyphIdArray = new char[entryCount];
             for (int i=0; i< entryCount; i++) {
                 glyphIdArray[i] = buffer.get();
             }
         }
 
Example 3
Source File: TrueTypeFont.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
private char[] getGaspTable() {

        if (gaspTable != null) {
            return gaspTable;
        }

        ByteBuffer buffer = getTableBuffer(gaspTag);
        if (buffer == null) {
            return gaspTable = new char[0];
        }

        CharBuffer cbuffer = buffer.asCharBuffer();
        char format = cbuffer.get();
        /* format "1" has appeared for some Windows Vista fonts.
         * Its presently undocumented but the existing values
         * seem to be still valid so we can use it.
         */
        if (format > 1) { // unrecognised format
            return gaspTable = new char[0];
        }

        char numRanges = cbuffer.get();
        if (4+numRanges*4 > getTableSize(gaspTag)) { // sanity check
            return gaspTable = new char[0];
        }
        gaspTable = new char[2*numRanges];
        cbuffer.get(gaspTable);
        return gaspTable;
    }
 
Example 4
Source File: CMap.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {

             firstCode = bbuffer.getInt() & INTMASK;
             entryCount = bbuffer.getInt() & INTMASK;
             bbuffer.position(offset+20);
             CharBuffer buffer = bbuffer.asCharBuffer();
             glyphIdArray = new char[entryCount];
             for (int i=0; i< entryCount; i++) {
                 glyphIdArray[i] = buffer.get();
             }
         }
 
Example 5
Source File: CMap.java    From jdk8u-dev-jdk 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: CMap.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat6(ByteBuffer bbuffer, int offset, char[] xlat) {

             bbuffer.position(offset+6);
             CharBuffer buffer = bbuffer.asCharBuffer();
             firstCode = buffer.get();
             entryCount = buffer.get();
             glyphIdArray = new char[entryCount];
             for (int i=0; i< entryCount; i++) {
                 glyphIdArray[i] = buffer.get();
             }
         }
 
Example 7
Source File: BufferTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
private void testBuffersIndependentLimit(ByteBuffer b) {
    CharBuffer c = b.asCharBuffer();
    b.limit(b.capacity()); c.put(1, (char)1);
    b.limit(0);  c.put(1, (char)1);

    b.limit(b.capacity());
    ShortBuffer s = b.asShortBuffer();
    b.limit(b.capacity()); s.put(1, (short)1);
    b.limit(0);  s.put(1, (short)1);

    b.limit(b.capacity());
    IntBuffer i = b.asIntBuffer();
    i.put(1, (int)1);
    b.limit(0);  i.put(1, (int)1);

    b.limit(b.capacity());
    LongBuffer l = b.asLongBuffer();
    l.put(1, (long)1);
    b.limit(0);  l.put(1, (long)1);

    b.limit(b.capacity());
    FloatBuffer f = b.asFloatBuffer();
    f.put(1, (float)1);
    b.limit(0);  f.put(1, (float)1);

    b.limit(b.capacity());
    DoubleBuffer d = b.asDoubleBuffer();
    d.put(1, (double)1);
    b.limit(0);  d.put(1, (double)1);
}
 
Example 8
Source File: BufferTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void testCharBufferSubSequence() throws Exception {
    ByteBuffer b = ByteBuffer.allocateDirect(10).order(ByteOrder.nativeOrder());
    b.putChar('H');
    b.putChar('e');
    b.putChar('l');
    b.putChar('l');
    b.putChar('o');
    b.flip();

    assertEquals("Hello", b.asCharBuffer().toString());

    CharBuffer cb = b.asCharBuffer();
    CharSequence cs = cb.subSequence(0, cb.length());
    assertEquals("Hello", cs.toString());
}
 
Example 9
Source File: DoubleArrayTrie.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public void write(OutputStream output) throws IOException {

        baseBuffer.rewind();
        checkBuffer.rewind();
        tailBuffer.rewind();

        int baseCheckSize = Math.min(maxBaseCheckIndex + 64, baseBuffer.capacity());
        int tailSize = Math.min(tailIndex - TAIL_OFFSET + 64, tailBuffer.capacity());

        DataOutputStream dataOutput = new DataOutputStream(new BufferedOutputStream(output));

        dataOutput.writeBoolean(compact);
        dataOutput.writeInt(baseCheckSize);
        dataOutput.writeInt(tailSize);

        WritableByteChannel channel = Channels.newChannel(dataOutput);

        ByteBuffer tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
        IntBuffer tmpIntBuffer = tmpBuffer.asIntBuffer();
        tmpIntBuffer.put(baseBuffer.array(), 0, baseCheckSize);
        tmpBuffer.rewind();
        channel.write(tmpBuffer);

        tmpBuffer = ByteBuffer.allocate(baseCheckSize * 4);
        tmpIntBuffer = tmpBuffer.asIntBuffer();
        tmpIntBuffer.put(checkBuffer.array(), 0, baseCheckSize);
        tmpBuffer.rewind();
        channel.write(tmpBuffer);

        tmpBuffer = ByteBuffer.allocate(tailSize * 2);
        CharBuffer tmpCharBuffer = tmpBuffer.asCharBuffer();
        tmpCharBuffer.put(tailBuffer.array(), 0, tailSize);
        tmpBuffer.rewind();
        channel.write(tmpBuffer);

        dataOutput.flush();
    }
 
Example 10
Source File: TrueTypeFont.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
private char[] getGaspTable() {

        if (gaspTable != null) {
            return gaspTable;
        }

        ByteBuffer buffer = getTableBuffer(gaspTag);
        if (buffer == null) {
            return gaspTable = new char[0];
        }

        CharBuffer cbuffer = buffer.asCharBuffer();
        char format = cbuffer.get();
        /* format "1" has appeared for some Windows Vista fonts.
         * Its presently undocumented but the existing values
         * seem to be still valid so we can use it.
         */
        if (format > 1) { // unrecognised format
            return gaspTable = new char[0];
        }

        char numRanges = cbuffer.get();
        if (4+numRanges*4 > getTableSize(gaspTag)) { // sanity check
            return gaspTable = new char[0];
        }
        gaspTable = new char[2*numRanges];
        cbuffer.get(gaspTable);
        return gaspTable;
    }
 
Example 11
Source File: CMap.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {

             firstCode = bbuffer.getInt() & INTMASK;
             entryCount = bbuffer.getInt() & INTMASK;
             bbuffer.position(offset+20);
             CharBuffer buffer = bbuffer.asCharBuffer();
             glyphIdArray = new char[entryCount];
             for (int i=0; i< entryCount; i++) {
                 glyphIdArray[i] = buffer.get();
             }
         }
 
Example 12
Source File: CMap.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {

             bbuffer.position(offset+12);
             firstCode = bbuffer.getInt() & INTMASK;
             entryCount = bbuffer.getInt() & INTMASK;
             // each glyph is a uint16, so 2 bytes per value.
             if (bbuffer.remaining() < (2 * (long)entryCount)) {
                 throw new RuntimeException("Format 10 table exceeded");
             }
             CharBuffer buffer = bbuffer.asCharBuffer();
             glyphIdArray = new char[entryCount];
             for (int i=0; i< entryCount; i++) {
                 glyphIdArray[i] = buffer.get();
             }
         }
 
Example 13
Source File: CMap.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat10(ByteBuffer bbuffer, int offset, char[] xlat) {

             firstCode = bbuffer.getInt() & INTMASK;
             entryCount = bbuffer.getInt() & INTMASK;
             bbuffer.position(offset+20);
             CharBuffer buffer = bbuffer.asCharBuffer();
             glyphIdArray = new char[entryCount];
             for (int i=0; i< entryCount; i++) {
                 glyphIdArray[i] = buffer.get();
             }
         }
 
Example 14
Source File: UsingBuffers.java    From LearningOfThinkInJava with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    char[] data="UsingBuffers".toCharArray();
    ByteBuffer bb=ByteBuffer.allocate(data.length*2);
    CharBuffer cb=bb.asCharBuffer();
    cb.put(data);
    print(cb.rewind());

    symmetricScramble(cb);
    print(cb.rewind());

    symmetricScramble(cb);
    print(cb.rewind());
}
 
Example 15
Source File: CMap.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
CMapFormat6(ByteBuffer bbuffer, int offset, char[] xlat) {

             bbuffer.position(offset+6);
             CharBuffer buffer = bbuffer.asCharBuffer();
             firstCode = buffer.get();
             entryCount = buffer.get();
             glyphIdArray = new char[entryCount];
             for (int i=0; i< entryCount; i++) {
                 glyphIdArray[i] = buffer.get();
             }
         }
 
Example 16
Source File: TrueTypeFont.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private char[] getGaspTable() {

        if (gaspTable != null) {
            return gaspTable;
        }

        ByteBuffer buffer = getTableBuffer(gaspTag);
        if (buffer == null) {
            return gaspTable = new char[0];
        }

        CharBuffer cbuffer = buffer.asCharBuffer();
        char format = cbuffer.get();
        /* format "1" has appeared for some Windows Vista fonts.
         * Its presently undocumented but the existing values
         * seem to be still valid so we can use it.
         */
        if (format > 1) { // unrecognised format
            return gaspTable = new char[0];
        }

        char numRanges = cbuffer.get();
        if (4+numRanges*4 > getTableSize(gaspTag)) { // sanity check
            return gaspTable = new char[0];
        }
        gaspTable = new char[2*numRanges];
        cbuffer.get(gaspTable);
        return gaspTable;
    }
 
Example 17
Source File: UsingBuffers.java    From java-core-learning-example with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    char[] data = "UsingBuffers".toCharArray();
    // 一个字符两个字节
    ByteBuffer bb = ByteBuffer.allocate(data.length * 2);
    CharBuffer cb = bb.asCharBuffer();
    cb.put(data);
    // 重绕此缓冲区
    System.out.println(cb.rewind());
    symmetricScaramble(cb);
    // 重绕此缓冲区
    System.out.println(cb.rewind());
    symmetricScaramble(cb);
    System.out.println(cb.rewind());
}
 
Example 18
Source File: YosegiWriter.java    From yosegi with Apache License 2.0 5 votes vote down vote up
/**
 * Initialize by setting OutputStream.
 */
public YosegiWriter( final OutputStream out , final Configuration config ) throws IOException {
  this.out = out;

  int blockSize = config.getInt( "block.size" , 1024 * 1024 * 64 );

  blockMaker = FindBlockWriter.get(
      config.get( "block.maker.class" , PushdownSupportedBlockWriter.class.getName() ) );
  blockMaker.setup( blockSize , config );
  String blockMakerClassName = BlockReaderNameShortCut
      .getShortCutName( blockMaker.getReaderClassName() );
  int classNameLength = blockMakerClassName.length() * Character.BYTES;

  byte[] header = new byte[MAGIC.length + Integer.BYTES + Integer.BYTES + classNameLength ];
  ByteBuffer wrapBuffer = ByteBuffer.wrap( header );
  final CharBuffer viewCharBuffer = wrapBuffer.asCharBuffer();
  int offset = 0;
  wrapBuffer.put( MAGIC , 0 , MAGIC.length );
  offset += MAGIC.length;
  wrapBuffer.putInt( offset , blockSize );
  offset += Integer.BYTES;
  wrapBuffer.putInt( offset , classNameLength );
  offset += Integer.BYTES;
  viewCharBuffer.position( offset / Character.BYTES );
  viewCharBuffer.put( blockMakerClassName.toCharArray() );

  blockMaker.appendHeader( header );
}
 
Example 19
Source File: NativeUint16Array.java    From jdk8u_nashorn with GNU General Public License v2.0 4 votes vote down vote up
@Override
public Uint16ArrayData createArrayData(final ByteBuffer nb, final int start, final int end) {
    return new Uint16ArrayData(nb.asCharBuffer(), start, end);
}
 
Example 20
Source File: CMap.java    From openjdk-jdk9 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]);
            }
*/
        }