コード例 #1
0
    @Override
    public void handleEvent(StreamSinkChannel channel) {
      try {
        int c;
        do {
          ByteBuffer buffer = this.buffers.peek().getBuffer();
          do {
            c = channel.write(buffer);
          } while (buffer.hasRemaining() && c > 0);

          if (!buffer.hasRemaining()) {
            safeClose(this.buffers.remove());
          }
        } while (!this.buffers.isEmpty() && c > 0);

        if (!this.buffers.isEmpty()) {
          channel.resumeWrites();
        } else {
          this.writing.decrementAndGet();

          if (this.closing.get()) {
            closeIfDone();
          } else {
            this.subscription.request(1);
          }
        }
      } catch (IOException ex) {
        onError(ex);
      }
    }
コード例 #2
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 (unmaskedpayload.hasRemaining()) unmaskedpayload.put(b);
     if (b.hasRemaining()) {
       ByteBuffer tmp = ByteBuffer.allocate(b.remaining() + unmaskedpayload.capacity());
       unmaskedpayload.flip();
       tmp.put(unmaskedpayload);
       tmp.put(b);
       unmaskedpayload = tmp;
     }
     unmaskedpayload.rewind();
     b.reset();
   }
   fin = nextframe.isFin();
 }
コード例 #3
0
    @Override
    public void onNext(ByteBuffer buffer) {
      super.onNext(buffer);

      if (this.responseChannel == null) {
        this.responseChannel = exchange.getResponseChannel();
      }

      this.writing.incrementAndGet();
      try {
        int c;
        do {
          c = this.responseChannel.write(buffer);
        } while (buffer.hasRemaining() && c > 0);

        if (buffer.hasRemaining()) {
          this.writing.incrementAndGet();
          enqueue(buffer);
          this.responseChannel.getWriteSetter().set(this);
          this.responseChannel.resumeWrites();
        } else {
          this.subscription.request(1);
        }

      } catch (IOException ex) {
        onError(ex);
      } finally {
        this.writing.decrementAndGet();
        if (this.closing.get()) {
          closeIfDone();
        }
      }
    }
コード例 #4
0
ファイル: OutputQueueImpl.java プロジェクト: jimieweile/repo
  public synchronized boolean enqueue(ByteBuffer byteBuffer) {
    if (byteBuffer.remaining() == 0) {
      return false;
    }

    if (queue.size() > 0) {
      ByteBuffer tail = queue.getLast();

      if (tail.hasRemaining()) {
        topUpBuffer(tail, byteBuffer);
      }
    }

    while (byteBuffer.hasRemaining()) {
      ByteBuffer newBuf = bufferFactory.newBuffer();

      topUpBuffer(newBuf, byteBuffer);

      queue.addLast(newBuf);
    }

    facade.modifyInterestOps(SelectionKey.OP_WRITE, 0);

    return true;
  }
コード例 #5
0
ファイル: ThreadTest.java プロジェクト: redpower1998/osjava
    /*
     * drive as much data down our output as possible
     */
    private void writeToOutput() {
      if (outBuffer.position() == 0 && outCount == 0) {
        /*
         * We've finished already
         */
        /*
        System.out.println("Already done.");
        */
        return;
      }
      while (true) {
        byte[] foo = new byte[1];
        while (outBuffer.hasRemaining() && outCount > 0) {
          outGenerator.nextBytes(foo);
          outBuffer.put(foo[0]);
          outCount--;
        }

        outBuffer.flip();
        out.broker(outBuffer, outCount == 0);
        outBuffer.compact();

        /*
         * if our outBuffer still has data
         * or if we're no longer filling the outBuffer with
         * data, return, as whatever we're writing to has
         * had its' fill
         */
        if (!outBuffer.hasRemaining() || outCount == 0) {
          return;
        }
      }
    }
コード例 #6
0
 @Override
 public int write(GatheringByteChannel channel) throws IOException {
   int written = 0;
   if (sizeBuffer.hasRemaining()) {
     written = GenUtil.retryWrite(channel, sizeBuffer);
     if (!sizeBuffer.hasRemaining()) {
       return written;
     }
   }
   if (!sizeBuffer.hasRemaining()) {
     if (currentReply == null) {
       if (messagesList.size() == 0) {
         throw new IOException("MessageSet list empty!");
       }
       currentReply = new FetchReply(partitionList.get(0), messagesList.get(0), offsetList.get(0));
     }
     written += currentReply.write(channel);
     if (currentReply.complete()) {
       index++;
       if (index == messagesList.size()) {
         complete = true;
       } else {
         currentReply =
             new FetchReply(
                 partitionList.get(index), messagesList.get(index), offsetList.get(index));
       }
     }
   }
   return written;
 }
コード例 #7
0
  @Override
  public int read(ScatteringByteChannel channel) throws IOException {
    int read = 0;
    if (sizeBuffer.hasRemaining()) {
      int num = channel.read(sizeBuffer);
      if (num < 0) {
        throw new IOException("end-of-stream reached");
      }
      read += num;
      if (sizeBuffer.hasRemaining()) {
        return read;
      } else {
        sizeBuffer.flip();
        size = sizeBuffer.getInt();
        contentBuffer = ByteBuffer.allocate(size);
      }
    }

    if (!sizeBuffer.hasRemaining()) {
      int num = channel.read(contentBuffer);
      if (num < 0) {
        throw new IOException("end-of-stream reached");
      }
      read += num;
      if (!contentBuffer.hasRemaining()) {
        contentBuffer.flip();
        complete = true;
      }
    }
    return read;
  }
コード例 #8
0
  /*
   * Begin the shutdown process.
   * <P>
   * Close out the SSLEngine if not already done so, then
   * wrap our outgoing close_notify message and try to send it on.
   * <P>
   * Return true when we're done passing the shutdown messsages.
   */
  boolean shutdown() throws IOException {

    if (!shutdown) {
      sslEngine.closeOutbound();
      shutdown = true;
    }

    if (outNetBB.hasRemaining() && tryFlush(outNetBB)) {
      return false;
    }

    /*
     * By RFC 2616, we can "fire and forget" our close_notify
     * message, so that's what we'll do here.
     */
    outNetBB.clear();
    SSLEngineResult result = sslEngine.wrap(hsBB, outNetBB);
    if (result.getStatus() != Status.CLOSED) {
      throw new SSLException("Improper close state");
    }
    outNetBB.flip();

    /*
     * We won't wait for a select here, but if this doesn't work,
     * we'll cycle back through on the next select.
     */
    if (outNetBB.hasRemaining()) {
      tryFlush(outNetBB);
    }

    return (!outNetBB.hasRemaining() && (result.getHandshakeStatus() != HandshakeStatus.NEED_WRAP));
  }
コード例 #9
0
  protected OperationResult doRead(
      ReadableByteChannel channel,
      ByteBuffer byteBuffer,
      AsyncQueueDataProcessor readPostProcessor,
      OperationResult dstResult)
      throws IOException {
    int totalReadBytes = 0;

    if (readPostProcessor != null) {
      ByteBuffer inputByteBuffer = null;
      int oldPosition = byteBuffer.position();

      do {
        inputByteBuffer = readPostProcessor.getInternalByteBuffer();
        int readBytes = doRead(channel, inputByteBuffer);

        if (readBytes > 0) {
          readPostProcessor.process(byteBuffer);
        } else if (readBytes == -1) {
          if (byteBuffer.position() == oldPosition) {
            throw new EOFException();
          } else {
            break;
          }
        }
        totalReadBytes += readBytes;
      } while (byteBuffer.hasRemaining() && !inputByteBuffer.hasRemaining());
    } else {
      totalReadBytes = doRead(channel, byteBuffer);
    }

    dstResult.bytesProcessed = totalReadBytes;
    dstResult.address = ((SocketChannel) channel).socket().getRemoteSocketAddress();
    return dstResult;
  }
コード例 #10
0
ファイル: ChannelStream.java プロジェクト: bruceadams/jruby
  /**
   * Copies bytes from the channel buffer into a destination <tt>ByteBuffer</tt>
   *
   * @param dst A <tt>ByteBuffer</tt> to place the data in.
   * @return The number of bytes copied.
   */
  private final int copyBufferedBytes(ByteBuffer dst) {
    final int bytesToCopy = dst.remaining();

    if (ungotc != -1 && dst.hasRemaining()) {
      dst.put((byte) ungotc);
      ungotc = -1;
    }

    if (buffer.hasRemaining() && dst.hasRemaining()) {

      if (dst.remaining() >= buffer.remaining()) {
        //
        // Copy out any buffered bytes
        //
        dst.put(buffer);

      } else {
        //
        // Need to clamp source (buffer) size to avoid overrun
        //
        ByteBuffer tmp = buffer.duplicate();
        tmp.limit(tmp.position() + dst.remaining());
        dst.put(tmp);
        buffer.position(tmp.position());
      }
    }

    return bytesToCopy - dst.remaining();
  }
コード例 #11
0
 protected boolean writeBatch() {
   try {
     if (handler.getChannel().write(buffer) < 0) {
       if (log.isTraceEnabled()) {
         log.trace("Closing channel");
       }
       inError = true;
       return false;
     } else if (buffer.hasRemaining()) {
       if (handler.getChannel().write(buffer) < 0) {
         if (log.isTraceEnabled()) {
           log.trace("Closing channel");
         }
         inError = true;
         return false;
       }
     }
   } catch (IOException e) {
     if (Utils.isClose(e)) {
       log.info(String.format("closing acknowledger %s ", fsm.getName()));
     } else {
       log.warn(
           String.format("Unable to write batch commit acknowledgement %s", fsm.getName()), e);
     }
     error();
     return false;
   }
   return !buffer.hasRemaining();
 }
コード例 #12
0
  /**
   * Read line.
   *
   * @param buf the buf
   * @return the string
   */
  public static String readLine(ByteBuffer buf) {
    boolean completed = false;
    buf.mark();
    while (buf.hasRemaining() && !completed) {
      byte b = buf.get();
      if (b == '\r') {
        if (buf.hasRemaining() && buf.get() == '\n') {
          completed = true;
        }
      }
    }

    if (!completed) {
      return null;
    }

    int limit = buf.position();
    buf.reset();
    int length = limit - buf.position();
    byte[] tmp = new byte[length];
    buf.get(tmp, 0, length);
    try {
      String line = new String(tmp, "US-ASCII");
      if (log.isLoggable(Level.FINEST)) {
        log.finest(line.trim());
      }
      return line;
    } catch (UnsupportedEncodingException e) {;
    }
    return null;
  }
コード例 #13
0
ファイル: SetCommand.java プロジェクト: nurkiewicz/hazelcast
 public boolean writeTo(ByteBuffer bb) {
   if (response == null) {
     response = ByteBuffer.wrap(STORED);
   }
   while (bb.hasRemaining() && response.hasRemaining()) {
     bb.put(response.get());
   }
   return !response.hasRemaining();
 }
コード例 #14
0
 public SSLEngineResult unwrap(
     ByteBuffer[] dsts,
     int offset,
     int length,
     ByteBuffer src,
     boolean needUnwrap,
     int actionIndex) {
   if (!src.hasRemaining()) {
     return new SSLEngineResult(
         closed && closeMessageUnwrapped ? Status.CLOSED : Status.BUFFER_UNDERFLOW,
         needUnwrap ? HandshakeStatus.NEED_UNWRAP : HandshakeStatus.NOT_HANDSHAKING,
         0,
         0);
   }
   Status okStatus = closed && closeMessageUnwrapped ? Status.CLOSED : Status.OK;
   // amount of bytes available at src
   int initialSrcRemaining = src.remaining();
   int bytesProduced = 0;
   while (src.hasRemaining()) {
     String unwrapped = unwrapBytes(dsts, offset, length, src, needUnwrap);
     int bytesConsumed = initialSrcRemaining - src.remaining();
     if (unwrapped == null) {
       if (bytesProduced == 0) {
         return new SSLEngineResult(Status.BUFFER_OVERFLOW, HandshakeStatus.NEED_UNWRAP, 0, 0);
       }
       return new SSLEngineResult(
           okStatus, HandshakeStatus.NEED_UNWRAP, bytesConsumed, bytesProduced);
     }
     // it means handshake message was found when it is not expected, ignore it and return
     // everything that
     // was read up until now
     if (unwrapped.length() == 0) {
       if (bytesConsumed > 0) {
         actionAccountedFor(HandshakeAction.PERFORM_REQUESTED_ACTION, actionIndex);
       }
       return new SSLEngineResult(okStatus, getHandshakeStatus(), bytesConsumed, bytesProduced);
     }
     // it means we are on a needUnwrap and we found the handshake message we are seeking
     if (unwrapped.equals(HANDSHAKE_MSG)) {
       actionAccountedFor(HandshakeAction.NEED_UNWRAP, actionIndex);
       // move to next action, NEED_UNWRAP action is finally accomplished
       return new SSLEngineResult(okStatus, getHandshakeStatus(), bytesConsumed, bytesProduced);
     }
     if (unwrapped.equals(CLOSE_MSG)) {
       closed = true;
       closeMessageUnwrapped = true;
       return new SSLEngineResult(
           Status.CLOSED, getHandshakeStatus(), bytesConsumed, bytesProduced);
     }
     bytesProduced += unwrapped.length();
   }
   int bytesConsumed = initialSrcRemaining - src.remaining();
   if (bytesConsumed > 0 && !needUnwrap) {
     actionAccountedFor(HandshakeAction.PERFORM_REQUESTED_ACTION, actionIndex);
   }
   return new SSLEngineResult(okStatus, getHandshakeStatus(), bytesConsumed, bytesProduced);
 }
コード例 #15
0
 public int read() throws IOException {
   do {
     if (encoderOut.hasRemaining()) {
       return encoderOut.get() & 0xff;
     }
     fillBuffer();
   } while (!endOfInput || encoderOut.hasRemaining());
   return -1;
 }
コード例 #16
0
 private Object roundtrip(Type type, Object obj) {
   ByteBuffer buffer = ByteBuffer.allocate(type.sizeOf(obj));
   type.write(buffer, obj);
   assertFalse("The buffer should now be full.", buffer.hasRemaining());
   buffer.rewind();
   Object read = type.read(buffer);
   assertFalse("All bytes should have been read.", buffer.hasRemaining());
   return read;
 }
コード例 #17
0
  protected String sendAndReceive(byte[] payload) throws IOException {
    ByteBuffer sbuf = ByteBuffer.wrap(payload);
    ByteBuffer rbuf = ByteBuffer.allocateDirect(256);
    CharsetDecoder rbufDecoder = Charset.forName("UTF-8").newDecoder();
    StringBuilder response = new StringBuilder();

    int ops = SelectionKey.OP_WRITE | SelectionKey.OP_READ;
    if (this.channel.isConnectionPending()) {
      ops = ops | SelectionKey.OP_CONNECT;
    }

    Selector selector = Selector.open();
    try {
      this.channel.register(selector, ops);
      while (true) {
        if (0 < selector.select(Client.POLLS_INTERVAL * 1000)) {
          Iterator keys = selector.selectedKeys().iterator();
          while (keys.hasNext()) {
            SelectionKey key = (SelectionKey) keys.next();
            SocketChannel ch = (SocketChannel) key.channel();
            if (key.isConnectable()) {
              // Just connected
              ch.finishConnect();
            }
            if (key.isReadable() && !sbuf.hasRemaining()) {
              // Receiving the response
              while (0 < ch.read(rbuf)) {
                rbuf.flip();
                response.append(rbufDecoder.decode(rbuf).toString());
              }
              if (2 <= response.length()
                  && response
                      .substring(response.length() - 2, response.length())
                      .equals(SocketClient.TERMINATOR)) {
                response.setLength(response.length() - 2);
                return response.toString();
              } else if (0 == response.length()) {
                throw new IOException("Connection lost");
              }
            }
            if (key.isWritable() && sbuf.hasRemaining()) {
              // Sending the request
              while (0 < ch.write(sbuf) && sbuf.hasRemaining()) {
                //
              }
            }
            keys.remove();
          }
        }
      }
    } catch (java.lang.Exception e) {
      throw new IOException("API communication failed: " + e.toString());
    } finally {
      selector.close();
    }
  }
コード例 #18
0
  /*
   * Flush any remaining data.
   * <P>
   * Return true when the fileChannelBB and outNetBB are empty.
   */
  boolean dataFlush() throws IOException {
    boolean fileFlushed = true;

    if ((fileChannelBB != null) && fileChannelBB.hasRemaining()) {
      doWrite(fileChannelBB);
      fileFlushed = !fileChannelBB.hasRemaining();
    } else if (outNetBB.hasRemaining()) {
      tryFlush(outNetBB);
    }

    return (fileFlushed && !outNetBB.hasRemaining());
  }
コード例 #19
0
ファイル: AjpParser.java プロジェクト: johnaohara/undertow
  private StringHolder parseString(ByteBuffer buf, AjpParseState state, boolean header) {
    if (!buf.hasRemaining()) {
      return new StringHolder(null, false);
    }
    int stringLength = state.stringLength;
    if (stringLength == -1) {
      int number = buf.get() & 0xFF;
      if (buf.hasRemaining()) {
        final byte b = buf.get();
        stringLength = ((0xFF & number) << 8) + (b & 0xFF);
      } else {
        state.stringLength = number | STRING_LENGTH_MASK;
        return new StringHolder(null, false);
      }
    } else if ((stringLength & STRING_LENGTH_MASK) != 0) {
      int number = stringLength & ~STRING_LENGTH_MASK;
      stringLength = ((0xFF & number) << 8) + (buf.get() & 0xFF);
    }
    if (header && (stringLength & 0xFF00) != 0) {
      state.stringLength = -1;
      return new StringHolder(HTTP_HEADERS[stringLength & 0xFF]);
    }
    if (stringLength == 0xFFFF) {
      // OxFFFF means null
      state.stringLength = -1;
      return new StringHolder(null, true);
    }
    StringBuilder builder = state.currentString;

    if (builder == null) {
      builder = new StringBuilder();
      state.currentString = builder;
    }
    int length = builder.length();
    while (length < stringLength) {
      if (!buf.hasRemaining()) {
        state.stringLength = stringLength;
        return new StringHolder(null, false);
      }
      builder.append((char) buf.get());
      ++length;
    }

    if (buf.hasRemaining()) {
      buf.get(); // null terminator
      state.currentString = null;
      state.stringLength = -1;
      return new StringHolder(builder.toString(), true);
    } else {
      return new StringHolder(null, false);
    }
  }
コード例 #20
0
ファイル: SetCommand.java プロジェクト: nurkiewicz/hazelcast
 public boolean doRead(ByteBuffer cb) {
   copy(cb);
   if (!bbValue.hasRemaining()) {
     while (cb.hasRemaining()) {
       char c = (char) cb.get();
       if (c == '\n') {
         bbValue.flip();
         return true;
       }
     }
   }
   return false;
 }
コード例 #21
0
 private void ping(int i) throws IOException {
   buffy.putInt(0, i);
   do {
     client.write(buffy);
   } while (buffy.hasRemaining());
   buffy.clear();
   buffy.putInt(0, 0);
   do {
     client.read(buffy);
   } while (buffy.hasRemaining());
   if (buffy.getInt(0) != i) throw new RuntimeException();
   buffy.clear();
 }
コード例 #22
0
  @Test
  public void multipleWritesWithSimpleHandshake() throws IOException {
    // map data to be read and written
    engineMock.addWrapEntry(HANDSHAKE_MSG, "{handshake data}");
    engineMock.addWrapEntry("MockTest", "{data}");
    engineMock.addWrapEntry(CLOSE_MSG, "{message closed}");
    // set the handshake actions that engineMock will emulate
    engineMock.setHandshakeActions(NEED_WRAP, NEED_UNWRAP, NEED_TASK, FINISH);
    // set ReadData on conduitMock
    conduitMock.setReadData("{handshake data}");
    // enable read on conduitMock, meaning that data above will be available to be read right away
    conduitMock.enableReads(true);
    // message we want to write
    final ByteBuffer buffer = ByteBuffer.allocate(100);
    buffer.put("MockTest".getBytes("UTF-8")).flip();

    // attempt to write... channel is expected to write all messages without any issues
    for (int i = 0; i < 10; i++) {
      while (buffer.hasRemaining()) {
        assertEquals(8, sslChannel.write(buffer));
        assertFalse(buffer.hasRemaining());
      }
      buffer.flip();
    }

    // channel should not be able to shutdown writes... for that, it must receive a close message
    sslChannel.shutdownWrites();
    assertFalse(sslChannel.flush());
    // send the close message
    conduitMock.setReadData("{message closed}");
    conduitMock.enableReads(true);
    sslChannel.shutdownWrites();
    assertTrue(sslChannel.flush());

    // close channel
    sslChannel.close();
    // data expected to have been written to 'conduitMock' by 'sslChannel'
    assertWrittenMessage(
        "{handshake data}",
        "{data}",
        "{data}",
        "{data}",
        "{data}",
        "{data}",
        "{data}",
        "{data}",
        "{data}",
        "{data}",
        "{data}",
        "{message closed}");
  }
コード例 #23
0
ファイル: HttpOutput.java プロジェクト: hypnoce/jetty.project
    @Override
    protected Action process() {
      // flush any content from the aggregate
      if (BufferUtil.hasContent(_aggregate)) {
        _completed = _len == 0;
        write(_aggregate, _complete && _completed, this);
        return Action.SCHEDULED;
      }

      // Can we just aggregate the remainder?
      if (!_complete && _len < BufferUtil.space(_aggregate) && _len < _commitSize) {
        int position = BufferUtil.flipToFill(_aggregate);
        BufferUtil.put(_buffer, _aggregate);
        BufferUtil.flipToFlush(_aggregate, position);
        return Action.SUCCEEDED;
      }

      // Is there data left to write?
      if (_buffer.hasRemaining()) {
        // if there is no slice, just write it
        if (_slice == null) {
          _completed = true;
          write(_buffer, _complete, this);
          return Action.SCHEDULED;
        }

        // otherwise take a slice
        int p = _buffer.position();
        int l = Math.min(getBufferSize(), _buffer.remaining());
        int pl = p + l;
        _slice.limit(pl);
        _buffer.position(pl);
        _slice.position(p);
        _completed = !_buffer.hasRemaining();
        write(_slice, _complete && _completed, this);
        return Action.SCHEDULED;
      }

      // all content written, but if we have not yet signal completion, we
      // need to do so
      if (_complete && !_completed) {
        _completed = true;
        write(BufferUtil.EMPTY_BUFFER, true, this);
        return Action.SCHEDULED;
      }

      if (LOG.isDebugEnabled() && _completed) LOG.debug("EOF of {}", this);
      return Action.SUCCEEDED;
    }
コード例 #24
0
 public int read(ByteBuffer output) throws IOException {
   if (!handshakeCompleted) {
     handshake();
   }
   int readBytesCount = 0;
   int limit;
   if (in.hasRemaining()) {
     limit = Math.min(in.remaining(), output.remaining());
     for (int i = 0; i < limit; i++) {
       output.put(in.get());
       readBytesCount++;
     }
     return readBytesCount;
   }
   if (netInBuffer.hasRemaining()) {
     unwrap(netInBuffer);
     in.flip();
     limit = Math.min(in.remaining(), output.remaining());
     for (int i = 0; i < limit; i++) {
       output.put(in.get());
       readBytesCount++;
     }
     if (sslEngineResult.getStatus() != SSLEngineResult.Status.BUFFER_UNDERFLOW) {
       netInBuffer.clear();
       netInBuffer.flip();
       return readBytesCount;
     }
   }
   if (netInBuffer.hasRemaining()) {
     netInBuffer.compact();
   } else {
     netInBuffer.clear();
   }
   if (socketChannel.read(netInBuffer) == -1) {
     netInBuffer.clear();
     netInBuffer.flip();
     return -1;
   }
   netInBuffer.flip();
   unwrap(netInBuffer);
   in.flip();
   limit = Math.min(in.remaining(), output.remaining());
   for (int i = 0; i < limit; i++) {
     output.put(in.get());
     readBytesCount++;
   }
   return readBytesCount;
 }
コード例 #25
0
ファイル: Zip64.java プロジェクト: BvbKoala/SourceCode
  /**
   * Returns the size of the extended info record if {@code extras} contains a zip64 extended info
   * record, {@code -1} otherwise. The buffer will be positioned at the start of the extended info
   * record.
   */
  private static int getZip64ExtendedInfoSize(ByteBuffer extras) {
    try {
      while (extras.hasRemaining()) {
        final int headerId = extras.getShort() & 0xffff;
        final int length = extras.getShort() & 0xffff;
        if (headerId == ZIP64_EXTENDED_INFO_HEADER_ID) {
          if (extras.remaining() >= length) {
            return length;
          } else {
            return -1;
          }
        } else {
          extras.position(extras.position() + length);
        }
      }

      return -1;
    } catch (BufferUnderflowException bue) {
      // We'll underflow if we have an incomplete header in our extras.
      return -1;
    } catch (IllegalArgumentException iae) {
      // ByteBuffer.position() will throw if we have a truncated extra or
      // an invalid length in the header.
      return -1;
    }
  }
コード例 #26
0
 /**
  * Read data from a top level Variable and send data to a WritableByteChannel.
  *
  * @param v2 Variable
  * @param section wanted section of data of Variable. The section list is a list of ucar.ma2.Range
  *     which define the requested data subset.
  * @param channel WritableByteChannel object - channel that can write bytes.
  * @return the number of bytes written, possibly zero.
  */
 public long readToByteChannel11(
     ucar.nc2.Variable v2, Section section, WritableByteChannel channel)
     throws java.io.IOException, ucar.ma2.InvalidRangeException {
   Array data = readData(v2, section);
   float[] ftdata = new float[(int) data.getSize()];
   byte[] bytedata = new byte[(int) data.getSize() * 4];
   IndexIterator iter = data.getIndexIterator();
   int i = 0;
   ByteBuffer buffer = ByteBuffer.allocateDirect(bytedata.length);
   while (iter.hasNext()) {
     ftdata[i] = iter.getFloatNext();
     bytedata[i] = new Float(ftdata[i]).byteValue();
     buffer.put(bytedata[i]);
     i++;
   }
   buffer = ByteBuffer.wrap(bytedata);
   // write the bytes to the channel
   int count = channel.write(buffer);
   System.out.println("COUNT=" + count);
   // check if all bytes where written
   if (buffer.hasRemaining()) {
     // if not all bytes were written, move the unwritten bytes to the beginning and
     // set position just after the last unwritten byte
     buffer.compact();
   } else {
     buffer.clear();
   }
   return (long) count;
 }
コード例 #27
0
 private static ByteBuffer buffer(int i) {
   ByteBuffer buffer = ByteBuffer.allocate(i);
   while (buffer.hasRemaining()) {
     buffer.put((byte) i);
   }
   return (ByteBuffer) buffer.flip();
 }
コード例 #28
0
ファイル: client.java プロジェクト: sheldonj22/NetIO
  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());
    }
  }
コード例 #29
0
  @Override
  public void writeAfterController(long timestamp) {
    jointCommand.clear();

    jointCommand.putLong(estimatorTicksPerControlTick);
    jointCommand.putLong(timestamp);
    jointCommand.putLong(estimatorFrequencyInHz);

    for (int i = 0; i < joints.size(); i++) {
      OneDoFJoint joint = joints.get(i);

      if (fingerJointMap == null || !fingerJointMap.containsKey(joint.getName())) {
        if (joint.isUnderPositionControl()) jointCommand.putDouble(joint.getqDesired());
        else jointCommand.putDouble(joint.getTau());
      } else
        jointCommand.putDouble(
            fingerJointMap
                .get(joint.getName())
                .getqDesired()); // fingers are always position controlled
    }

    jointCommand.flip();

    try {
      while (jointCommand.hasRemaining()) {
        channel.write(jointCommand);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
コード例 #30
0
  @NonNull
  protected static Map<String, Chunk> readChunks(@NonNull File file) throws IOException {
    Map<String, Chunk> chunks = Maps.newHashMap();

    byte[] fileBuffer = Files.toByteArray(file);
    ByteBuffer buffer = ByteBuffer.wrap(fileBuffer);

    byte[] sig = new byte[8];
    buffer.get(sig);

    assertTrue(Arrays.equals(sig, SIGNATURE));

    byte[] data, type;
    int len;
    int crc32;

    while (buffer.hasRemaining()) {
      len = buffer.getInt();

      type = new byte[4];
      buffer.get(type);

      data = new byte[len];
      buffer.get(data);

      // crc
      crc32 = buffer.getInt();

      Chunk chunk = new Chunk(type, data, crc32);
      chunks.put(chunk.getTypeAsString(), chunk);
    }

    return chunks;
  }