@NotNull
  private static Pair<CharSequence, String> convertLineSeparators(@NotNull CharBuffer buffer) {
    int dst = 0;
    char prev = ' ';
    int crCount = 0;
    int lfCount = 0;
    int crlfCount = 0;

    final int length = buffer.length();
    final char[] bufferArray = CharArrayUtil.fromSequenceWithoutCopying(buffer);

    for (int src = 0; src < length; src++) {
      char c = bufferArray != null ? bufferArray[src] : buffer.charAt(src);
      switch (c) {
        case '\r':
          if (bufferArray != null) bufferArray[dst++] = '\n';
          else buffer.put(dst++, '\n');
          crCount++;
          break;
        case '\n':
          if (prev == '\r') {
            crCount--;
            crlfCount++;
          } else {
            if (bufferArray != null) bufferArray[dst++] = '\n';
            else buffer.put(dst++, '\n');
            lfCount++;
          }
          break;
        default:
          if (bufferArray != null) bufferArray[dst++] = c;
          else buffer.put(dst++, c);
          break;
      }
      prev = c;
    }

    String detectedLineSeparator = null;
    if (crlfCount > crCount && crlfCount > lfCount) {
      detectedLineSeparator = "\r\n";
    } else if (crCount > lfCount) {
      detectedLineSeparator = "\r";
    } else if (lfCount > 0) {
      detectedLineSeparator = "\n";
    }

    CharSequence result;
    if (buffer.length() == dst) {
      result = buffer;
    } else {
      // in Mac JDK CharBuffer.subSequence() signature differs from Oracle's
      // more than that, the signature has changed between jd6 and jdk7,
      // so use more generic CharSequence.subSequence() just in case
      @SuppressWarnings("UnnecessaryLocalVariable")
      CharSequence seq = buffer;
      result = seq.subSequence(0, dst);
    }
    return Pair.create(result, detectedLineSeparator);
  }
Beispiel #2
0
    private void addCommand(String s) {
      CharBuffer buffer = CharBuffer.allocate(s.length() + 1);
      buffer.put(s);
      buffer.put('\n');
      buffer.flip();

      _command.compact();
      _encoder.encode(buffer, _command, true);
      _command.flip();
    }
  public void char2byte(char[] src, int off, int len, byte[] dst, int off2) {

    if (len > (BUFFER_SIZE / 2)) {
      CharBuffer buffer = ByteBuffer.wrap(dst, off2, len * 2).order(order).asCharBuffer();
      buffer.put(src, off, len);
    } else {
      charBuffer.clear();
      charBuffer.put(src, off, len);

      byteBuffer.position(0).limit(len * 2);
      byteBuffer.get(dst, off2, len * 2);
    }
  }
 /*     */ protected CoderResult decodeLoop(
     ByteBuffer paramByteBuffer, CharBuffer paramCharBuffer)
       /*     */ {
   /*  65 */ if (paramByteBuffer.remaining() < 4) /*  66 */ return CoderResult.UNDERFLOW;
   /*  67 */ int i = paramByteBuffer.position();
   /*     */ try
   /*     */ {
     /*     */ int j;
     /*  70 */ if (this.currentBO == 0) {
       /*  71 */ j =
           (paramByteBuffer.get() & 0xFF) << 24
               | (paramByteBuffer.get() & 0xFF) << 16
               | (paramByteBuffer.get() & 0xFF) << 8
               | paramByteBuffer.get() & 0xFF;
       /*     */
       /*  75 */ if ((j == 65279) && (this.expectedBO != 2)) {
         /*  76 */ this.currentBO = 1;
         /*  77 */ i += 4;
         /*  78 */ } else if ((j == -131072) && (this.expectedBO != 1)) {
         /*  79 */ this.currentBO = 2;
         /*  80 */ i += 4;
         /*     */ } else {
         /*  82 */ if (this.expectedBO == 0) /*  83 */ this.currentBO = 1;
         /*     */ else /*  85 */ this.currentBO = this.expectedBO;
         /*  86 */ paramByteBuffer.position(i);
         /*     */ }
       /*     */ }
     /*     */ CoderResult localCoderResult;
     /*  89 */ while (paramByteBuffer.remaining() >= 4) {
       /*  90 */ j = getCP(paramByteBuffer);
       /*  91 */ if (Character.isBmpCodePoint(j)) {
         /*  92 */ if (!paramCharBuffer.hasRemaining()) /*  93 */ return CoderResult.OVERFLOW;
         /*  94 */ i += 4;
         /*  95 */ paramCharBuffer.put((char) j);
         /*  96 */ } else if (Character.isValidCodePoint(j)) {
         /*  97 */ if (paramCharBuffer.remaining() < 2) /*  98 */ return CoderResult.OVERFLOW;
         /*  99 */ i += 4;
         /* 100 */ paramCharBuffer.put(Character.highSurrogate(j));
         /* 101 */ paramCharBuffer.put(Character.lowSurrogate(j));
         /*     */ } else {
         /* 103 */ return CoderResult.malformedForLength(4);
         /*     */ }
       /*     */ }
     /* 106 */ return CoderResult.UNDERFLOW;
     /*     */ } finally {
     /* 108 */ paramByteBuffer.position(i);
     /*     */ }
   /*     */ }
Beispiel #5
0
 @Override
 public void add() {
   boolean done = false;
   int pos = size - 1;
   while (pos >= 0 && !done) {
     char c = buffer.get(pos);
     if (c == '9') {
       buffer.put(pos, '0');
       pos--;
     } else {
       c++;
       buffer.put(pos, c);
       done = true;
     }
   }
 }
Beispiel #6
0
 @Override
 public void zero() {
   buffer.rewind();
   for (int x = 0; x < size; x++) {
     buffer.put('0');
   }
 }
  protected CoderResult decodeLoop(ByteBuffer in, CharBuffer out) {

    while (true) {

      if (in.remaining() < 1) return CoderResult.UNDERFLOW;

      in.mark();
      byte b = in.get();
      char c = (char) (b & 0x7f);

      if (CodingErrorAction.REPORT == unmappableCharacterAction()
          && !PrintableCharset.isPrintableChar(c)) {
        /*
        "bug" fix for 359010
        return CoderResult.unmappableForLength(1);
        */
        continue;
      }

      if (out.remaining() < 1) {
        in.reset();
        return CoderResult.OVERFLOW;
      }

      out.put(c);
    }
  }
 /**
  * Send as many items as possible from this buffer to the given byte buffer.
  *
  * <p>The <TT>sendItems()</TT> method must not block the calling thread; if it does, all message
  * I/O in MP will be blocked.
  *
  * @param i Index of first item to send, in the range 0 .. <TT>length</TT>-1.
  * @param buffer Byte buffer.
  * @return Number of items sent.
  */
 protected int sendItems(int i, ByteBuffer buffer) {
   CharBuffer charbuffer = buffer.asCharBuffer();
   int n = 0;
   int r = i2r(i);
   int row = r * myRowStride + myLowerRow;
   int c = i2c(i);
   int col = c * myColStride + myLowerCol;
   int ncols = Math.min(myColCount - c, charbuffer.remaining());
   while (r < myRowCount && ncols > 0) {
     char[] myMatrix_row = myMatrix[row];
     while (c < ncols) {
       charbuffer.put(myMatrix_row[col]);
       ++c;
       col += myColStride;
     }
     n += ncols;
     ++r;
     row += myRowStride;
     c = 0;
     col = myLowerCol;
     ncols = Math.min(myColCount, charbuffer.remaining());
   }
   buffer.position(buffer.position() + 2 * n);
   return n;
 }
Beispiel #9
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;
  }
Beispiel #10
0
  /**
   * Write a string to the network. The length header of the string is written automatically and its
   * encoded to the correct CharSet automatically.
   *
   * @param value the string that shall be send to the server
   */
  @Override
  public void writeString(@Nonnull String value) throws CharacterCodingException {
    int startIndex = buffer.position();
    buffer.putShort((short) 0);

    encodingBuffer.clear();
    encodingBuffer.put(value, 0, Math.min(encodingBuffer.capacity(), value.length()));
    encodingBuffer.flip();

    do {
      CoderResult encodingResult = encoder.encode(encodingBuffer, buffer, true);
      if (!encodingResult.isError()) {
        break;
      }
      if (encodingResult.isUnmappable()) {
        log.warn(
            "Found a character that failed to encode for the transfer to the server: {} - SKIP",
            encodingBuffer.get());
      } else {
        encodingResult.throwException();
      }
    } while (encodingBuffer.hasRemaining());

    int lastIndex = buffer.position();
    buffer.position(startIndex);
    writeUShort(lastIndex - startIndex - 2);
    buffer.position(lastIndex);
  }
Beispiel #11
0
 static byte[] encode(char[] cc, Charset cs, boolean testDirect, Time t) throws Exception {
   ByteBuffer bbf;
   CharBuffer cbf;
   CharsetEncoder enc = cs.newEncoder();
   String csn = cs.name();
   if (testDirect) {
     bbf = ByteBuffer.allocateDirect(cc.length * 4);
     cbf = ByteBuffer.allocateDirect(cc.length * 2).asCharBuffer();
     cbf.put(cc).flip();
   } else {
     bbf = ByteBuffer.allocate(cc.length * 4);
     cbf = CharBuffer.wrap(cc);
   }
   CoderResult cr = null;
   long t1 = System.nanoTime() / 1000;
   for (int i = 0; i < iteration; i++) {
     cbf.rewind();
     bbf.clear();
     enc.reset();
     cr = enc.encode(cbf, bbf, true);
   }
   long t2 = System.nanoTime() / 1000;
   t.t = (t2 - t1) / iteration;
   if (cr != CoderResult.UNDERFLOW) {
     System.out.println("ENC-----------------");
     int pos = cbf.position();
     System.out.printf("  cr=%s, cbf.pos=%d, cc[pos]=%x%n", cr.toString(), pos, cc[pos] & 0xffff);
     throw new RuntimeException("Encoding err: " + csn);
   }
   byte[] bb = new byte[bbf.position()];
   bbf.flip();
   bbf.get(bb);
   return bb;
 }
Beispiel #12
0
  public void init(float vertices[], float normals[], float texcoords[], char indices[]) {
    int nbVertex = vertices.length / 3;
    mVertices = FloatBuffer.allocate(nbVertex * D3DMesh.nbFloatPerVertex);

    for (int i = 0; i < nbVertex; i++) {
      float px = vertices[i * 3];
      float py = vertices[i * 3 + 1];
      float pz = vertices[i * 3 + 2];
      float nx = 0f;
      float ny = 0f;
      float nz = 0f;
      if (normals != null) {
        nx = normals[i * 3];
        ny = normals[i * 3 + 1];
        nz = normals[i * 3 + 2];
      }
      float tx = 0f;
      float ty = 0f;
      if (texcoords != null) {
        tx = texcoords[i * 2];
        ty = texcoords[i * 2 + 1];
      }

      this.addPoint(px, py, pz, nx, ny, nz, tx, ty);
    }

    mVertices.position(0);

    mIndices = CharBuffer.allocate(indices.length);
    mIndices.put(indices);
    mIndices.position(0);
  }
Beispiel #13
0
 /**
  * Attempts to read characters into the specified character buffer. The buffer is used as a
  * repository of characters as-is: the only changes made are the results of a put operation. No
  * flipping or rewinding of the buffer is performed.
  *
  * @param target the buffer to read characters into
  * @return The number of characters added to the buffer, or -1 if this source of characters is at
  *     its end
  * @throws IOException if an I/O error occurs
  * @throws NullPointerException if target is null
  * @throws java.nio.ReadOnlyBufferException if target is a read only buffer
  * @since 1.5
  */
 public int read(java.nio.CharBuffer target) throws IOException {
   int len = target.remaining();
   char[] cbuf = new char[len];
   int n = read(cbuf, 0, len);
   if (n > 0) target.put(cbuf, 0, n);
   return n;
 }
Beispiel #14
0
 @Override
 public boolean onStdinCharsReady(CharBuffer buffer) {
   if (utf8Buffer.remaining() <= buffer.remaining()) {
     charsWritten += utf8Buffer.remaining();
     buffer.put(utf8Buffer);
     buffer.flip();
     return false;
   } else {
     charsWritten += buffer.remaining();
     buffer.put(utf8Buffer.subSequence(0, buffer.remaining()));
     buffer.flip();
     utf8Buffer.position(utf8Buffer.position() + buffer.remaining());
     stdinOverflow = true;
     return true;
   }
 }
 protected PlaceNameChunk createPlaceNameChunk(PlaceNameService service) {
   int numChars = this.textArray.length();
   CharBuffer textBuffer = newCharBuffer(numChars);
   textBuffer.put(this.textArray.toString());
   textBuffer.rewind();
   return new PlaceNameChunk(
       service, textBuffer, this.textIndexArray, this.latlonArray, this.numEntries);
 }
Beispiel #16
0
 // 将字符转为字节(编码)
 public static byte[] getBytes(char[] chars) {
   Charset cs = Charset.forName("UTF-8");
   CharBuffer cb = CharBuffer.allocate(chars.length);
   cb.put(chars);
   cb.flip();
   ByteBuffer bb = cs.encode(cb);
   return bb.array();
 }
Beispiel #17
0
 private void ensureCharBuffRemaining(final int size) {
   if (charBuff.remaining() < size) {
     final int cpcty = (charBuff.capacity() + size) * 2;
     final java.nio.CharBuffer newChBuf = java.nio.CharBuffer.allocate(cpcty);
     newChBuf.put(charBuff);
     charBuff = newChBuf;
   }
 }
 private static boolean fillBuffer(CharBuffer buffer) {
   if (index >= strings.length) {
     return false;
   }
   String string = strings[index++];
   for (int i = 0; i < string.length(); i++) {
     buffer.put(string.charAt(i));
   }
   return true;
 }
Beispiel #19
0
 protected CoderResult decodeLoop(ByteBuffer src, CharBuffer dst) {
   if (src.remaining() < 4) return CoderResult.UNDERFLOW;
   int mark = src.position();
   int cp;
   try {
     if (currentBO == NONE) {
       cp =
           ((src.get() & 0xff) << 24)
               | ((src.get() & 0xff) << 16)
               | ((src.get() & 0xff) << 8)
               | (src.get() & 0xff);
       if (cp == BOM_BIG && expectedBO != LITTLE) {
         currentBO = BIG;
         mark += 4;
       } else if (cp == BOM_LITTLE && expectedBO != BIG) {
         currentBO = LITTLE;
         mark += 4;
       } else {
         if (expectedBO == NONE) currentBO = BIG;
         else currentBO = expectedBO;
         src.position(mark);
       }
     }
     while (src.remaining() >= 4) {
       cp = getCP(src);
       if (Character.isBmpCodePoint(cp)) {
         if (!dst.hasRemaining()) return CoderResult.OVERFLOW;
         mark += 4;
         dst.put((char) cp);
       } else if (Character.isValidCodePoint(cp)) {
         if (dst.remaining() < 2) return CoderResult.OVERFLOW;
         mark += 4;
         dst.put(Character.highSurrogate(cp));
         dst.put(Character.lowSurrogate(cp));
       } else {
         return CoderResult.malformedForLength(4);
       }
     }
     return CoderResult.UNDERFLOW;
   } finally {
     src.position(mark);
   }
 }
 @Override
 public int read(CharBuffer target) throws IOException {
   int len = target.remaining();
   char[] cbuf = new char[len];
   int n = read(cbuf, 0, len);
   if (n > 0) {
     target.put(cbuf, 0, n);
   }
   return n;
 }
Beispiel #21
0
 private void emit(int c) throws IOException {
   assert c >= 0;
   CharBuffer buf = lineBuffer;
   if (buf.remaining() == 0) {
     if (buf.capacity() == BUFFER_LIMIT) {
       throw new IOException(
           MessageFormat.format(
               "Line is too large (near {0}:{1}, size={2}, record-number={3})",
               path, currentPhysicalHeadLine, BUFFER_LIMIT, currentRecordNumber));
     }
     CharBuffer newBuf = CharBuffer.allocate(Math.min(buf.capacity() * 2, BUFFER_LIMIT));
     newBuf.clear();
     buf.flip();
     newBuf.put(buf);
     buf = newBuf;
     lineBuffer = newBuf;
   }
   buf.put((char) c);
 }
Beispiel #22
0
  public void putTriangle(
      float x1, float y1, float z1, float x2, float y2, float z2, float x3, float y3, float z3) {
    // System.out.println("A= "+x1+" "+y1+" "+z1);
    // System.out.println("B= "+x2+" "+y2+" "+z2);
    // System.out.println("C= "+x3+" "+y3+" "+z3);

    int indice = mVertices.position() / D3DMesh.nbFloatPerVertex;
    // int indice = mVertices.position();
    mVertices.put(x1);
    mVertices.put(y1);
    mVertices.put(z1);

    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);

    mVertices.put(x2);
    mVertices.put(y2);
    mVertices.put(z2);

    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);

    mVertices.put(x3);
    mVertices.put(y3);
    mVertices.put(z3);

    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);
    mVertices.put(0f);

    mIndices.put((char) indice);
    mIndices.put((char) (indice + 1));
    mIndices.put((char) (indice + 2));
  }
  private ByteBuffer charSequenceToBytes(final CharSequence word) throws UnmappableInputException {
    // Encode word characters into bytes in the same encoding as the FSA's.
    charBuffer = BufferUtils.clearAndEnsureCapacity(charBuffer, word.length());
    for (int i = 0; i < word.length(); i++) {
      final char chr = word.charAt(i);
      charBuffer.put(chr);
    }
    charBuffer.flip();

    return BufferUtils.charsToBytes(encoder, charBuffer, byteBuffer);
  }
 public static void main(String[] args) throws Exception {
   // 创建简体中文对应的Charset
   Charset cn = Charset.forName("GBK");
   // 获取cn对象对应的编码器和解码器
   CharsetEncoder cnEncoder = cn.newEncoder();
   CharsetDecoder cnDecoder = cn.newDecoder();
   // 创建一个CharBuffer对象
   CharBuffer cbuff = CharBuffer.allocate(8);
   cbuff.put('孙');
   cbuff.put('悟');
   cbuff.put('空');
   cbuff.flip();
   // 将CharBuffer中的字符序列转换成字节序列
   ByteBuffer bbuff = cnEncoder.encode(cbuff);
   // 循环访问ByteBuffer中的每个字节
   for (int i = 0; i < bbuff.capacity(); i++) {
     System.out.print(bbuff.get(i) + " ");
   }
   // 将ByteBuffer的数据解码成字符序列
   System.out.println("\n" + cnDecoder.decode(bbuff));
 }
  private String generateRandomStringData(int dataSize) {
    /*
     * Generate a random string of "words" using random single and multi-byte characters that are
     * separated by punctuation marks and whitespace.
     */
    String punctuationChars = "!,.;?";
    int wordLength = maxWordLength;

    CharBuffer data = CharBuffer.allocate(dataSize);
    while (data.remaining() > 0) {
      String word;
      if (limitWordCount) {
        word = pickRandomWord(wordLength);
      } else {
        word = generateRandomUniqueWord(wordLength, true);
      }
      data = data.put(word);
      countOfWordsInData++;
      if (wordCount.containsKey(word)) {
        wordCount.put(word, wordCount.get(word) + 1);
      } else {
        wordCount.put(word, 1);
      }

      if (data.remaining() >= 2 && random.nextInt() % newlinePunctuationModulo == 0) {
        data.put(punctuationChars.charAt(random.nextInt(punctuationChars.length() - 1)));
        data.put('\n');
      } else {
        if (data.remaining() >= 1) {
          data.put(' ');
        }
      }

      if (data.remaining() < wordLength) {
        wordLength = data.remaining();
      }
    }

    return data.rewind().toString();
  }
Beispiel #26
0
 private static CharBuffer allocate(CharBuffer buffer, int length) {
   if (buffer == null) {
     length = Math.max(length * 3 / 2, DEFAULT_BUFFER_CAPACITY);
     return CharBuffer.allocate(length);
   }
   if (buffer.remaining() < length) {
     CharBuffer expandedBuffer = CharBuffer.allocate((buffer.position() + length) * 3 / 2);
     buffer.flip();
     expandedBuffer.put(buffer);
     return expandedBuffer;
   }
   return buffer;
 }
Beispiel #27
0
 @Override
 public synchronized int read(CharBuffer target) throws IOException {
   checkNotNull(target);
   checkOpen();
   if (!hasRemaining()) {
     return -1;
   }
   int charsToRead = Math.min(target.remaining(), remaining());
   for (int i = 0; i < charsToRead; i++) {
     target.put(seq.charAt(pos++));
   }
   return charsToRead;
 }
  public LinkedList<CharBuffer> permuteTerms(char[] term, int offset, int end) {
    LinkedList<CharBuffer> buffers = new LinkedList<CharBuffer>();

    int i = offset;
    for (; i < end; i++) {
      if (term[i] == tokenDelimiter) {
        int k = offset;
        for (; k < i; k++) {
          if (term[k] == pipe) {
            break;
          }
        }
        LinkedList<CharBuffer> nextBuffers = permuteTerms(term, i + 1, end);
        Iterator iter = nextBuffers.iterator();
        while (iter.hasNext()) {
          CharBuffer formBuffer = CharBuffer.allocate(end - offset);
          formBuffer.put((CharBuffer) iter.next());
          /*if (k<i) {
              formBuffer.put(term, k+1, i-k);
              //formBuffer.put(term, offset, k-offset);
          }
          else {
              formBuffer.put(term, offset, i-offset);
              //formBuffer.put(term, offset, i-offset);
          }*/
          // formBuffer.put(' ');
          buffers.add(formBuffer);
        }

        return buffers;
      }
    }

    // i = length
    CharBuffer formBuffer = CharBuffer.allocate(2);
    formBuffer.put("hello", 0, 2);
    buffers.add(formBuffer);
    return buffers;
  }
Beispiel #29
0
  public void copy(D3DMesh mesh) {
    mIndices = CharBuffer.allocate(mesh.mIndices.position());
    mVertices = FloatBuffer.allocate(mesh.mVertices.position());

    for (int j = 0; j < mIndices.capacity(); j++) mIndices.put(mesh.mIndices.get(j));

    for (int j = 0; j < mVertices.capacity(); j++) {
      mVertices.put(mesh.mVertices.get(j));
    }

    mIndices.position(0);
    mVertices.position(0);
  }
  private void fillCharBuf() throws IOException {
    int limit = 0;
    int c = 0;
    do {
      c = _delegate.read();
      if (c >= 0) {
        _charBuf.put((char) c);
        limit++;
      }
    } while ((c >= 0xD800) && (c <= 0xDBFF));

    _charBuf.position(0);
    _charBuf.limit(limit);
  }