Beispiel #1
0
    private void endBuffer(ZipEntry ze) {
      if (ze == null) {
        return;
      }
      if (store != null) {
        store.close(); // 終了処理は別スレッドで実行中
        String digest = store.getDigest();
        logPersister.addDigest(addDigests, digest);
        store = null;
      } else if (charsetDecoder != null) {
        charBuffer.flip();
        charBuffer.array();
        String accessLogJson =
            new String(charBuffer.array(), charBuffer.position(), charBuffer.limit());
        charsetDecoder = null;
        charBuffer = null;
        AccessLog accessLog = AccessLog.fromJson(accessLogJson);
        if (accessLog == null) {
          return;
        }
        logPersister.addDigest(refDigests, accessLog.getRequestHeaderDigest());
        logPersister.addDigest(refDigests, accessLog.getRequestBodyDigest());
        logPersister.addDigest(refDigests, accessLog.getResponseHeaderDigest());
        logPersister.addDigest(refDigests, accessLog.getResponseBodyDigest());
        accessLog.setId(null);
        accessLog.setPersist(true);

        PersistenceManager pm = JdoUtil.getPersistenceManager();
        logPersister.executeInsert(pm, accessLog);
        if (pm.currentTransaction().isActive()) {
          pm.currentTransaction().rollback();
        }
      }
    }
Beispiel #2
0
  @Override
  public final boolean incrementToken() throws IOException {
    if (isMailto) {
      termAtt.setEmpty();
      // return the scheme + the mail part
      isMailto = false;
      posIncrAtt.setPositionIncrement(0);
      termAtt.copyBuffer(termBuffer.array(), 0, termBuffer.position());
      return true;
    }

    if (input.incrementToken()) {
      final String type = typeAtt.type();
      if (type.equals(TupleTokenizer.getTokenTypes()[TupleTokenizer.URI])
          && this.isMailtoScheme()) {
        this.updateBuffer();
        termBuffer.put(termAtt.buffer(), 0, termAtt.length());
        // return only the mail part
        posIncrAtt.setPositionIncrement(1);
        termAtt.copyBuffer(termBuffer.array(), 7, termBuffer.position() - 7);
      }
      return true;
    }
    return false;
  }
    private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
      byte[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();
      assert (sp <= sl);
      sp = (sp <= sl ? sp : sl);
      char[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      assert (dp <= dl);
      dp = (dp <= dl ? dp : dl);

      try {
        while (sp < sl) {
          byte b = sa[sp];
          if (b >= 0) {
            if (dp >= dl) return CoderResult.OVERFLOW;
            da[dp++] = (char) b;
            sp++;
            continue;
          }
          return CoderResult.malformedForLength(1);
        }
        return CoderResult.UNDERFLOW;
      } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
      }
    }
Beispiel #4
0
 private static String decode(Charset charset, byte[] b) {
   if (b == null) {
     return null;
   }
   final CharBuffer cb = charset.decode(ByteBuffer.wrap(b));
   return new String(cb.array(), 0, cb.length());
 }
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();
      assert (sp <= sl);
      sp = (sp <= sl ? sp : sl);
      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      assert (dp <= dl);
      dp = (dp <= dl ? dp : dl);

      try {
        while (sp < sl) {
          char c = sa[sp];
          if (c < 0x80) {
            if (dp >= dl) return CoderResult.OVERFLOW;
            da[dp] = (byte) c;
            sp++;
            dp++;
            continue;
          }
          if (sgp.parse(c, sa, sp, sl) < 0) return sgp.error();
          return sgp.unmappableResult();
        }
        return CoderResult.UNDERFLOW;
      } finally {
        src.position(sp - src.arrayOffset());
        dst.position(dp - dst.arrayOffset());
      }
    }
  @Override
  public boolean incrementToken() throws IOException {
    if (!terms.isEmpty()) {
      char[] buffer = terms.poll();
      termAttribute.setEmpty();
      termAttribute.copyBuffer(buffer, 0, buffer.length);
      posIncAttr.setPositionIncrement(1);
      return true;
    }

    if (!input.incrementToken()) {
      return false;
    } else {
      final char term[] = termAttribute.buffer();
      final int length = termAttribute.length();

      int k = 0;
      for (; k < length; k++) {
        if (term[k] == tokenDelimiter) {
          break;
        }
      }

      LinkedList<CharBuffer> buffers = permuteTerms(term, 0, length);

      Iterator iter = buffers.iterator();
      while (iter.hasNext()) {
        CharBuffer cb = (CharBuffer) iter.next();
        terms.add(cb.array());
      }

      // we return true and leave the original token unchanged
      return true;
    }
  }
    private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
      byte[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      char[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();

      CoderResult cr = CoderResult.UNDERFLOW;
      if ((dl - dp) < (sl - sp)) {
        sl = sp + (dl - dp);
        cr = CoderResult.OVERFLOW;
      }

      while (sp < sl) {
        char c = decode(sa[sp]);
        if (c == UNMAPPABLE_DECODING) {
          return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp);
        }
        da[dp++] = c;
        sp++;
      }
      return withResult(cr, src, sp, dst, dp);
    }
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int len = Math.min(dl - dp, sl - sp);

      while (len-- > 0) {
        char c = sa[sp];
        int b = encode(c);
        if (b == UNMAPPABLE_ENCODING) {
          if (Character.isSurrogate(c)) {
            if (sgp == null) sgp = new Surrogate.Parser();
            if (sgp.parse(c, sa, sp, sl) < 0) {
              return withResult(sgp.error(), src, sp, dst, dp);
            }
            return withResult(sgp.unmappableResult(), src, sp, dst, dp);
          }
          return withResult(CoderResult.unmappableForLength(1), src, sp, dst, dp);
        }
        da[dp++] = (byte) b;
        sp++;
      }
      return withResult(sp < sl ? CoderResult.OVERFLOW : CoderResult.UNDERFLOW, src, sp, dst, dp);
    }
Beispiel #9
0
 public static byte[] toBytes(char[] chars) {
   CharBuffer charBuffer = CharBuffer.wrap(chars);
   ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
   byte[] bytes =
       Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
   Arrays.fill(charBuffer.array(), '\u0000'); // clear sensitive data
   Arrays.fill(byteBuffer.array(), (byte) 0); // clear sensitive data
   return bytes;
 }
 /**
  * Wraps the result of {@linkplain #getCharContent} in a Reader. Subclasses can change this
  * behavior as long as the contract of {@link FileObject} is obeyed.
  *
  * @param ignoreEncodingErrors {@inheritDoc}
  * @return a Reader wrapping the result of getCharContent
  * @throws IllegalStateException {@inheritDoc}
  * @throws UnsupportedOperationException {@inheritDoc}
  * @throws IOException {@inheritDoc}
  */
 public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
   CharSequence charContent = getCharContent(ignoreEncodingErrors);
   if (charContent == null) throw new UnsupportedOperationException();
   if (charContent instanceof CharBuffer) {
     CharBuffer buffer = (CharBuffer) charContent;
     if (buffer.hasArray()) return new CharArrayReader(buffer.array());
   }
   return new StringReader(charContent.toString());
 }
Beispiel #11
0
  // 将字节转为字符(解码)
  public static char[] getChars(byte[] bytes) {
    Charset cs = Charset.forName("UTF-8");
    ByteBuffer bb = ByteBuffer.allocate(bytes.length);
    bb.put(bytes);
    bb.flip();
    CharBuffer cb = cs.decode(bb);

    return cb.array();
  }
Beispiel #12
0
 private BigDecimal toBigDecimal() {
   if (lineBuffer.hasArray()) {
     char[] array = lineBuffer.array();
     int offset = lineBuffer.arrayOffset() + lineBuffer.position();
     int length = lineBuffer.remaining();
     return new BigDecimal(array, offset, length);
   } else {
     return new BigDecimal(lineBuffer.toString());
   }
 }
Beispiel #13
0
  public void run() {
    byteBuffer = ByteBuffer.allocate(BUFFER_SIZE);
    charBuffer = CharBuffer.allocate(BUFFER_SIZE);

    /* for East Asian character widths */
    byte[] wideAttribute = new byte[BUFFER_SIZE];

    byteArray = byteBuffer.array();
    charArray = charBuffer.array();

    CoderResult result;

    int bytesRead = 0;
    byteBuffer.limit(0);
    int bytesToRead;
    int offset;
    int charWidth;

    EastAsianWidth measurer = EastAsianWidth.getInstance();

    try {
      while (true) {
        charWidth = bridge.charWidth;
        bytesToRead = byteBuffer.capacity() - byteBuffer.limit();
        offset = byteBuffer.arrayOffset() + byteBuffer.limit();
        bytesRead = transport.read(byteArray, offset, bytesToRead);

        if (bytesRead > 0) {
          byteBuffer.limit(byteBuffer.limit() + bytesRead);

          synchronized (this) {
            result = decoder.decode(byteBuffer, charBuffer, false);
          }

          if (result.isUnderflow() && byteBuffer.limit() == byteBuffer.capacity()) {
            byteBuffer.compact();
            byteBuffer.limit(byteBuffer.position());
            byteBuffer.position(0);
          }

          offset = charBuffer.position();

          measurer.measure(charArray, 0, offset, wideAttribute, bridge.defaultPaint, charWidth);
          buffer.putString(charArray, wideAttribute, 0, charBuffer.position());
          bridge.propagateConsoleText(charArray, charBuffer.position());
          charBuffer.clear();
          bridge.redraw();
        }
      }
    } catch (IOException e) {
      Log.e(TAG, "Problem while handling incoming data in relay thread", e);
    }
  }
 public static String newStringFromSplit(
     CharsetDecoder decoder,
     CharsetDecoder utf8Decoder,
     String encoding,
     byte[] fieldBytes,
     int length) {
   ByteBuffer fieldBuf = ByteBuffer.wrap(fieldBytes, 0, length);
   CharBuffer fieldCharBuf = CharBuffer.allocate(length);
   utf8Decoder.reset();
   CoderResult res = utf8Decoder.decode(fieldBuf, fieldCharBuf, true);
   if (res.isError() && decoder != null) {
     decoder.reset();
     res = decoder.decode(fieldBuf, fieldCharBuf, true);
     if (!res.isError()) {
       decoder.flush(fieldCharBuf);
       return new String(fieldCharBuf.array());
     }
   } else {
     utf8Decoder.flush(fieldCharBuf);
     return new String(fieldCharBuf.array());
   }
   return "";
 }
    private CoderResult encodeArrayLoop(CharBuffer src, ByteBuffer dst) {
      char[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      byte[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int dlASCII = dp + Math.min(sl - sp, dl - dp);

      // ASCII only loop
      while (dp < dlASCII && sa[sp] < '\u0080') da[dp++] = (byte) sa[sp++];
      while (sp < sl) {
        char c = sa[sp];
        if (c < 0x80) {
          // Have at most seven bits
          if (dp >= dl) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) c;
        } else if (c < 0x800) {
          // 2 bytes, 11 bits
          if (dl - dp < 2) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xc0 | (c >> 6));
          da[dp++] = (byte) (0x80 | (c & 0x3f));
        } else if (isSurrogate(c)) {
          // Have a surrogate pair
          if (sgp == null) sgp = new Parser();
          int uc = sgp.parse(c, sa, sp, sl);
          if (uc < 0) {
            updatePositions(src, sp, dst, dp);
            return sgp.error();
          }
          if (dl - dp < 4) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xf0 | ((uc >> 18)));
          da[dp++] = (byte) (0x80 | ((uc >> 12) & 0x3f));
          da[dp++] = (byte) (0x80 | ((uc >> 6) & 0x3f));
          da[dp++] = (byte) (0x80 | (uc & 0x3f));
          sp++; // 2 chars
        } else {
          // 3 bytes, 16 bits
          if (dl - dp < 3) return overflow(src, sp, dst, dp);
          da[dp++] = (byte) (0xe0 | ((c >> 12)));
          da[dp++] = (byte) (0x80 | ((c >> 6) & 0x3f));
          da[dp++] = (byte) (0x80 | (c & 0x3f));
        }
        sp++;
      }
      updatePositions(src, sp, dst, dp);
      return CoderResult.UNDERFLOW;
    }
 private void fillBuffer() throws IOException {
   if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) {
     encoderIn.compact();
     int i = encoderIn.position();
     int j = reader.read(encoderIn.array(), i, encoderIn.remaining());
     if (j == -1) {
       endOfInput = true;
     } else {
       encoderIn.position(i + j);
     }
     encoderIn.flip();
   }
   encoderOut.compact();
   lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput);
   encoderOut.flip();
 }
Beispiel #17
0
 static Reader adjustReader2(Reader reader) {
   try {
     StringBuilder result = new StringBuilder(40);
     int pretype = 0;
     CharBuffer cb = CharBuffer.allocate(40);
     while (reader.read(cb) != -1) {
       for (char c : cb.array()) {
         int currtype = Character.getType(c);
         if (isSplit(pretype, currtype)) result.append(' ');
         result.append(c);
         pretype = currtype;
       }
       cb.flip();
     }
     return new StringReader(result.toString());
   } catch (IOException ex) {
     ex.printStackTrace();
     return new StringReader("");
   }
 }
 public String readAsString(Charset charset) throws CharacterCodingException {
   int unreadSize = totalBytesUnread();
   if (unreadSize > 0) {
     CharsetDecoder decoder =
         charset
             .newDecoder()
             .onMalformedInput(CodingErrorAction.REPLACE)
             .onUnmappableCharacter(CodingErrorAction.REPLACE);
     CharBuffer charbuffer = CharBuffer.allocate(unreadSize);
     ByteBuffer buf = null;
     while (prepareRead() != -1) {
       buf = currentReadChunk.readToNioBuffer();
       boolean endOfInput = (prepareRead() == -1);
       CoderResult result = decoder.decode(buf, charbuffer, endOfInput);
       if (endOfInput) {
         if (!result.isUnderflow()) {
           result.throwException();
         }
       }
     }
     CoderResult result = decoder.flush(charbuffer);
     if (buf.hasRemaining()) {
       throw new IllegalStateException("There's a bug here, buffer wasn't read fully.");
     }
     if (!result.isUnderflow()) result.throwException();
     charbuffer.flip();
     String str;
     if (charbuffer.hasArray()) {
       int len = charbuffer.remaining();
       char[] ch = charbuffer.array();
       if (len != ch.length) {
         ch = ArrayUtils.subarray(ch, 0, len);
       }
       str = StringCharArrayAccessor.createString(ch);
     } else {
       str = charbuffer.toString();
     }
     return str;
   }
   return null;
 }
  /**
   * 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);
  }
  /**
   * Writes any remaining output to the output buffer and resets the converter to its initial state.
   *
   * @param output char array to receive flushed output.
   * @param outStart start writing to output array at this offset.
   * @param outEnd stop writing to output array at this offset (exclusive).
   * @exception MalformedInputException if the output to be flushed contained a partial or invalid
   *     multibyte character sequence. flush will write what it can to the output buffer and reset
   *     the converter before throwing this exception. An additional call to flush is not required.
   * @exception ConversionBufferFullException if output array is filled before all the output can be
   *     flushed. flush will write what it can to the output buffer and remember its state. An
   *     additional call to flush with a new output buffer will conclude the operation.
   */
  public int flush(char[] output, int outStart, int outEnd)
      throws MalformedInputException, ConversionBufferFullException {

    byteOff = charOff = 0;
    if (outStart >= outEnd || outStart >= output.length) throw new ConversionBufferFullException();
    if (dst != null && dst.array() == output) dst.position(outStart).limit(outEnd);
    else dst = CharBuffer.wrap(output, outStart, outEnd - outStart);

    CoderResult cr = null;
    try {
      if (src != null) cr = decoder.decode((ByteBuffer) src.clear(), dst, true);
      assert !cr.isUnmappable();
      if (cr.isMalformed()) {
        badInputLength = cr.length();
        reset();
        throw new MalformedInputException();
      }
    } catch (IllegalStateException ise) {
      if (src != null) cr = decoder.reset().decode(src, dst, true);
    }
    try {
      cr = decoder.flush(dst);
    } catch (Exception e) {
      assert false;
    } finally {
      byteOff = 0;
      charOff = dst.position();
      src = null;
    }
    if (cr.isOverflow()) throw new ConversionBufferFullException();

    // Return the length written to the output buffer
    if (cr.isUnderflow()) {
      int written = charOff - outStart;
      reset();
      return written;
    }
    assert false;
    return -1; // should be never reached
  }
 private void consumeDecoded() {
   decodeBuffer.flip();
   if (decodeBuffer.hasRemaining()) {
     char[] array = decodeBuffer.array();
     for (int i = decodeBuffer.position(), n = decodeBuffer.limit(); i < n; i++) {
       char c = array[i];
       if (c == '\t') {
         lineBuffer.append(ESCAPE_CHAR);
         lineBuffer.append(ESCAPE_HT);
       } else if (c == '\n') {
         lineBuffer.append(ESCAPE_CHAR);
         lineBuffer.append(ESCAPE_LF);
       } else if (c == '\\') {
         lineBuffer.append(ESCAPE_CHAR);
         lineBuffer.append(ESCAPE_CHAR);
       } else {
         lineBuffer.append(c);
       }
     }
   }
   decodeBuffer.clear();
 }
  /**
   * Converts an array of bytes containing characters in an external encoding into an array of
   * Unicode characters. This method allows a buffer by buffer conversion of a data stream. The
   * state of the conversion is saved between calls to convert. Among other things, this means
   * multibyte input sequences can be split between calls. If a call to convert results in an
   * exception, the conversion may be continued by calling convert again with suitably modified
   * parameters. All conversions should be finished with a call to the flush method.
   *
   * @return the number of bytes written to output.
   * @param input byte array containing text to be converted.
   * @param inStart begin conversion at this offset in input array.
   * @param inEnd stop conversion at this offset in input array (exclusive).
   * @param output character array to receive conversion result.
   * @param outStart start writing to output array at this offset.
   * @param outEnd stop writing to output array at this offset (exclusive).
   * @exception MalformedInputException if the input buffer contains any sequence of bytes that is
   *     illegal for the input character set.
   * @exception UnknownCharacterException for any character that that cannot be converted to
   *     Unicode. Thrown only when converter is not in substitution mode.
   * @exception ConversionBufferFullException if output array is filled prior to converting all the
   *     input.
   */
  public int convert(byte[] input, int inStart, int inEnd, char[] output, int outStart, int outEnd)
      throws UnknownCharacterException, MalformedInputException, ConversionBufferFullException {

    byteOff = inStart;
    charOff = outStart;
    // throw exceptions compatible to legacy ByteToCharXxx converters
    if (inStart >= inEnd) return 0;
    if (inStart >= input.length) throw new ArrayIndexOutOfBoundsException(inStart);
    if (outStart >= outEnd || outStart >= output.length) throw new ConversionBufferFullException();

    if (src != null && src.array() == input) src.position(inStart).limit(inEnd);
    else src = ByteBuffer.wrap(input, inStart, inEnd - inStart);
    if (dst != null && dst.array() == output) dst.position(outStart).limit(outEnd);
    else dst = CharBuffer.wrap(output, outStart, outEnd - outStart);

    CoderResult cr;
    try {
      cr = decoder.decode(src, dst, false);
    } catch (IllegalStateException ise) {
      cr = decoder.reset().decode(src, dst, false);
    } finally {
      byteOff = src.position();
      charOff = dst.position();
    }
    if (cr.isUnmappable()) {
      badInputLength = cr.length();
      throw new UnknownCharacterException();
    }
    if (cr.isMalformed()) {
      badInputLength = cr.length();
      throw new MalformedInputException();
    }
    if (cr.isOverflow()) throw new ConversionBufferFullException();

    // Return the length written to the output buffer
    if (cr.isUnderflow()) return charOff - outStart;
    return -1; // should be never reached
  }
Beispiel #23
0
  /**
   * Creates a new big-endian buffer whose content is a subregion of the specified {@code string}
   * encoded in the specified {@code charset}. The new buffer's {@code readerIndex} and {@code
   * writerIndex} are {@code 0} and the length of the encoded string respectively.
   */
  public static ByteBuf copiedBuffer(CharSequence string, int offset, int length, Charset charset) {
    if (string == null) {
      throw new NullPointerException("string");
    }
    if (length == 0) {
      return EMPTY_BUFFER;
    }

    if (string instanceof CharBuffer) {
      CharBuffer buf = (CharBuffer) string;
      if (buf.hasArray()) {
        return copiedBuffer(
            buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset);
      }

      buf = buf.slice();
      buf.limit(length);
      buf.position(offset);
      return copiedBuffer(buf, charset);
    }

    return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset);
  }
  public String ascii_to_latin1(String str) {
    if (str != null) {
      try {
        InputStream is = new ByteArrayInputStream(str.getBytes());
        String text = "";

        // setup readers with Latin-1 (ISO 8859-1) encoding
        BufferedReader i = new BufferedReader(new InputStreamReader(is, "8859_1"));

        int numBytes;
        CharBuffer buf = CharBuffer.allocate(512);
        while ((numBytes = i.read(buf)) != -1) {
          text += String.copyValueOf(buf.array(), 0, numBytes);
          buf.clear();
        }

        return text;
      } catch (Exception e) {
        e.printStackTrace();
      }
    }
    return "";
  }
Beispiel #25
0
  /**
   * Convert the byte array to char array with respect to given charset.
   *
   * @param byteArray
   * @param charset null or "" means default charset
   * @exception CharacterCodingException
   */
  public static char[] convertByteArrayToCharArray(byte[] byteArray, String charset)
      throws CharacterCodingException {

    if (byteArray == null) {
      return null;
    }

    byte[] bArray = (byte[]) byteArray.clone();
    ByteBuffer byteBuffer = ByteBuffer.wrap(bArray);
    Charset charSet;
    if (charset == null || "".equals(charset)) {
      charSet = Charset.defaultCharset();
    } else if (Charset.isSupported(charset)) {
      charSet = Charset.forName(charset);
    } else {
      CharacterCodingException e = new CharacterCodingException();
      e.initCause(new UnsupportedCharsetException(charset));
      throw e;
    }

    CharsetDecoder decoder = charSet.newDecoder();
    CharBuffer charBuffer = null;
    try {
      charBuffer = decoder.decode(byteBuffer);
    } catch (CharacterCodingException cce) {
      throw cce;
    } catch (Throwable t) {
      CharacterCodingException e = new CharacterCodingException();
      e.initCause(t);
      throw e;
    }
    char[] result = (char[]) charBuffer.array().clone();
    clear(byteBuffer);
    clear(charBuffer);

    return result;
  }
 @Override
 public void run() {
   try {
     InputStream in = client.getInputStream();
     CharBuffer buffer = CharBuffer.allocate(5000);
     char ch;
     while (true) {
       ch = (char) in.read();
       if (((int) ch) == -1) {
         break;
       }
       while (ch != EOL) {
         buffer.put(ch);
         ch = (char) in.read();
       }
       buffer.flip();
       String s = new String(buffer.array(), 0, buffer.limit());
       messageReceiver.sendMessage(s + "\n");
       buffer.position(0);
       buffer.limit(5000);
     }
   } catch (Exception e) {
     e.printStackTrace();
     if (LOGGER.isLoggable(Level.WARNING)) {
       LOGGER.warning("Unable to process mesages from client" + client);
     }
   } finally {
     if (client != null) {
       try {
         client.close();
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
 }
 /**
  * Returns the password that is bound to the username and is stored for the specified <code>
  * forUser</code> If the <code>forUser</code> is not specified the system user is used.
  *
  * @param forUser the user for whom the password is being retrieved
  * @param username the matching username to go with the password
  * @return the password as a character array or empty object if password could not be loaded
  */
 public static Optional<char[]> getPassword(Optional<String> forUser, String username) {
   if (username == null) {
     return Optional.empty();
   }
   final String user = forUser.orElse(System.getProperty(SYSTEM_PROPERTY_USER_NAME));
   try {
     byte[] val =
         SecurePreferences.getSecurePreferences()
             .node(Activator.ID)
             .node(user)
             .node(username)
             .getByteArray(PREF_PASSWORD, null);
     if (val != null) {
       CharBuffer buffer = StandardCharsets.UTF_8.newDecoder().decode(ByteBuffer.wrap(val));
       return Optional.of(Arrays.copyOfRange(buffer.array(), buffer.position(), buffer.limit()));
     }
   } catch (StorageException | IOException e) {
     SaveRestoreService.LOGGER.log(
         Level.WARNING,
         e,
         () -> String.format("Could not read the password for '%s' from secured storage.", user));
   }
   return Optional.empty();
 }
  /* Read in DNA or Peptide FA record in mixed case.   Allow any upper or lower case
   * letter, or the dash character in.
   *
   * boolean faMixedSpeedReadNext(struct lineFile *lf, DNA **retDna, int *retSize, char **retName)
   * */
  public boolean faMixedSpeedReadNext(LineFile f, DnaSeq seq)
      throws IOException, FaFormatException {
    char c;
    int bufIx = 0;
    String name = null; // the name of teh fa sequence.
    int lineSize, i;
    String line;

    // this could be necesary
    // dnaUtilOpen();

    /* Read first line, make sure it starts with '>', and read first word
     * as name of sequence. */
    if ((line = f.lineFileNext()) == null) { // f, &line, &lineSize
      // *retDna = NULL;
      // *retSize = 0;
      return false;
    }
    if (line.charAt(0) == '>') {
      name = line.substring(1).split(" ")[0]; // .substring(1).replaceAll(" ", "");
      if (line.equals("")) {
        System.err.println("Expecting sequence name after '>' " + f.getFile().getName());
        throw new FaFormatException();
      }
    } else {
      System.err.println("Expecting '>' line " + f.getFile().getName());
      throw new FaFormatException();
    }
    /* Read until next '>' */

    for (; ; ) {
      if ((line = f.lineFileNext()) == null) // !lineFileNext(lf, &line, &lineSize)
      break;
      if (line.charAt(0) == '>') {
        f.lineFileReuse(line);
        break;
      }
      for (char m : line.toCharArray()) {
        if (Character.isLetter(m) || m == '-') {
          buff.put(m);
        }
      }
    }
    // if (bufIx >= faFastBufSize)
    //    expandFaFastBuf(bufIx, 0);
    // faFastBuf[bufIx] = 0;
    // *retDna = faFastBuf;
    // *retSize = bufIx;
    char[] dna = new char[buff.position()];
    char[] c_buff = buff.array();
    for (int k = 0; k < dna.length; k++) {
      dna[k] = c_buff[k];
    }
    seq.setName(name);
    seq.setDna(dna);

    // if (bufIx == 0){
    //    System.err.println("Invalid fasta format: sequence size == 0 for element " + name);
    // }
    buff.clear();
    return true;
  }
Beispiel #29
0
 private void flushOutput() throws IOException {
   writer.write(out.array(), 0, out.position());
   out.clear();
 }
    private CoderResult decodeArrayLoop(ByteBuffer src, CharBuffer dst) {
      // This method is optimized for ASCII input.
      byte[] sa = src.array();
      int sp = src.arrayOffset() + src.position();
      int sl = src.arrayOffset() + src.limit();

      char[] da = dst.array();
      int dp = dst.arrayOffset() + dst.position();
      int dl = dst.arrayOffset() + dst.limit();
      int dlASCII = dp + Math.min(sl - sp, dl - dp);

      // ASCII only loop
      while (dp < dlASCII && sa[sp] >= 0) da[dp++] = (char) sa[sp++];
      while (sp < sl) {
        int b1 = sa[sp];
        if (b1 >= 0) {
          // 1 byte, 7 bits: 0xxxxxxx
          if (dp >= dl) return xflow(src, sp, sl, dst, dp, 1);
          da[dp++] = (char) b1;
          sp++;
        } else if ((b1 >> 5) == -2 && (b1 & 0x1e) != 0) {
          // 2 bytes, 11 bits: 110xxxxx 10xxxxxx
          if (sl - sp < 2 || dp >= dl) return xflow(src, sp, sl, dst, dp, 2);
          int b2 = sa[sp + 1];
          if (isNotContinuation(b2)) return malformedForLength(src, sp, dst, dp, 1);
          da[dp++] = (char) (((b1 << 6) ^ b2) ^ (((byte) 0xC0 << 6) ^ ((byte) 0x80)));
          sp += 2;
        } else if ((b1 >> 4) == -2) {
          // 3 bytes, 16 bits: 1110xxxx 10xxxxxx 10xxxxxx
          int srcRemaining = sl - sp;
          if (srcRemaining < 3 || dp >= dl) {
            if (srcRemaining > 1 && isMalformed3_2(b1, sa[sp + 1]))
              return malformedForLength(src, sp, dst, dp, 1);
            return xflow(src, sp, sl, dst, dp, 3);
          }
          int b2 = sa[sp + 1];
          int b3 = sa[sp + 2];
          if (isMalformed3(b1, b2, b3)) return malformed(src, sp, dst, dp, 3);
          char c =
              (char)
                  ((b1 << 12)
                      ^ (b2 << 6)
                      ^ (b3 ^ (((byte) 0xE0 << 12) ^ ((byte) 0x80 << 6) ^ ((byte) 0x80))));
          if (isSurrogate(c)) return malformedForLength(src, sp, dst, dp, 3);
          da[dp++] = c;
          sp += 3;
        } else if ((b1 >> 3) == -2) {
          // 4 bytes, 21 bits: 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
          int srcRemaining = sl - sp;
          if (srcRemaining < 4 || dl - dp < 2) {
            if (srcRemaining > 1 && isMalformed4_2(b1, sa[sp + 1]))
              return malformedForLength(src, sp, dst, dp, 1);
            if (srcRemaining > 2 && isMalformed4_3(sa[sp + 2]))
              return malformedForLength(src, sp, dst, dp, 2);
            return xflow(src, sp, sl, dst, dp, 4);
          }
          int b2 = sa[sp + 1];
          int b3 = sa[sp + 2];
          int b4 = sa[sp + 3];
          int uc =
              ((b1 << 18)
                  ^ (b2 << 12)
                  ^ (b3 << 6)
                  ^ (b4
                      ^ (((byte) 0xF0 << 18)
                          ^ ((byte) 0x80 << 12)
                          ^ ((byte) 0x80 << 6)
                          ^ ((byte) 0x80))));
          if (isMalformed4(b2, b3, b4)
              ||
              // shortest form check
              !Character.isSupplementaryCodePoint(uc)) {
            return malformed(src, sp, dst, dp, 4);
          }
          da[dp++] = highSurrogate(uc);
          da[dp++] = lowSurrogate(uc);
          sp += 4;
        } else return malformed(src, sp, dst, dp, 1);
      }
      return xflow(src, sp, sl, dst, dp, 0);
    }