示例#1
0
文件: Helper.java 项目: jruyi/jruyi
 public static int prepend(IByteSequence src, int offset, int length, IUnit unit) {
   int start = unit.start();
   if (length > start) {
     offset += length - start;
     length = start;
   }
   start -= length;
   unit.set(start, src, offset, offset + length);
   unit.start(start);
   unit.size(unit.size() + length);
   return length;
 }
示例#2
0
 @Override
 public char[] get(IUnitChain unitChain, int index) {
   final ByteBufferArray bba = ByteBufferArray.get();
   try {
     IUnit unit = unitChain.currentUnit();
     bba.add(unit.getByteBufferForRead(index, unit.size() - index));
     while ((unit = unitChain.nextUnit()) != null)
       bba.add(unit.getByteBufferForRead(0, unit.size()));
     final ICharsetCodec cc = CharsetCodec.get(m_charsetName);
     return cc.decode(bba.array(), 0, bba.size());
   } finally {
     bba.clear();
   }
 }
示例#3
0
文件: Helper.java 项目: jruyi/jruyi
  public static void write(ICharsetCodec cc, CharBuffer cb, IUnitChain unitChain) {
    final CharsetEncoder encoder = cc.getEncoder();
    try {
      IUnit unit = Util.lastUnit(unitChain);
      ByteBuffer bb = unit.getByteBufferForWrite();

      boolean flush = false;
      encoder.reset();
      for (; ; ) {
        CoderResult cr = flush ? encoder.flush(bb) : encoder.encode(cb, bb, true);
        unit.size(bb.position() - unit.start());
        if (cr.isOverflow()) {
          unit = Util.appendNewUnit(unitChain);
          bb = unit.getByteBufferForWrite();
          continue;
        }

        if (!cr.isUnderflow()) cr.throwException();

        if (flush) break;
        else flush = true;
      }
    } catch (CharacterCodingException e) {
      throw new RuntimeException(e);
    } finally {
      cc.releaseEncoder(encoder);
    }
  }
示例#4
0
 @Override
 public char[] read(IUnitChain unitChain) {
   final ByteBufferArray bba = ByteBufferArray.get();
   try {
     IUnit unit = unitChain.currentUnit();
     bba.add(unit.getByteBufferForRead(unit.position(), unit.remaining()));
     unit.position(unit.size());
     while ((unit = unitChain.nextUnit()) != null) {
       bba.add(unit.getByteBufferForRead(unit.position(), unit.remaining()));
       unit.position(unit.size());
     }
     final ICharsetCodec cc = CharsetCodec.get(m_charsetName);
     return cc.decode(bba.array(), 0, bba.size());
   } finally {
     bba.clear();
   }
 }
示例#5
0
  @Override
  public char[] get(IUnitChain unitChain, int index, int length) {
    if (length < 0) throw new IndexOutOfBoundsException();
    if (length == 0) return Helper.EMPTY_CHARS;

    final ByteBufferArray bba = ByteBufferArray.get();
    try {
      IUnit unit = unitChain.currentUnit();
      bba.add(unit.getByteBufferForRead(index, length));
      length -= (unit.size() - index);
      while (length > 0) {
        unit = unitChain.nextUnit();
        if (unit == null) throw new IndexOutOfBoundsException();
        bba.add(unit.getByteBufferForRead(0, length));
        length -= unit.size();
      }
      final ICharsetCodec cc = CharsetCodec.get(m_charsetName);
      return cc.decode(bba.array(), 0, bba.size());
    } finally {
      bba.clear();
    }
  }