Пример #1
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);
  }
Пример #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);
   }
 }
Пример #3
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 "";
    }
Пример #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();
     }
   }
 }
  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;
  }
Пример #7
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 "";
    }
Пример #8
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 {
     }
   }
 }
Пример #9
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;
    }
  }
Пример #10
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;
  }