Esempio n. 1
0
  // Writes a C string and expands the frame if necessary
  // FIXME: Property strings containing nul ('\0') characters will corrupt the frame when they are
  // written.
  //        Figure out how to throw an exception here if any nul chars are encountered
  private static ByteBuffer writeCString(ByteBuffer frame, String string) {
    Byte b = propertyAbbreviations.get(string);
    if (b != null) {
      if (frame.remaining() < 2)
        frame = ByteBuffer.allocate(frame.capacity() << 1).put((ByteBuffer) frame.rewind());
      frame.put(b);
      frame.put((byte) 0);
    } else {
      CharsetEncoder cStringEncoder = cStringCharset.newEncoder();

      CharBuffer chars = CharBuffer.wrap(string);
      for (int size = frame.capacity(); ; cStringEncoder.flush(frame)) {
        cStringEncoder.reset();
        if (cStringEncoder.encode(chars, frame, true) == CoderResult.OVERFLOW) {
          // debug output
          // System.out.println("overflow, reallocating to size " + (size << 1) + " (printing \"" +
          // string + "\")");
          frame = ByteBuffer.allocate(size = (size << 1)).put((ByteBuffer) frame.rewind());
        } else break;
      }
      cStringEncoder.flush(frame);
      frame.put((byte) 0);
    }
    return frame;
  }
Esempio n. 2
0
    byte[] encode(char[] ca, int off, int len) {

      int en = (int) (ce.maxBytesPerChar() * len);

      byte[] ba = new byte[en];

      if (len == 0) return ba;

      ce.reset();

      ByteBuffer bb = ByteBuffer.wrap(ba);

      CharBuffer cb = CharBuffer.wrap(ca, off, len);

      try {

        CoderResult cr = ce.encode(cb, bb, true);

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

        cr = ce.flush(bb);

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

      } catch (CharacterCodingException x) {

        // Substitution is always enabled,

        // so this shouldn't happen

        throw new Error(x);
      }

      return trim(ba, bb.position());
    }
Esempio n. 3
0
 public ByteBuffer flush(ByteBuffer dst) throws SVNException {
   if (myCharBuffer != null) {
     CoderResult result;
     while (true) {
       result = myDecoder.flush(myCharBuffer);
       if (result.isError()) {
         throwException(result);
       }
       if (result.isUnderflow()) {
         break;
       }
     }
     myCharBuffer.flip();
     dst = allocate(dst, (int) (myEncoder.maxBytesPerChar() * myCharBuffer.remaining()));
     result = myEncoder.encode(myCharBuffer, dst, true);
     if (result.isError()) {
       throwException(result);
     }
     while (true) {
       result = myEncoder.flush(dst);
       if (result.isError()) {
         throwException(result);
       }
       if (result.isUnderflow()) {
         break;
       }
     }
   }
   reset();
   return dst;
 }
Esempio n. 4
0
  private static void writeEntry(
      OutputStream os, Attributes.Name name, String value, CharsetEncoder encoder, ByteBuffer bBuf)
      throws IOException {
    byte[] out = name.getBytes();
    if (out.length > LINE_LENGTH_LIMIT) {
      throw new IOException(
          Messages.getString(
              "archive.33", name, Integer.valueOf(LINE_LENGTH_LIMIT))); // $NON-NLS-1$
    }

    os.write(out);
    os.write(VALUE_SEPARATOR);

    encoder.reset();
    bBuf.clear().limit(LINE_LENGTH_LIMIT - out.length - 2);

    CharBuffer cBuf = CharBuffer.wrap(value);
    CoderResult r;

    while (true) {
      r = encoder.encode(cBuf, bBuf, true);
      if (CoderResult.UNDERFLOW == r) {
        r = encoder.flush(bBuf);
      }
      os.write(bBuf.array(), bBuf.arrayOffset(), bBuf.position());
      os.write(LINE_SEPARATOR);
      if (CoderResult.UNDERFLOW == r) {
        break;
      }
      os.write(' ');
      bBuf.clear().limit(LINE_LENGTH_LIMIT - 1);
    }
  }
Esempio n. 5
0
  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);
    }
  }
Esempio n. 6
0
 static byte[] encode(Charset cs, char[] ca, int off, int len) {
   CharsetEncoder ce = cs.newEncoder();
   int en = scale(len, ce.maxBytesPerChar());
   byte[] ba = new byte[en];
   if (len == 0) return ba;
   boolean isTrusted = false;
   if (System.getSecurityManager() != null) {
     if (!(isTrusted = (cs.getClass().getClassLoader0() == null))) {
       ca = Arrays.copyOfRange(ca, off, off + len);
       off = 0;
     }
   }
   ce.onMalformedInput(CodingErrorAction.REPLACE)
       .onUnmappableCharacter(CodingErrorAction.REPLACE)
       .reset();
   if (ce instanceof ArrayEncoder) {
     int blen = ((ArrayEncoder) ce).encode(ca, off, len, ba);
     return safeTrim(ba, blen, cs, isTrusted);
   } else {
     ByteBuffer bb = ByteBuffer.wrap(ba);
     CharBuffer cb = CharBuffer.wrap(ca, off, len);
     try {
       CoderResult cr = ce.encode(cb, bb, true);
       if (!cr.isUnderflow()) cr.throwException();
       cr = ce.flush(bb);
       if (!cr.isUnderflow()) cr.throwException();
     } catch (CharacterCodingException x) {
       throw new Error(x);
     }
     return safeTrim(ba, bb.position(), cs, isTrusted);
   }
 }
Esempio n. 7
0
 public static int encode(
     final CharsetEncoder ce,
     final char[] ca,
     final int off,
     final int len,
     final byte[] targetArray) {
   if (len == 0) {
     return 0;
   }
   if (ce instanceof ArrayEncoder) {
     return ((ArrayEncoder) ce).encode(ca, off, len, targetArray);
   } else {
     ce.reset();
     ByteBuffer bb = ByteBuffer.wrap(targetArray);
     CharBuffer cb = CharBuffer.wrap(ca, off, len);
     try {
       CoderResult cr = ce.encode(cb, bb, true);
       if (!cr.isUnderflow()) {
         cr.throwException();
       }
       cr = ce.flush(bb);
       if (!cr.isUnderflow()) {
         cr.throwException();
       }
     } catch (CharacterCodingException x) {
       throw new Error(x);
     }
     return bb.position();
   }
 }
Esempio n. 8
0
 /** Encode a string into a ByteBuffer : on return position is the end of the encoding */
 public static int toByteBuffer(CharSequence s, ByteBuffer bb, CharsetEncoder enc) {
   int start = bb.position();
   CharBuffer cBuff = CharBuffer.wrap(s);
   enc.reset();
   CoderResult r = enc.encode(cBuff, bb, true);
   if (r.isOverflow()) throw new InternalErrorException("Bytes.toByteBuffer: encode overflow (1)");
   r = enc.flush(bb);
   if (r.isOverflow()) throw new InternalErrorException("Bytes.toByteBuffer: encode overflow (2)");
   //        if ( r.isUnderflow() )
   //            throw new InternalErrorException("Bytes.toByteBuffer: encode underflow") ;
   int finish = bb.position();
   return finish - start;
 }
 /**
  * Closes this writer. This implementation flushes the buffer as well as the target stream. The
  * target stream is then closed and the resources for the buffer and converter are released.
  *
  * <p>Only the first invocation of this method has any effect. Subsequent calls do nothing.
  *
  * @throws IOException if an error occurs while closing this writer.
  */
 @Override
 public void close() throws IOException {
   synchronized (lock) {
     if (encoder != null) {
       encoder.flush(bytes);
       flush();
       out.flush();
       out.close();
       encoder = null;
       bytes = null;
     }
   }
 }
 byte[] getBytes(String s) {
   CharsetEncoder ce = encoder().reset();
   char[] ca = s.toCharArray();
   int len = (int) (ca.length * ce.maxBytesPerChar());
   byte[] ba = new byte[len];
   if (len == 0) return ba;
   ByteBuffer bb = ByteBuffer.wrap(ba);
   CharBuffer cb = CharBuffer.wrap(ca);
   CoderResult cr = ce.encode(cb, bb, true);
   if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString());
   cr = ce.flush(bb);
   if (!cr.isUnderflow()) throw new IllegalArgumentException(cr.toString());
   if (bb.position() == ba.length) // defensive copy?
   return ba;
   else return Arrays.copyOf(ba, bb.position());
 }
Esempio n. 11
0
  /**
   * Encodes any String into a buffer to send it with a message
   *
   * @param content the String to encode into a buffer
   * @return a buffer containing the (encoded) String.
   */
  public static Buffer encodeString(String content) {
    if (content == null) {
      return null;
    }

    ByteBuffer byteBuffer;
    synchronized (encoder) {
      encoder.reset();
      try {
        byteBuffer = encoder.encode(CharBuffer.wrap(content));
      } catch (CharacterCodingException e) {
        return null;
      }
      encoder.flush(byteBuffer);
    }
    ByteBuf wrappedBuffer = Unpooled.wrappedBuffer(byteBuffer);
    return new Buffer(wrappedBuffer);
  }
Esempio n. 12
0
 static ByteBuffer encodeString(CharBuffer src, Charset charset) {
   final CharsetEncoder encoder = CharsetUtil.getEncoder(charset);
   final ByteBuffer dst =
       ByteBuffer.allocate((int) ((double) src.remaining() * encoder.maxBytesPerChar()));
   try {
     CoderResult cr = encoder.encode(src, dst, true);
     if (!cr.isUnderflow()) {
       cr.throwException();
     }
     cr = encoder.flush(dst);
     if (!cr.isUnderflow()) {
       cr.throwException();
     }
   } catch (CharacterCodingException x) {
     throw new IllegalStateException(x);
   }
   dst.flip();
   return dst;
 }
  private void fillBuffer() throws IOException {
    _byteBuf.clear();
    _charBuf.clear();

    while (_byteBuf.position() == 0) {
      fillCharBuf();
      if (_charBuf.limit() == 0) {
        _byteBuf.limit(0);
        return;
      }

      _encoder.reset();
      CoderResult rslt = _encoder.encode(_charBuf, _byteBuf, true);
      // FIXME - optionally throw on malformed input
      _encoder.flush(_byteBuf);
    }

    _byteBuf.limit(_byteBuf.position());
    _byteBuf.position(0);
  }
  /**
   * Write the UTF-8 representation of a single Unicode code point to the terminal output. The
   * written data will be consumed by the emulation client as input.
   *
   * <p>This implementation encodes the code point and then calls {@link #write(byte[], int, int)}
   * to do the actual writing. It should therefore usually be unnecessary to override this method;
   * override {@link #write(byte[], int, int)} instead.
   *
   * @param codePoint The Unicode code point to write to the terminal.
   */
  public void write(int codePoint) {
    ByteBuffer byteBuf = mWriteByteBuffer;
    if (codePoint < 128) {
      // Fast path for ASCII characters
      byte[] buf = byteBuf.array();
      buf[0] = (byte) codePoint;
      write(buf, 0, 1);
      return;
    }

    CharBuffer charBuf = mWriteCharBuffer;
    CharsetEncoder encoder = mUTF8Encoder;

    charBuf.clear();
    byteBuf.clear();
    Character.toChars(codePoint, charBuf.array(), 0);
    encoder.reset();
    encoder.encode(charBuf, byteBuf, true);
    encoder.flush(byteBuf);
    write(byteBuf.array(), 0, byteBuf.position() - 1);
  }
Esempio n. 15
0
  public byte[] encode(char[] chars, int off, int len, byte[] bytes) {
    ByteBuffer byteBuf = ByteBuffer.wrap(bytes);

    CharBuffer charBuf = CharBuffer.wrap(chars, off, len);
    try {
      CoderResult cr = encoder.encode(charBuf, byteBuf, true);
      if (!cr.isUnderflow()) {
        cr.throwException();
      }
      cr = encoder.flush(byteBuf);
      if (!cr.isUnderflow()) {
        cr.throwException();
      }
    } catch (CharacterCodingException x) {
      // Substitution is always enabled,
      // so this shouldn't happen
      throw new JSONException(x.getMessage(), x);
    }

    int bytesLength = byteBuf.position();
    byte[] copy = new byte[bytesLength];
    System.arraycopy(bytes, 0, copy, 0, bytesLength);
    return copy;
  }
Esempio n. 16
0
 byte[] encode(char[] ca, int off, int len) {
   int en = scale(len, ce.maxBytesPerChar());
   byte[] ba = new byte[en];
   if (len == 0) return ba;
   if (ce instanceof ArrayEncoder) {
     int blen = ((ArrayEncoder) ce).encode(ca, off, len, ba);
     return safeTrim(ba, blen, cs, isTrusted);
   } else {
     ce.reset();
     ByteBuffer bb = ByteBuffer.wrap(ba);
     CharBuffer cb = CharBuffer.wrap(ca, off, len);
     try {
       CoderResult cr = ce.encode(cb, bb, true);
       if (!cr.isUnderflow()) cr.throwException();
       cr = ce.flush(bb);
       if (!cr.isUnderflow()) cr.throwException();
     } catch (CharacterCodingException x) {
       // Substitution is always enabled,
       // so this shouldn't happen
       throw new Error(x);
     }
     return safeTrim(ba, bb.position(), cs, isTrusted);
   }
 }
  protected long buildResponseStringBuf(String s, long r, final long preChain) {
    if (s == null) {
      return 0;
    }

    if (s.length() == 0) {
      return -NGX_HTTP_NO_CONTENT;
    }

    CharsetEncoder charsetEncoder =
        ThreadLocalCoders.encoderFor(DEFAULT_ENCODING)
            .onMalformedInput(CodingErrorAction.REPLACE)
            .onUnmappableCharacter(CodingErrorAction.REPLACE);
    ByteBuffer bb = pickByteBuffer();
    CharBuffer cb = CharBuffer.wrap((char[]) UNSAFE.getObject(s, STRING_CHAR_ARRAY_OFFSET));
    charsetEncoder.reset();
    CoderResult result = CoderResult.UNDERFLOW;
    long first = 0;
    long chain = preChain;
    do {
      result = charsetEncoder.encode(cb, bb, true);
      if (result == CoderResult.OVERFLOW) {
        bb.flip();
        chain =
            ngx_http_clojure_mem_build_temp_chain(
                r, chain, bb.array(), BYTE_ARRAY_OFFSET, bb.remaining());
        if (chain <= 0) {
          return chain;
        }
        bb.clear();
        if (first == 0) {
          first = chain;
        }
      } else if (result == CoderResult.UNDERFLOW) {
        break;
      } else {
        log.error("%s can not decode string : %s", result.toString(), s);
        return -NGX_HTTP_INTERNAL_SERVER_ERROR;
      }
    } while (true);

    while (charsetEncoder.flush(bb) == CoderResult.OVERFLOW) {
      bb.flip();
      chain =
          ngx_http_clojure_mem_build_temp_chain(
              r, chain, bb.array(), BYTE_ARRAY_OFFSET, bb.remaining());
      if (chain <= 0) {
        return chain;
      }
      if (first == 0) {
        first = chain;
      }
      bb.clear();
    }

    if (bb.hasRemaining()) {
      bb.flip();
      chain =
          ngx_http_clojure_mem_build_temp_chain(
              r, chain, bb.array(), BYTE_ARRAY_OFFSET, bb.remaining());
      if (chain <= 0) {
        return chain;
      }
      if (first == 0) {
        first = chain;
      }
      bb.clear();
    }

    return preChain == 0 ? first : chain;
  }