Exemplo n.º 1
0
  public static void main(String args[]) throws Exception {
    ByteBuffer buffer = ByteBuffer.allocate(10);

    for (int i = 0; i < buffer.capacity(); ++i) {
      buffer.put((byte) i);
    }

    buffer.position(3);
    buffer.limit(7);

    ByteBuffer slice = buffer.slice();

    for (int i = 0; i < slice.capacity(); ++i) {
      byte b = slice.get(i);
      b *= 11;
      slice.put(i, b);
    }

    buffer.position(0);
    buffer.limit(buffer.capacity());

    while (buffer.hasRemaining()) {
      System.out.println(buffer.get());
    }
  }
Exemplo n.º 2
0
  public String asHexToken(Key key) {
    try {
      Cipher encodingCipher = Cipher.getInstance("AES");
      encodingCipher.init(Cipher.ENCRYPT_MODE, key);

      ByteBuffer buffer = ByteBuffer.allocate(16 + 1 + 8 + 8 + getBufferSize());
      buffer.position(16);
      buffer.put(getId());
      buffer.putLong(getExpires().getTimeInMillis());
      buffer.putLong(getUoid());
      getBytes(buffer);
      if (buffer.position() != buffer.capacity()) {
        throw new RuntimeException(
            "Buffer's position should be at the end "
                + buffer.position()
                + "/"
                + buffer.capacity());
      }
      MessageDigest messageDigest = MessageDigest.getInstance("MD5");
      buffer.position(16);
      messageDigest.update(buffer);
      buffer.position(0);
      buffer.put(messageDigest.digest());

      byte[] encodedBytes = encodingCipher.doFinal(buffer.array());
      String encodedHexString = new String(Hex.encodeHex(encodedBytes));
      return encodedHexString;
    } catch (Exception e) {
      LOGGER.error("", e);
    }
    return null;
  }
Exemplo n.º 3
0
  /** Method <code>resizeApplicationBuffer</code> is used to perform */
  private ByteBuffer resizeApplicationBuffer(ByteBuffer net, ByteBuffer app) {

    //  if (appBuffSize > app.remaining()) {
    //    if (net.remaining() > app.remaining()) {
    //    if (appBuffSize > app.capacity() - app.remaining()) {
    //      if (log.isLoggable(Level.FINE)) {
    //        log.fine("Resizing tlsInput to " + (appBuffSize + app.capacity()) + " bytes.");
    //      }
    //
    //      ByteBuffer bb = ByteBuffer.allocate(app.capacity() + appBuffSize);
    if (log.isLoggable(Level.FINE)) {
      log.log(
          Level.FINE,
          "Resizing tlsInput to {0} bytes, {1}",
          new Object[] {(2048 + app.capacity()), debugId});
    }

    ByteBuffer bb = ByteBuffer.allocate(app.capacity() + 2048);

    // bb.clear();
    bb.order(app.order());
    app.flip();
    bb.put(app);

    return bb;

    //  } else {
    //
    //    return app;
    //  }    // end of else
  }
Exemplo n.º 4
0
  private CommandArgs<K, V> write(byte[] arg) {
    buffer.mark();

    if (buffer.remaining() < arg.length) {
      int estimate = buffer.remaining() + arg.length + 10;
      realloc(max(buffer.capacity() * 2, estimate));
    }

    while (true) {
      try {
        buffer.put((byte) '$');
        write(arg.length);
        buffer.put(CRLF);
        buffer.put(arg);
        buffer.put(CRLF);
        break;
      } catch (BufferOverflowException e) {
        buffer.reset();
        realloc(buffer.capacity() * 2);
      }
    }

    count++;
    return this;
  }
Exemplo n.º 5
0
  private CommandArgs<K, V> write(String arg) {
    int length = arg.length();

    buffer.mark();

    if (buffer.remaining() < length) {
      int estimate = buffer.remaining() + length + 10;
      realloc(max(buffer.capacity() * 2, estimate));
    }

    while (true) {
      try {
        buffer.put((byte) '$');
        write(length);
        buffer.put(CRLF);
        for (int i = 0; i < length; i++) {
          buffer.put((byte) arg.charAt(i));
        }
        buffer.put(CRLF);
        break;
      } catch (BufferOverflowException e) {
        buffer.reset();
        realloc(buffer.capacity() * 2);
      }
    }

    count++;
    return this;
  }
Exemplo n.º 6
0
 TileIndex(final File tileIndexFile) {
   try {
     this.tileIndexFile = tileIndexFile;
     final InputStream is = IOUtil.maybeBufferInputStream(new FileInputStream(tileIndexFile));
     final ByteBuffer buf = ByteBuffer.allocate(8);
     buf.order(ByteOrder.LITTLE_ENDIAN);
     int absoluteRecordIndex = 0;
     int numTiles = 0;
     while (readTileIndexRecord(buf.array(), buf.capacity(), is)) {
       buf.rewind();
       buf.limit(buf.capacity());
       final int tile = buf.getInt();
       // Note: not handling unsigned ints > 2^31, but could if one of these exceptions is thrown.
       if (tile < 0)
         throw new PicardException("Tile number too large in " + tileIndexFile.getAbsolutePath());
       final int numClusters = buf.getInt();
       if (numClusters < 0)
         throw new PicardException("Cluster size too large in " + tileIndexFile.getAbsolutePath());
       tiles.add(new TileIndexRecord(tile, numClusters, absoluteRecordIndex, numTiles++));
       absoluteRecordIndex += numClusters;
     }
     CloserUtil.close(is);
   } catch (final IOException e) {
     throw new PicardException("Problem reading " + tileIndexFile.getAbsolutePath(), e);
   }
 }
  @Override
  public void read(int len) {
    if (readByteBuffer.position() != readByteBuffer.capacity()) {
      logger.error(
          "Read buffer position {} != capacity {}",
          readByteBuffer.position(),
          readByteBuffer.capacity());
      eventLoop.disconnect(this);
      return;
    }

    readByteBuffer.flip();
    long timestamp = readByteBuffer.getLong();
    if (timestamp < -2) {
      logger.error("Received bad timestamp {}", timestamp);
      eventLoop.disconnect(this);
      return;
    } else if (timestamp != this.timestamp) {
      logger.error("Received bad timestamp {}. Sent timestamp {}", timestamp, this.timestamp);
      eventLoop.disconnect(this);
      return;
    } else if (timestamp > 0) {
      benchmarkResults.addResult(System.nanoTime() - timestamp);
    }
    readByteBuffer.clear();

    send();
  }
    private void free(ByteBuffer oldBuf) {
      if ((oldBuf == null)
          || ((maxCachedBufferSize != 0) && (oldBuf.capacity() > maxCachedBufferSize))
          || oldBuf.isReadOnly()
          || isDerived()
          || (Thread.currentThread() != ownerThread)) {
        return;
      }

      // Add to the cache.
      Queue<CachedBuffer> pool;

      if (oldBuf.isDirect()) {
        pool = directBuffers.get().get(oldBuf.capacity());
      } else {
        pool = heapBuffers.get().get(oldBuf.capacity());
      }

      if (pool == null) {
        return;
      }

      // Restrict the size of the pool to prevent OOM.
      if ((maxPoolSize == 0) || (pool.size() < maxPoolSize)) {
        pool.offer(new CachedBuffer(oldBuf));
      }
    }
  private int bufferCopiedCallback(long nCopiedSize, long nAvailableSize) {
    if (!super.mValid) {
      Log.e(TAG, "Invalid state");
      return -1;
    }
    if (mPreview == null || !mPreview.isReady()) {
      // Not on the top
      return 0;
    }

    if (mVideoFrame == null || mVideoFrame.capacity() != nAvailableSize) {
      synchronized (mPreview) {
        long newWidth = mConsumer.getDisplayWidth();
        long newHeight = mConsumer.getDisplayHeight();

        mVideoFrame = ByteBuffer.allocateDirect((int) nAvailableSize);
        mConsumer.setConsumeBuffer(mVideoFrame, mVideoFrame.capacity());

        mWidth = (int) newWidth;
        mHeight = (int) newHeight;
        mPreview.setBuffer(mVideoFrame, mWidth, mHeight);
      }
      return 0;
    }

    mPreview.requestRender();

    return 0;
  }
  /** Read at least the specified amount of bytes, and place them in the input buffer. */
  protected boolean readt(int n, boolean useAvailableData) throws IOException {

    if (useAvailableData && inputBuffer.remaining() == 0) {
      return false;
    }
    if (inputBuffer.capacity() - inputBuffer.limit() <= n - inputBuffer.remaining()) {
      inputBuffer.compact();
      inputBuffer.limit(inputBuffer.position());
      inputBuffer.position(0);
    }
    int nRead;
    while (inputBuffer.remaining() < n) {
      nRead =
          Socket.recvbb(
              getSocketWrapper().getSocket().longValue(),
              inputBuffer.limit(),
              inputBuffer.capacity() - inputBuffer.limit());
      if (nRead > 0) {
        inputBuffer.limit(inputBuffer.limit() + nRead);
      } else {
        if ((-nRead) == Status.getEtimedout() || (-nRead) == Status.getTimeup()) {
          return false;
        } else {
          throw new IOException(getSm().getString("ajpprocessor.failedread"));
        }
      }
    }

    return true;
  }
  private UpfrontAllocatingPageSource(
      BufferSource source, long max, int maxChunk, int minChunk, boolean fixed) {
    Long totalPhysical = PhysicalMemory.totalPhysicalMemory();
    Long freePhysical = PhysicalMemory.freePhysicalMemory();
    if (totalPhysical != null && max > totalPhysical) {
      throw new IllegalArgumentException(
          "Attempting to allocate "
              + DebuggingUtils.toBase2SuffixedString(max)
              + "B of memory "
              + "when the host only contains "
              + DebuggingUtils.toBase2SuffixedString(totalPhysical)
              + "B of physical memory");
    }
    if (freePhysical != null && max > freePhysical) {
      LOGGER.warn(
          "Attempting to allocate {}B of offheap when there is only {}B of free physical memory - some paging will therefore occur.",
          DebuggingUtils.toBase2SuffixedString(max),
          DebuggingUtils.toBase2SuffixedString(freePhysical));
    }

    LOGGER.info("Allocating {}B in chunks", DebuggingUtils.toBase2SuffixedString(max));

    for (ByteBuffer buffer : allocateBackingBuffers(source, max, maxChunk, minChunk, fixed)) {
      sliceAllocators.add(new PowerOfTwoAllocator(buffer.capacity()));
      victimAllocators.add(new PowerOfTwoAllocator(buffer.capacity()));
      victims.add(new TreeSet<Page>(REGION_COMPARATOR));
      buffers.add(buffer);
    }
  }
Exemplo n.º 12
0
    @Override
    protected Action process() throws Exception {
      // Only return if EOF has previously been read and thus
      // a write done with EOF=true
      if (_eof) {
        if (LOG.isDebugEnabled()) LOG.debug("EOF of {}", this);
        // Handle EOF
        _in.close();
        closed();
        _channel.getByteBufferPool().release(_buffer);
        return Action.SUCCEEDED;
      }

      // Read until buffer full or EOF
      int len = 0;
      while (len < _buffer.capacity() && !_eof) {
        int r = _in.read(_buffer.array(), _buffer.arrayOffset() + len, _buffer.capacity() - len);
        if (r < 0) _eof = true;
        else len += r;
      }

      // write what we have
      _buffer.position(0);
      _buffer.limit(len);
      write(_buffer, _eof, this);
      return Action.SCHEDULED;
    }
Exemplo n.º 13
0
  @Override
  public void append(Framedata nextframe) throws InvalidFrameException {
    ByteBuffer b = nextframe.getPayloadData();
    if (unmaskedpayload == null) {
      unmaskedpayload = ByteBuffer.allocate(b.remaining());
      b.mark();
      unmaskedpayload.put(b);
      b.reset();
    } else {
      b.mark();
      unmaskedpayload.position(unmaskedpayload.limit());
      unmaskedpayload.limit(unmaskedpayload.capacity());

      if (b.remaining() > unmaskedpayload.remaining()) {
        ByteBuffer tmp = ByteBuffer.allocate(b.remaining() + unmaskedpayload.capacity());
        unmaskedpayload.flip();
        tmp.put(unmaskedpayload);
        tmp.put(b);
        unmaskedpayload = tmp;

      } else {
        unmaskedpayload.put(b);
      }
      unmaskedpayload.rewind();
      b.reset();
    }
    fin = nextframe.isFin();
  }
Exemplo n.º 14
0
    private void writeToTexture(RectanglePacker.Rectangle r, ByteBuffer data) {
      assert r.width * r.height * format.bytes == data.capacity()
          : r + " * " + format.bytes + " != " + data.capacity();

      GL11.glBindTexture(GL11.GL_TEXTURE_2D, id);

      GL11.glPixelStorei(GL11.GL_UNPACK_ALIGNMENT, format.bytes);

      GL11.glTexSubImage2D(
          GL11.GL_TEXTURE_2D,
          0,
          r.x,
          r.y,
          r.width,
          r.height,
          format.glFormat,
          GL11.GL_UNSIGNED_BYTE,
          data);

      GLUtil.checkGLError();

      if (mipmap) {
        mipmapDirty = true;
      }
    }
Exemplo n.º 15
0
  protected void shrinkCapacity() {
    boolean shrink = false;
    int aver = (int) Math.ceil(average);
    int skrinkSize = aver;

    if (_buffer.capacity() > AUTO_SHRINK_SIZE) {
      shrink = true;
      if (Runtime.getRuntime().freeMemory() < MAX_BUFFER_CAPACITY) {
        skrinkSize = INITIAL_BUFFER_CAPACITY;
      } else {
        skrinkSize = (int) aver * 2 > AUTO_SHRINK_SIZE ? AUTO_SHRINK_SIZE : (int) aver * 2;
      }
    } else if (_buffer.capacity() <= AUTO_SHRINK_SIZE) {

      if (_buffer.capacity() > aver * 4) {
        shrink = true;
        skrinkSize = (int) aver * 2;
      }
    }

    if (shrink && skrinkSize < MAX_BUFFER_CAPACITY && skrinkSize > _have) {
      // System.out.println("hashcode="+this.hashCode()+",capacity="+_buffer.capacity()+",shrink="+shrinkSize+",average="+aver);
      ByteBuffer newbuf = ByteBuffer.allocate(skrinkSize);
      newbuf.put((ByteBuffer) _buffer.flip());
      _buffer = newbuf;
    }
  }
Exemplo n.º 16
0
 /** Borrow a buffer from the pool. If none in the pool, create a new one. */
 public ByteBuffer borrowBuffer() {
   ByteBuffer buffer = pool.poll();
   if (buffer == null) {
     buffer = ByteBuffer.allocateDirect(bufferSize + guard.length);
     initializeGuard(buffer);
     if (logger.isDetailEnabled())
       logger.detail(
           "FixedByteBufferPoolImpl for "
               + name
               + " got new  buffer: position "
               + buffer.position()
               + " capacity "
               + buffer.capacity()
               + " limit "
               + buffer.limit());
   } else {
     if (logger.isDetailEnabled())
       logger.detail(
           "FixedByteBufferPoolImpl for "
               + name
               + " got used buffer: position "
               + buffer.position()
               + " capacity "
               + buffer.capacity()
               + " limit "
               + buffer.limit());
   }
   buffer.clear();
   return buffer;
 }
Exemplo n.º 17
0
 /**
  * Writes the ETC1Data with a PKM header to the given file.
  *
  * @param file the file.
  */
 public void write(FileHandle file) {
   DataOutputStream write = null;
   byte[] buffer = new byte[10 * 1024];
   int writtenBytes = 0;
   compressedData.position(0);
   compressedData.limit(compressedData.capacity());
   try {
     write = new DataOutputStream(new GZIPOutputStream(file.write(false)));
     write.writeInt(compressedData.capacity());
     while (writtenBytes != compressedData.capacity()) {
       int bytesToWrite = Math.min(compressedData.remaining(), buffer.length);
       compressedData.get(buffer, 0, bytesToWrite);
       write.write(buffer, 0, bytesToWrite);
       writtenBytes += bytesToWrite;
     }
   } catch (Exception e) {
     throw new GdxRuntimeException("Couldn't write PKM file to '" + file + "'", e);
   } finally {
     if (write != null)
       try {
         write.close();
       } catch (Exception e) {
       }
   }
   compressedData.position(dataOffset);
   compressedData.limit(compressedData.capacity());
 }
Exemplo n.º 18
0
 /** Helper method for implementing {@link MessageLite#hashCode()} for bytes field. */
 public static int hashCodeByteBuffer(ByteBuffer bytes) {
   if (bytes.hasArray()) {
     // Fast path.
     int h =
         LiteralByteString.hashCode(
             bytes.capacity(), bytes.array(), bytes.arrayOffset(), bytes.capacity());
     return h == 0 ? 1 : h;
   } else {
     // Read the data into a temporary byte array before calculating the
     // hash value.
     final int bufferSize =
         bytes.capacity() > DEFAULT_BUFFER_SIZE ? DEFAULT_BUFFER_SIZE : bytes.capacity();
     final byte[] buffer = new byte[bufferSize];
     final ByteBuffer duplicated = bytes.duplicate();
     duplicated.clear();
     int h = bytes.capacity();
     while (duplicated.remaining() > 0) {
       final int length =
           duplicated.remaining() <= bufferSize ? duplicated.remaining() : bufferSize;
       duplicated.get(buffer, 0, length);
       h = LiteralByteString.hashCode(h, buffer, 0, length);
     }
     return h == 0 ? 1 : h;
   }
 }
Exemplo n.º 19
0
  /**
   * read the size of the op (i.e., number of bytes, including the op size) written at the given
   * position
   */
  private final int readSize(ByteBuffer reusableBuffer, long position) {
    // read op size from disk
    assert reusableBuffer.capacity() >= 4
        : "reusable buffer must have capacity >=4 when reading opSize. got ["
            + reusableBuffer.capacity()
            + "]";
    try {
      reusableBuffer.clear();
      reusableBuffer.limit(4);
      readBytes(reusableBuffer, position);
      reusableBuffer.flip();
      // Add an extra 4 to account for the operation size integer itself
      final int size = reusableBuffer.getInt() + 4;
      final long maxSize = sizeInBytes() - position;
      if (size < 0 || size > maxSize) {
        throw new TranslogCorruptedException(
            "operation size is corrupted must be [0.." + maxSize + "] but was: " + size);
      }

      return size;
    } catch (IOException e) {
      throw new ElasticsearchException(
          "unexpected exception reading from translog snapshot of "
              + this.channelReference.getPath(),
          e);
    }
  }
Exemplo n.º 20
0
  public void send(byte[] d) throws IOException {
    ByteBuffer send = ByteBuffer.allocate(messageSize + 4);
    send.clear();
    if (d.length + send.position() > send.capacity()) {
      out("!!!!!: Cant not send message, message length greater than max buffer size");
      throw new RuntimeException(
          "!!!!!: Cant not send message, message length greater than max buffer size. capacity:"
              + send.capacity()
              + " , position:"
              + send.position()
              + ","
              + " data size:"
              + d.length);
    }
    if (this.pad) {
      send.putInt(d.length);
    }
    send.put(d);
    send.flip();

    while (send.hasRemaining()) // write to channel
    {
      int bw = ssc.write(send);
      // out("bytes written: "+bw + " remaining: "+send.hasRemaining() +"   r:"+send.position()+" v
      // "+send.limit());
    }
  }
Exemplo n.º 21
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;
  }
  /** Copy the required number of bytes into the save buffer. */
  private void copyToSaveBuffer(int bytesNeeded) {
    /* How much can we get from this current read buffer? */
    int bytesFromThisBuffer;

    if (bytesNeeded <= window.remaining()) {
      bytesFromThisBuffer = bytesNeeded;
    } else {
      bytesFromThisBuffer = window.remaining();
    }

    /* Gather it all into this save buffer. */
    ByteBuffer temp;

    /* Make sure the save buffer is big enough. */
    if (saveBuffer.capacity() - threadSafeBufferPosition(saveBuffer) < bytesFromThisBuffer) {
      /* Grow the save buffer. */
      temp = ByteBuffer.allocate(saveBuffer.capacity() + bytesFromThisBuffer);
      threadSafeBufferFlip(saveBuffer);
      temp.put(saveBuffer);
      saveBuffer = temp;
    }

    /*
     * Bulk copy only the required section from the read buffer into the
     * save buffer. We need from readBuffer.position() to
     * readBuffer.position() + bytesFromThisBuffer
     */
    temp = window.getBuffer().slice();
    temp.limit(bytesFromThisBuffer);
    saveBuffer.put(temp);
    window.incrementBufferPosition(bytesFromThisBuffer);
  }
    /**
     * Change the read buffer size if we start hitting large log entries so we don't get into an
     * expensive cycle of multiple reads and piecing together of log entries.
     */
    protected void adjustReadBufferSize(int amountToRead) {

      int readBufferSize = readBuffer.capacity();

      /*
       * We need to read something larger than the current buffer
       * size.
       */
      if (amountToRead > readBufferSize) {

        /* We're not at the max yet. */
        if (readBufferSize < maxReadBufferSize) {

          /*
           * Make the buffer the minimum of amountToRead or a
           * maxReadBufferSize.
           */
          if (amountToRead < maxReadBufferSize) {
            readBufferSize = amountToRead;
            /* Make it a multiple of 1K. */
            int remainder = readBufferSize % 1024;
            readBufferSize += 1024 - remainder;
            readBufferSize = Math.min(readBufferSize, maxReadBufferSize);
          } else {
            readBufferSize = maxReadBufferSize;
          }
          readBuffer = ByteBuffer.allocate(readBufferSize);
        }

        if (amountToRead > readBuffer.capacity()) {
          nRepeatIteratorReads++;
        }
      }
    }
  /**
   * Write a chunk of bytes to the underyling ByteBuffer via this OutputStream.
   *
   * @param bytes Write bytes from this byte array.
   * @param off Start reading at this offset within byte array.
   * @param len Write this many bytes, and enlarge underyling ByteBuffer when necessary, preserving
   *     the contents.
   */
  @Override
  public synchronized void write(byte[] bytes, int off, int len) throws IOException {

    if (mBuffer.position() + len > mBuffer.capacity()) {
      expand(mBuffer.capacity() + len);
    }
    mBuffer.put(bytes, off, len);
  }
  /**
   * Write one byte to the underlying ByteBuffer via this OutputStream.
   *
   * @param b Byte to be written.
   */
  @Override
  public synchronized void write(int b) throws IOException {

    if (mBuffer.position() + 1 > mBuffer.capacity()) {
      expand(mBuffer.capacity() + 1);
    }
    mBuffer.put((byte) b);
  }
Exemplo n.º 26
0
  @Override
  public boolean processed() {
    if (position() != mByteBuffer.capacity() - mSharedByteBuffer.capacity()) {
      return false;
    }

    return true;
  }
Exemplo n.º 27
0
 /** Helper method for implementing {@link MessageLite#equals()} for bytes field. */
 public static boolean equalsByteBuffer(ByteBuffer a, ByteBuffer b) {
   if (a.capacity() != b.capacity()) {
     return false;
   }
   // ByteBuffer.equals() will only compare the remaining bytes, but we want to
   // compare all the content.
   return a.duplicate().clear().equals(b.duplicate().clear());
 }
 /**
  * Before parsing the bytes, initialize and prepare the algorithm.
  *
  * @param byteBuffer the <code>ByteBuffer</code> used by this algorithm
  * @return <code>ByteBuffer</code> used by this algorithm
  */
 public ByteBuffer preParse(ByteBuffer byteBuffer) {
   if (byteBuffer.position() == byteBuffer.capacity()) {
     // Add space at the end for \n\r
     int bufferSize =
         contentLength > 0 ? contentLength + headerLength + 5 : byteBuffer.capacity() * 2;
     byteBuffer = swapBuffer(byteBuffer, bufferSize);
   }
   return byteBuffer;
 }
Exemplo n.º 29
0
  /**
   * Method tryReadPacket2.
   *
   * @param key SelectionKey
   * @param con MMOConnection<T>
   * @param buf ByteBuffer
   * @return boolean
   */
  protected boolean tryReadPacket2(SelectionKey key, MMOConnection<T> con, ByteBuffer buf) {
    if (con.isClosed()) {
      return false;
    }

    int pos = buf.position();
    if (buf.remaining() > _sc.HEADER_SIZE) {
      int size = buf.getShort() & 0xffff;

      if ((size <= _sc.HEADER_SIZE) || (size > _sc.PACKET_SIZE)) {
        _log.error(
            "Incorrect packet size : "
                + size
                + "! Client : "
                + con.getClient()
                + ". Closing connection.");
        closeConnectionImpl(con);
        return false;
      }

      size -= _sc.HEADER_SIZE;

      if (size <= buf.remaining()) {
        stats.increaseIncomingPacketsCount();
        parseClientPacket(getPacketHandler(), buf, size, con);
        buf.position(pos + size + _sc.HEADER_SIZE);

        if (!buf.hasRemaining()) {
          freeBuffer(buf, con);
          return false;
        }

        return true;
      }

      buf.position(pos);
    }

    if (pos == buf.capacity()) {
      _log.warn(
          "Read buffer exhausted for client : "
              + con.getClient()
              + ", try to adjust buffer size, current : "
              + buf.capacity()
              + ", primary : "
              + (buf == READ_BUFFER)
              + ".");
    }

    if (buf == READ_BUFFER) {
      allocateReadBuffer(con);
    } else {
      buf.compact();
    }

    return false;
  }
Exemplo n.º 30
0
 /**
  * Serialize the Transaction to a ByteBuffer.
  *
  * <ol>
  *   <li><strong>lockSize</strong> - position 0
  *   <li><strong>locks</strong> - position 4
  *   <li><strong>writes</strong> - position 4 + lockSize
  * </ol>
  *
  * @return the ByteBuffer representation
  */
 private ByteBuffer serialize() {
   ByteBuffer _locks = ByteableCollections.toByteBuffer(locks.values());
   ByteBuffer _writes = ByteableCollections.toByteBuffer(((Queue) buffer).getWrites());
   ByteBuffer bytes = ByteBuffer.allocate(4 + _locks.capacity() + _writes.capacity());
   bytes.putInt(_locks.capacity());
   bytes.put(_locks);
   bytes.put(_writes);
   bytes.rewind();
   return bytes;
 }