public CharBuffer put(CharBuffer src) {

    if (src instanceof DirectCharBufferS) {
      if (src == this) throw new IllegalArgumentException();
      DirectCharBufferS sb = (DirectCharBufferS) src;

      int spos = sb.position();
      int slim = sb.limit();
      assert (spos <= slim);
      int srem = (spos <= slim ? slim - spos : 0);

      int pos = position();
      int lim = limit();
      assert (pos <= lim);
      int rem = (pos <= lim ? lim - pos : 0);

      if (srem > rem) throw new BufferOverflowException();
      unsafe.copyMemory(sb.ix(spos), ix(pos), srem << 1);
      sb.position(spos + srem);
      position(pos + srem);
    } else if (!src.isDirect()) {

      int spos = src.position();
      int slim = src.limit();
      assert (spos <= slim);
      int srem = (spos <= slim ? slim - spos : 0);

      put(src.hb, src.offset + spos, srem);
      src.position(spos + srem);

    } else {
      super.put(src);
    }
    return this;
  }
Example #2
0
 static byte[] encode(char[] cc, Charset cs, boolean testDirect, Time t) throws Exception {
   ByteBuffer bbf;
   CharBuffer cbf;
   CharsetEncoder enc = cs.newEncoder();
   String csn = cs.name();
   if (testDirect) {
     bbf = ByteBuffer.allocateDirect(cc.length * 4);
     cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
     cbf.put(cc).flip();
   } else {
     bbf = ByteBuffer.allocate(cc.length * 4);
     cbf = CharBuffer.wrap(cc);
   }
   CoderResult cr = null;
   long t1 = System.nanoTime() / 1000;
   for (int i = 0; i < iteration; i++) {
     cbf.rewind();
     bbf.clear();
     enc.reset();
     cr = enc.encode(cbf, bbf, true);
   }
   long t2 = System.nanoTime() / 1000;
   t.t = (t2 - t1) / iteration;
   if (cr != CoderResult.UNDERFLOW) {
     System.out.println("ENC-----------------");
     int pos = cbf.position();
     System.out.printf("  cr=%s, cbf.pos=%d, cc[pos]=%x%n", cr.toString(), pos, cc[pos] & 0xffff);
     throw new RuntimeException("Encoding err: " + csn);
   }
   byte[] bb = new byte[bbf.position()];
   bbf.flip();
   bbf.get(bb);
   return bb;
 }
 protected PlaceNameChunk createPlaceNameChunk(PlaceNameService service) {
   int numChars = this.textArray.length();
   CharBuffer textBuffer = newCharBuffer(numChars);
   textBuffer.put(this.textArray.toString());
   textBuffer.rewind();
   return new PlaceNameChunk(
       service, textBuffer, this.textIndexArray, this.latlonArray, this.numEntries);
 }
 public static void main(String[] args) throws Exception {
   // 创建简体中文对应的Charset
   Charset cn = Charset.forName("GBK");
   // 获取cn对象对应的编码器和解码器
   CharsetEncoder cnEncoder = cn.newEncoder();
   CharsetDecoder cnDecoder = cn.newDecoder();
   // 创建一个CharBuffer对象
   CharBuffer cbuff = CharBuffer.allocate(8);
   cbuff.put('孙');
   cbuff.put('悟');
   cbuff.put('空');
   cbuff.flip();
   // 将CharBuffer中的字符序列转换成字节序列
   ByteBuffer bbuff = cnEncoder.encode(cbuff);
   // 循环访问ByteBuffer中的每个字节
   for (int i = 0; i < bbuff.capacity(); i++) {
     System.out.print(bbuff.get(i) + " ");
   }
   // 将ByteBuffer的数据解码成字符序列
   System.out.println("\n" + cnDecoder.decode(bbuff));
 }
 public String toString(int start, int end) {
   if ((end > limit()) || (start > end)) throw new IndexOutOfBoundsException();
   try {
     int len = end - start;
     char[] ca = new char[len];
     CharBuffer cb = CharBuffer.wrap(ca);
     CharBuffer db = this.duplicate();
     db.position(start);
     db.limit(end);
     cb.put(db);
     return new String(ca);
   } catch (StringIndexOutOfBoundsException x) {
     throw new IndexOutOfBoundsException();
   }
 }
  public CharBuffer put(char[] src, int offset, int length) {

    if ((length << 1) > Bits.JNI_COPY_FROM_ARRAY_THRESHOLD) {
      checkBounds(offset, length, src.length);
      int pos = position();
      int lim = limit();
      assert (pos <= lim);
      int rem = (pos <= lim ? lim - pos : 0);
      if (length > rem) throw new BufferOverflowException();

      if (order() != ByteOrder.nativeOrder())
        Bits.copyFromCharArray(src, offset << 1, ix(pos), length << 1);
      else Bits.copyFromByteArray(src, offset << 1, ix(pos), length << 1);
      position(pos + length);
    } else {
      super.put(src, offset, length);
    }
    return this;
  }
Example #7
0
 static CoderResult encodeCR(char[] cc, Charset cs, boolean testDirect) throws Exception {
   ByteBuffer bbf;
   CharBuffer cbf;
   CharsetEncoder enc = cs.newEncoder();
   if (testDirect) {
     bbf = ByteBuffer.allocateDirect(cc.length * 4);
     cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
     cbf.put(cc).flip();
   } else {
     bbf = ByteBuffer.allocate(cc.length * 4);
     cbf = CharBuffer.wrap(cc);
   }
   CoderResult cr = null;
   for (int i = 0; i < iteration; i++) {
     cbf.rewind();
     bbf.clear();
     enc.reset();
     cr = enc.encode(cbf, bbf, true);
   }
   return cr;
 }