Example #1
0
    public String get() {

      int i = 0;
      if (DBCBaseStruct.this.lang < 0) {
        for (int j = 0; j < 16; j++) {
          if (getByteBuffer().getInt(getByteBufferPosition() + offset() + (4 * j)) > 0) {
            DBCBaseStruct.this.lang = j;
            break;
          }
        }
      }
      if (DBCBaseStruct.this.lang < 0) {
        return "";
      }
      for (;
          (this.tsring[i] =
                  getByteBuffer()
                      .get(
                          i
                              + DBCBaseStruct.this.stringBufPos
                              + getByteBuffer()
                                  .getInt(
                                      getByteBufferPosition()
                                          + offset()
                                          + (4 * DBCBaseStruct.this.lang))))
              != 0;
          i++) {}
      try {
        return this.decoder.decode(ByteBuffer.wrap(this.tsring, 0, i)).toString();
      } catch (final CharacterCodingException e) {
        e.printStackTrace();
      }
      return "";
    }
Example #2
0
 public static void saveFile(File file, String content, String charsetName) {
   // if (Utils.isEmpty(fileName) || Utils.isEmpty(content)) {
   // return;
   // }
   // logger.info("save file:" + fileName + " charset:" + charsetName);
   file.getParentFile().mkdirs();
   Charset cs;
   if (null == charsetName || "".equals(charsetName)) {
     cs = Charset.defaultCharset();
   } else {
     cs = Charset.forName(charsetName);
   }
   CharsetEncoder encoder = cs.newEncoder();
   FileOutputStream os = null;
   FileChannel out = null;
   try {
     os = new FileOutputStream(file);
     out = os.getChannel();
     out.write(encoder.encode(CharBuffer.wrap(content)));
   } catch (CharacterCodingException e) {
     e.printStackTrace();
   } catch (IOException e) {
     e.printStackTrace();
   } finally {
     close(out);
     close(os);
   }
 }
Example #3
0
 /**
  * Convert text back to string
  *
  * @see java.lang.Object#toString()
  */
 public String toString() {
   try {
     return decode(bytes, 0, length);
   } catch (CharacterCodingException e) {
     throw new RuntimeException("Should not have happened " + e.toString());
   }
 }
Example #4
0
 @Override
 public void completed(Integer i, ByteBuffer buf) {
   if (i > 0) {
     buf.flip();
     try {
       msg = decoder.decode(buf).toString();
       System.out.println("收到" + socket.getRemoteAddress().toString() + "的消息:" + msg);
       buf.compact();
     } catch (CharacterCodingException e) {
       e.printStackTrace();
     } catch (IOException e) {
       e.printStackTrace();
     }
     socket.read(buf, buf, this);
     try {
       write(socket);
     } catch (UnsupportedEncodingException ex) {
       Logger.getLogger(AioReadHandler.class.getName()).log(Level.SEVERE, null, ex);
     }
   } else if (i == -1) {
     try {
       System.out.println("客户端断线:" + socket.getRemoteAddress().toString());
       buf = null;
     } catch (IOException e) {
       e.printStackTrace();
     }
   }
 }
Example #5
0
  /**
   * Find potential matches for a query
   *
   * @param query The query string
   * @return Search results object
   */
  public EdictSearchResults search(String query) {

    CharsetEncoder encoder = this.dictionary.getCharacterHandler().getCharsetEncoder();
    ByteBuffer encodedQuery = null;
    try {
      encodedQuery = encoder.encode(CharBuffer.wrap(query));
    } catch (CharacterCodingException e) {
      // If we can't encode it we can't search for it here
      // TODO some sort of exception
      return null;
    }

    try {

      EdictComparator comparator = this.dictionary.comparator();

      int start = 0;
      int end = this.dictionary.getIndexSize() - 1;
      int match = -1;

      do {
        int current = start + ((end - start) / 2);

        int character =
            comparator.compareLeft(encodedQuery, this.dictionary.getIndexEntry(current));
        if (character > 0) {
          start = current + 1;
        } else if (character < 0) {
          end = current - 1;
        } else {
          match = current;
        }
      } while ((start <= end) && (match == -1));

      if (match != -1) {
        end = this.dictionary.getIndexSize() - 1;
        int min = match;
        int max = match;
        while ((min > 0)
            && (comparator.compareLeft(encodedQuery, this.dictionary.getIndexEntry(min - 1))
                == 0)) {
          min--;
        }
        while ((max < end)
            && (comparator.compareLeft(encodedQuery, this.dictionary.getIndexEntry(max + 1))
                == 0)) {
          max++;
        }

        return new EdictSearchResults(this.dictionary, encodedQuery, min, max);
      }

    } catch (CharacterCodingException e) {
      // Shouldn't happen. If any entries of the dictionary were broken, the term index should omit
      // all terms from that entry
      e.printStackTrace();
    }

    return new EdictSearchResults(this.dictionary, encodedQuery, null, null);
  }
Example #6
0
 /** Set to contain the contents of a string. */
 public void set(String string) {
   try {
     ByteBuffer bb = encode(string, true);
     bytes = bb.array();
     length = bb.limit();
   } catch (CharacterCodingException e) {
     throw new RuntimeException("Should not have happened " + e.toString());
   }
 }
  /**
   * Write String into byte array
   *
   * <p>It will remove a trailing null terminator if exists if the option
   * RemoveTrailingTerminatorOnWrite has been set.
   *
   * @return the data as a byte array in format to write to file
   */
  public byte[] writeByteArray() {
    byte[] data;
    // Try and write to buffer using the CharSet defined by getTextEncodingCharSet()
    String charSetName = getTextEncodingCharSet();
    try {

      stripTrailingNull();

      // Special Handling because there is no UTF16 BOM LE charset
      String stringValue = (String) value;
      String actualCharSet = null;
      if (charSetName.equals(TextEncoding.CHARSET_UTF_16)) {
        if (TagOptionSingleton.getInstance().isEncodeUTF16BomAsLittleEndian()) {
          actualCharSet = TextEncoding.CHARSET_UTF_16_LE_ENCODING_FORMAT;
        } else {
          actualCharSet = TextEncoding.CHARSET_UTF_16_BE_ENCODING_FORMAT;
        }
      }

      // Ensure large enough for any encoding
      ByteBuffer outputBuffer = ByteBuffer.allocate((stringValue.length() + 3) * 3);

      // Ensure each string (if multiple values) is written with BOM by writing separately
      List<String> values = splitByNullSeperator(stringValue);
      checkTrailingNull(values, stringValue);

      // For each value
      for (int i = 0; i < values.size(); i++) {
        String next = values.get(i);
        if (actualCharSet != null) {
          if (actualCharSet.equals(TextEncoding.CHARSET_UTF_16_LE_ENCODING_FORMAT)) {
            outputBuffer.put(writeStringUTF16LEBOM(next, i, values.size()));
          } else if (actualCharSet.equals(TextEncoding.CHARSET_UTF_16_BE_ENCODING_FORMAT)) {
            outputBuffer.put(writeStringUTF16BEBOM(next, i, values.size()));
          }
        } else {
          CharsetEncoder charsetEncoder = Charset.forName(charSetName).newEncoder();
          charsetEncoder.onMalformedInput(CodingErrorAction.IGNORE);
          charsetEncoder.onUnmappableCharacter(CodingErrorAction.IGNORE);
          outputBuffer.put(writeString(charsetEncoder, next, i, values.size()));
        }
      }
      outputBuffer.flip();
      data = new byte[outputBuffer.limit()];
      outputBuffer.rewind();
      outputBuffer.get(data, 0, outputBuffer.limit());
      setSize(data.length);
    }
    // https://bitbucket.org/ijabz/jaudiotagger/issue/1/encoding-metadata-to-utf-16-can-fail-if
    catch (CharacterCodingException ce) {
      logger.severe(ce.getMessage() + ":" + charSetName + ":" + value);
      throw new RuntimeException(ce);
    }
    return data;
  }
  public static ByteBuffer stringEncode(String s) {
    ByteBuffer oEncodedChat;
    Charset oCharset = Charset.forName("utf8");
    CharsetEncoder encoder = oCharset.newEncoder();

    try {
      oEncodedChat = encoder.encode(CharBuffer.wrap(s));
      return oEncodedChat;

    } catch (CharacterCodingException ex) {
      ex.printStackTrace();
    }
    return null;
  }
  public static String stringDecode(ByteBuffer oByteBuffer) {
    CharBuffer oDecodedChat;
    Charset charset = Charset.forName("utf8");
    CharsetDecoder decoder = charset.newDecoder();

    try {
      oDecodedChat = decoder.decode(oByteBuffer);
      return oDecodedChat.toString();

    } catch (CharacterCodingException ex) {
      ex.printStackTrace();
    }
    return null;
  }
Example #10
0
    public String get() {

      int i = 0;
      final int index = getByteBufferPosition() + offset();
      for (;
          (this.tsring[i] =
                  getByteBuffer()
                      .get(i + DBCBaseStruct.this.stringBufPos + getByteBuffer().getInt(index)))
              != 0;
          i++) {}
      try {
        return this.decoder.decode(ByteBuffer.wrap(this.tsring, 0, i)).toString();
      } catch (final CharacterCodingException e) {
        e.printStackTrace();
      }
      return "";
    }
Example #11
0
  /** @param args */
  public static void main(String[] args) {

    Charset charset = Charset.forName("UTF-8");
    CharsetEncoder encoder = charset.newEncoder();
    byte[] b = null;
    try {
      // Convert a string to UTF-8 bytes in a ByteBuffer
      ByteBuffer bbuf = encoder.encode(CharBuffer.wrap("http://www.google.co.in/"));
      b = bbuf.array();
    } catch (CharacterCodingException e) {
      System.out.println(e.getMessage());
    }

    String data;
    try {
      data = new String(b, "UTF-8");
      // get a byte matrix for the data
      BitMatrix matrix = null;
      ByteMatrix byteMatrix = null;
      int h = 100;
      int w = 100;
      com.google.zxing.Writer writer = new MultiFormatWriter();
      try {
        Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>(2);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        matrix = writer.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, w, h, hints);
      } catch (com.google.zxing.WriterException e) {
        System.out.println(e.getMessage());
      }

      // change this path to match yours (this is my mac home folder, you can use: c:\\qr_png.png if
      // you are on windows)
      String filePath = "D:\\CreateQR.JPG";
      File file = new File(filePath);
      try {
        MatrixToImageWriter.writeToFile(matrix, "JPG", file);
        System.out.println("printing to " + file.getAbsolutePath());
      } catch (IOException e) {
        System.out.println(e.getMessage());
      }
    } catch (UnsupportedEncodingException e) {
      System.out.println(e.getMessage());
    }
  }
Example #12
0
  public static void decode(
      CharsetDecoder charsetDecoder, ByteBuffer byteBuf, CharBuffer charByte) {
    try {
      CoderResult cr = charsetDecoder.decode(byteBuf, charByte, true);

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

      cr = charsetDecoder.flush(charByte);

      if (!cr.isUnderflow()) {
        cr.throwException();
      }
    } catch (CharacterCodingException x) {
      // Substitution is always enabled,
      // so this shouldn't happen
      throw new JSONException(x.getMessage(), x);
    }
  }
Example #13
0
 private void readWebsocket() {
   try {
     while (this.state == State.OPEN) {
       handleWebsocketFrame(WebSocketFrame.read(this.in));
     }
   } catch (CharacterCodingException e) {
     onException(e);
     doClose(CloseCode.InvalidFramePayloadData, e.toString(), false);
   } catch (IOException e) {
     onException(e);
     if (e instanceof WebSocketException) {
       doClose(((WebSocketException) e).getCode(), ((WebSocketException) e).getReason(), false);
     }
   } finally {
     doClose(
         CloseCode.InternalServerError,
         "Handler terminated without closing the connection.",
         false);
   }
 }
  /**
   * This method will send an close command, which does not return any rowsets, to the ODBC server.
   *
   * @retrun A CloseReply class representing the reply from the ODBC server is returned
   * @exception A SQLException is thrown
   */
  CloseReply Close() throws SQLException {

    try {
      getInputOutput().setTimeout(m_ic.t4props_.getNetworkTimeout());

      LogicalByteArray wbuffer =
          CloseMessage.marshal(m_dialogueId, m_stmtLabel, SQL_CLOSE, this.m_ic);

      LogicalByteArray rbuffer = getReadBuffer(TRANSPORT.SRVR_API_SQLFREESTMT, wbuffer);

      CloseReply cr = new CloseReply(rbuffer, m_ncsAddress.getIPorName(), m_ic);

      return cr;
    } // end try
    catch (SQLException se) {
      throw se;
    } catch (CharacterCodingException e) {
      SQLException se =
          HPT4Messages.createSQLException(
              m_ic.t4props_,
              m_locale,
              "translation_of_parameter_failed",
              "CloseMessage",
              e.getMessage());
      se.initCause(e);
      throw se;
    } catch (UnsupportedCharsetException e) {
      SQLException se =
          HPT4Messages.createSQLException(
              m_ic.t4props_, m_locale, "unsupported_encoding", e.getCharsetName());
      se.initCause(e);
      throw se;
    } catch (Exception e) {
      SQLException se =
          HPT4Messages.createSQLException(
              m_ic.t4props_, m_locale, "close_message_error", e.getMessage());

      se.initCause(e);
      throw se;
    } // end catch
  } // end Close
Example #15
0
 // we synchronize on ourselves, in case we are executed by several threads
 // from the thread pool.
 public synchronized void run() {
   // go over all complete messages and process them.
   while (_tokenizer.hasMessage()) {
     T msg = _tokenizer.nextMessage();
     String temp = _tokenizer.getLastMessageSend(); // TODO delete
     try {
       this._tokenizer.setConnectionHandler(_handler);
     } catch (Exception e) {
     }
     T response = this._protocol.processMessage(msg);
     if (response != null) {
       try {
         ByteBuffer bytes = _tokenizer.getBytesForMessage(response);
         this._handler.addOutData(bytes);
       } catch (CharacterCodingException e) {
         e.printStackTrace();
       }
     } else {
     }
   }
 }
Example #16
0
  /**
   * Convert the char array to byte array with respect to given charset.
   *
   * @param charArray
   * @param strCharset null or "" means default charset
   * @exception CharacterCodingException
   */
  public static byte[] convertCharArrayToByteArray(char[] charArray, String strCharset)
      throws CharacterCodingException {

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

    char[] cArray = (char[]) charArray.clone();
    CharBuffer charBuffer = CharBuffer.wrap(cArray);
    Charset charSet;
    if (strCharset == null || "".equals(strCharset)) {
      charSet = Charset.defaultCharset();
    } else if (Charset.isSupported(strCharset)) {
      charSet = Charset.forName(strCharset);
    } else {
      CharacterCodingException e = new CharacterCodingException();
      e.initCause(new UnsupportedCharsetException(strCharset));
      throw e;
    }

    CharsetEncoder encoder = charSet.newEncoder();
    ByteBuffer byteBuffer = null;
    try {
      byteBuffer = encoder.encode(charBuffer);
    } catch (CharacterCodingException cce) {
      throw cce;
    } catch (Throwable t) {
      CharacterCodingException e = new CharacterCodingException();
      e.initCause(t);
      throw e;
    }

    byte[] result = new byte[byteBuffer.remaining()];
    byteBuffer.get(result);
    clear(byteBuffer);
    clear(charBuffer);

    return result.clone();
  }
Example #17
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;
  }
Example #18
0
  /**
   * Finds any occurence of <code>what</code> in the backing buffer, starting as position <code>
   * start</code>. The starting position is measured in bytes and the return value is in terms of
   * byte position in the buffer. The backing buffer is not converted to a string for this
   * operation.
   *
   * @return byte position of the first occurence of the search string in the UTF-8 buffer or -1 if
   *     not found
   */
  public int find(String what, int start) {
    try {
      ByteBuffer src = ByteBuffer.wrap(this.bytes, 0, this.length);
      ByteBuffer tgt = encode(what);
      byte b = tgt.get();
      src.position(start);

      while (src.hasRemaining()) {
        if (b == src.get()) { // matching first byte
          src.mark(); // save position in loop
          tgt.mark(); // save position in target
          boolean found = true;
          int pos = src.position() - 1;
          while (tgt.hasRemaining()) {
            if (!src.hasRemaining()) { // src expired first
              tgt.reset();
              src.reset();
              found = false;
              break;
            }
            if (!(tgt.get() == src.get())) {
              tgt.reset();
              src.reset();
              found = false;
              break; // no match
            }
          }
          if (found) return pos;
        }
      }
      return -1; // not found
    } catch (CharacterCodingException e) {
      // can't get here
      e.printStackTrace();
      return -1;
    }
  }
  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;
  }
  /**
   * This method will send a fetch rowset command to the server.
   *
   * @param maxRowCnt the maximum rowset count to return
   * @param maxRowLen the maximum row length to return
   * @param sqlAsyncEnable a flag to enable/disable asynchronies execution
   * @param queryTimeout the number of seconds before the query times out
   * @retrun a FetchPerfReply class representing the reply from the ODBC server is returned
   * @exception A SQLException is thrown
   */
  FetchReply Fetch(
      int sqlAsyncEnable,
      int queryTimeout,
      int stmtHandle,
      int stmtCharset,
      int maxRowCnt,
      String cursorName,
      int cursorCharset,
      String stmtOptions)
      throws SQLException {

    try {
      getInputOutput().setTimeout(m_ic.t4props_.getNetworkTimeout());

      LogicalByteArray wbuffer =
          FetchMessage.marshal(
              m_dialogueId,
              sqlAsyncEnable,
              queryTimeout,
              stmtHandle,
              m_stmtLabel,
              stmtCharset,
              maxRowCnt,
              0 // infinite row size
              ,
              cursorName,
              cursorCharset,
              stmtOptions,
              this.m_ic);

      LogicalByteArray rbuffer = getReadBuffer(TRANSPORT.SRVR_API_SQLFETCH, wbuffer);

      //
      // Process output parameters
      //
      FetchReply frr = new FetchReply(rbuffer, m_ic);

      return frr;
    } // end try
    catch (SQLException se) {
      throw se;
    } catch (CharacterCodingException e) {
      SQLException se =
          HPT4Messages.createSQLException(
              m_ic.t4props_,
              m_locale,
              "translation_of_parameter_failed",
              "FetchMessage",
              e.getMessage());
      se.initCause(e);
      throw se;
    } catch (UnsupportedCharsetException e) {
      SQLException se =
          HPT4Messages.createSQLException(
              m_ic.t4props_, m_locale, "unsupported_encoding", e.getCharsetName());
      se.initCause(e);
      throw se;
    } catch (Exception e) {
      SQLException se =
          HPT4Messages.createSQLException(
              m_ic.t4props_, m_locale, "fetch_perf_message_error", e.getMessage());

      se.initCause(e);
      throw se;
    } // end catch
  } // end FetchPerf
Example #21
0
  /**
   * Updates the ScanSpec by setting the row interval to match this split
   *
   * @param scan_spec The base ScanSpec to start with
   * @return a new scan_spec object with a row interval matching this split
   */
  public ScanSpec createScanSpec(ScanSpec base_spec) {
    ScanSpec scan_spec = new ScanSpec(base_spec);

    RowInterval interval = new RowInterval();

    scan_spec.unsetRow_intervals();

    try {

      if (m_startrow != null && m_startrow.limit() > 0) {
        interval.setStart_row_binary(m_startrow);
        interval.setStart_inclusive(false);
        interval.setStart_inclusiveIsSet(true);
      }

      if (m_endrow != null && m_endrow.limit() > 0) {
        interval.setEnd_row_binary(m_endrow);
        interval.setEnd_inclusive(true);
        interval.setEnd_inclusiveIsSet(true);
      }

      ByteBuffer riStartRow;
      ByteBuffer riEndRow;
      Charset charset = Charset.forName("UTF-8");
      CharsetEncoder encoder = charset.newEncoder();

      if (base_spec.isSetRow_intervals()) {
        for (RowInterval ri : base_spec.getRow_intervals()) {
          riStartRow =
              (ri != null && ri.isSetStart_row())
                  ? encoder.encode(CharBuffer.wrap(ri.getStart_row()))
                  : null;
          riEndRow =
              (ri != null && ri.isSetEnd_row())
                  ? encoder.encode(CharBuffer.wrap(ri.getEnd_row()))
                  : null;
          if (riStartRow != null) {
            if (m_startrow == null
                || m_startrow.limit() == 0
                || riStartRow.compareTo(m_startrow) > 0) {
              interval.setStart_row_binary(riStartRow);
              interval.setStart_inclusive(ri.isStart_inclusive());
              interval.setStart_inclusiveIsSet(true);
            }
          }
          if (riEndRow != null) {
            if (m_endrow == null || m_endrow.limit() == 0 || riEndRow.compareTo(m_endrow) < 0) {
              interval.setEnd_row_binary(riEndRow);
              interval.setEnd_inclusive(ri.isEnd_inclusive());
              interval.setEnd_inclusiveIsSet(true);
            }
          }
          // Only allowing a single row interval
          break;
        }
      }

    } catch (CharacterCodingException e) {
      e.printStackTrace();
      System.exit(-1);
    }

    if (interval.isSetStart_row_binary() || interval.isSetEnd_row_binary()) {
      scan_spec.addToRow_intervals(interval);
      scan_spec.setRow_intervalsIsSet(true);
    }

    return scan_spec;
  }