Пример #1
0
 @Test
 public void testReadGenerationChanged() throws IOException {
   BlobId blobId = BlobId.of(BUCKET_NAME, BLOB_NAME);
   reader = new BlobReadChannel(options, blobId, EMPTY_RPC_OPTIONS);
   byte[] firstResult = randomByteArray(DEFAULT_CHUNK_SIZE);
   byte[] secondResult = randomByteArray(DEFAULT_CHUNK_SIZE);
   ByteBuffer firstReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
   ByteBuffer secondReadBuffer = ByteBuffer.allocate(DEFAULT_CHUNK_SIZE);
   expect(storageRpcMock.read(blobId.toPb(), EMPTY_RPC_OPTIONS, 0, DEFAULT_CHUNK_SIZE))
       .andReturn(StorageRpc.Tuple.of("etag1", firstResult));
   expect(
           storageRpcMock.read(
               blobId.toPb(), EMPTY_RPC_OPTIONS, DEFAULT_CHUNK_SIZE, DEFAULT_CHUNK_SIZE))
       .andReturn(StorageRpc.Tuple.of("etag2", secondResult));
   replay(storageRpcMock);
   reader.read(firstReadBuffer);
   try {
     reader.read(secondReadBuffer);
     fail("Expected ReadChannel read to throw StorageException");
   } catch (StorageException ex) {
     StringBuilder messageBuilder = new StringBuilder();
     messageBuilder.append("Blob ").append(blobId).append(" was updated while reading");
     assertEquals(messageBuilder.toString(), ex.getMessage());
   }
 }
Пример #2
0
 public static String getRegSuccessStr() {
   String start =
       "23 23 00 4D 01 55 D2 0F E7 13 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 BE E2 58 31 32 33 34 35 36 37 38 39 31 39 39 "; // 包头和size
   String[] replace = {"31", "32", "33", "34", "35", "36", "37", "38", "39"};
   // 根据注册校验结果,形成返回数据包
   StringBuilder sb = new StringBuilder();
   sb.append(start);
   // 随机生成vin
   for (int j = 0; j < 17; j++) {
     int max = 8;
     int min = 0;
     Random random = new Random();
     int s = random.nextInt(max) % (max - min + 1) + min;
     sb.append(replace[s] + " ");
   }
   ByteBuffer bb = ByteBuffer.allocate(1024);
   String byteString = sb.toString();
   String[] command = byteString.split(" ");
   byte[] abc = new byte[command.length];
   for (int i = 0; i < command.length; i++) {
     abc[i] = Integer.valueOf(command[i], 16).byteValue();
   }
   bb.put(abc);
   for (int i = 0; i < 15; i++) bb.put(Integer.valueOf("00", 16).byteValue());
   bb.flip();
   byte[] bodyData = getBytesFromByteBuffer(bb); // 不包含checkSum的字节数组
   ByteBuffer re = ByteBuffer.allocate(1024);
   re.put(bodyData);
   re.put(getCheckSum(bodyData));
   re.flip();
   return bytes2hex(getBytesFromByteBuffer(re));
 }
Пример #3
0
  /*
   * Create and size the buffers appropriately.
   */
  private void createBuffers() {

    /*
     * We'll assume the buffer sizes are the same
     * between client and server.
     */
    SSLSession session = clientEngine.getSession();
    int appBufferMax = session.getApplicationBufferSize();
    int netBufferMax = session.getPacketBufferSize();

    /*
     * We'll make the input buffers a bit bigger than the max needed
     * size, so that unwrap()s following a successful data transfer
     * won't generate BUFFER_OVERFLOWS.
     *
     * We'll use a mix of direct and indirect ByteBuffers for
     * tutorial purposes only.  In reality, only use direct
     * ByteBuffers when they give a clear performance enhancement.
     */
    clientIn = ByteBuffer.allocate(appBufferMax + 50);
    serverIn = ByteBuffer.allocate(appBufferMax + 50);

    cTOs = ByteBuffer.allocateDirect(netBufferMax);
    sTOc = ByteBuffer.allocateDirect(netBufferMax);

    clientOut = ByteBuffer.wrap("Hi Server, I'm Client".getBytes());
    serverOut = ByteBuffer.wrap("Hello Client, I'm Server".getBytes());
  }
Пример #4
0
 /**
  * 入力データからパケットを構築して送信する。今回はSocketを複数開いたり、 ということは起こらないので本メソッドに集約してしまってる。空文字列が送られてきたら
  * 特殊パターンとしてrefresh用のパケットを構築する(つまり\0単独は特殊パターン)
  *
  * @return 正常終了時は0。サーバへの接続が失われていれば-4。その他I/Oエラー時は-1。
  */
 private int sendPacket(String src) {
   OutputStream writer;
   Log.d("moku99", "sending a packet");
   try {
     if (clientSock.isConnected() == false) {
       return -4;
     }
     writer = clientSock.getOutputStream();
     if (src.equals("")) {
       // 空の特殊パターン
       ByteBuffer buf = ByteBuffer.allocate(8);
       buf.putInt(myId.intValue());
       buf.putInt(0);
       writer.write(buf.array());
     } else {
       // 通常メッセージ送信パターン
       byte[] strBuf = src.getBytes();
       ByteBuffer buf = ByteBuffer.allocate(8 + strBuf.length);
       buf.putInt(myId.intValue());
       buf.putInt(strBuf.length);
       buf.put(strBuf);
       writer.write(buf.array());
     }
   } catch (IOException e) {
     Log.d("moku99", e.getLocalizedMessage());
     return -1;
   }
   return 0;
 }
Пример #5
0
  public void write(Tag tag, RandomAccessFile raf, RandomAccessFile tempRaf)
      throws CannotWriteException, IOException {
    FileChannel fc = raf.getChannel();

    int oldTagSize = 0;

    if (tagExists(fc)) {
      // read the length
      if (!canOverwrite(raf))
        throw new CannotWriteException("Overwritting of this kind of ID3v2 tag not supported yet");
      fc.position(6);

      ByteBuffer buf = ByteBuffer.allocate(4);
      fc.read(buf);
      oldTagSize = (buf.get(0) & 0xFF) << 21;
      oldTagSize += (buf.get(1) & 0xFF) << 14;
      oldTagSize += (buf.get(2) & 0xFF) << 7;
      oldTagSize += buf.get(3) & 0xFF;
      oldTagSize += 10;

      // System.err.println("Old tag size: "+oldTagSize);
      int newTagSize = tc.getTagLength(tag);

      if (oldTagSize >= newTagSize) {
        // replace
        // System.err.println("Old ID32v Tag found, replacing the old
        // tag");
        fc.position(0);

        fc.write(tc.convert(tag, oldTagSize - newTagSize));

        // ID3v2 Tag Written

        return;
      }
    }

    // create new tag with padding
    // System.err.println("Creating a new ID3v2 Tag");
    fc.position(oldTagSize);

    if (fc.size() > 15 * 1024 * 1024) {
      FileChannel tempFC = tempRaf.getChannel();

      tempFC.position(0);
      tempFC.write(tc.convert(tag, Id3v2TagCreator.DEFAULT_PADDING));
      tempFC.transferFrom(fc, tempFC.position(), fc.size() - oldTagSize);

      fc.close();
    } else {
      ByteBuffer[] content = new ByteBuffer[2];

      content[1] = ByteBuffer.allocate((int) fc.size());
      fc.read(content[1]);
      content[1].rewind();
      content[0] = tc.convert(tag, Id3v2TagCreator.DEFAULT_PADDING);
      fc.position(0);
      fc.write(content);
    }
  }
Пример #6
0
  public synchronized String slurp(ThreadContext tc) {
    try {
      // Read in file.
      ArrayList<ByteBuffer> buffers = new ArrayList<ByteBuffer>();
      ByteBuffer curBuffer = ByteBuffer.allocate(32768);
      int total = 0;
      int read;
      if (readBuffer != null) {
        total = readBuffer.limit() - readBuffer.position();
        buffers.add(ByteBuffer.wrap(readBuffer.array(), readBuffer.position(), total));
        readBuffer = null;
      }
      while ((read = chan.read(curBuffer)) != -1) {
        curBuffer.flip();
        buffers.add(curBuffer);
        curBuffer = ByteBuffer.allocate(32768);
        total += read;
      }
      eof = true;

      return decodeBuffers(buffers, total);
    } catch (IOException e) {
      throw ExceptionHandling.dieInternal(tc, e);
    }
  }
Пример #7
0
  // Creates the first frame, which contains the message properties
  final ByteBuffer makeFirstFrame() {
    // Write the properties into a bytebuffer first:
    ByteBuffer frame;
    frame = ByteBuffer.allocate(0x20);
    for (Map.Entry<String, String> entry : this.properties.entrySet()) {
      frame = writeCString(frame, entry.getKey());
      frame = writeCString(frame, entry.getValue());
    }
    int len = frame.position();
    frame.limit(len);
    frame.rewind();

    // Setup compression objects if this message should be compressed
    if (this.isCompressed) {
      try {
        this.gzipIn = new ByteBufferInputStream(this.body);
        this.gzipOut = new GZIPOutputStream(new ByteArrayOutputStream());
      } catch (IOException e) {
      }
    }

    // Then make another bytebuffer and copy them into it after writing the header
    // This is because the header is written as varints and the space it will occupy
    // cannot be determined before writing the properties
    // Since the properties are almost always small, this has little cost
    ByteBuffer outFrame = ByteBuffer.allocate(len + 12);
    writeVarint(outFrame, this.number);
    writeVarint(outFrame, this.flags | MORECOMING);
    writeVarint(outFrame, len);
    outFrame.put(frame);
    outFrame.limit(outFrame.position());
    outFrame.rewind();
    return outFrame;
  }
    @Test
    public void nemesisBlockCannotBeLoadedFromInvalidBlob() {
      // Arrange:
      final byte[] buffer = loadNemesisBlockBlobObject();
      final byte[] badBuffer1 =
          ByteBuffer.allocate(3 + buffer.length).put("bad".getBytes()).put(buffer).array();
      final byte[] badBuffer2 =
          ByteBuffer.allocate(3 + buffer.length)
              .put(Arrays.copyOfRange(buffer, 0, 100))
              .put("bad".getBytes())
              .put(Arrays.copyOfRange(buffer, 100, buffer.length))
              .array();

      // Act:
      ExceptionAssert.assertThrows(
          v ->
              NemesisBlock.fromBlobObject(
                  NEMESIS_BLOCK_INFO,
                  badBuffer1,
                  new DeserializationContext(new MockAccountLookup())),
          IllegalArgumentException.class);
      ExceptionAssert.assertThrows(
          v ->
              NemesisBlock.fromBlobObject(
                  NEMESIS_BLOCK_INFO,
                  badBuffer2,
                  new DeserializationContext(new MockAccountLookup())),
          SerializationException.class);
    }
Пример #9
0
  private void assertHandlesLogTruncation(XaCommand cmd) throws IOException {
    inMemoryBuffer.reset();
    cmd.writeToFile(inMemoryBuffer);

    int bytesSuccessfullyWritten = inMemoryBuffer.bytesWritten();
    assertEquals(cmd, Command.readCommand(null, null, inMemoryBuffer, ByteBuffer.allocate(100)));

    bytesSuccessfullyWritten--;

    while (bytesSuccessfullyWritten-- > 0) {
      inMemoryBuffer.reset();
      cmd.writeToFile(inMemoryBuffer);
      inMemoryBuffer.truncateTo(bytesSuccessfullyWritten);

      Command deserialized =
          Command.readCommand(null, null, inMemoryBuffer, ByteBuffer.allocate(100));

      assertNull(
          "Deserialization did not detect log truncation! Record: "
              + cmd
              + ", deserialized: "
              + deserialized,
          deserialized);
    }
  }
 private ByteBuffer loadResource(URL url) throws IOException {
   InputStream stream = null;
   try {
     stream = url.openStream();
     int initialBufferCapacity = Math.min(0x40000, stream.available() + 1);
     if (initialBufferCapacity <= 2) initialBufferCapacity = 0x10000;
     else initialBufferCapacity = Math.max(initialBufferCapacity, 0x200);
     ByteBuffer buf = ByteBuffer.allocate(initialBufferCapacity);
     while (true) {
       if (!buf.hasRemaining()) {
         ByteBuffer newBuf = ByteBuffer.allocate(2 * buf.capacity());
         buf.flip();
         newBuf.put(buf);
         buf = newBuf;
       }
       int len = stream.read(buf.array(), buf.position(), buf.remaining());
       if (len <= 0) break;
       buf.position(buf.position() + len);
     }
     buf.flip();
     return buf;
   } finally {
     if (stream != null) stream.close();
   }
 }
Пример #11
0
 /**
  * Writes the SOCKS "connect" bytes, differentiating between SOCKS 4 and 4A; the former sends an
  * IPv4 address, the latter the full domain name.
  */
 private void writeSocks4Connect() {
   HttpDestination destination =
       (HttpDestination) context.get(HttpClientTransport.HTTP_DESTINATION_CONTEXT_KEY);
   String host = destination.getHost();
   short port = (short) destination.getPort();
   Matcher matcher = IPv4_PATTERN.matcher(host);
   if (matcher.matches()) {
     // SOCKS 4
     ByteBuffer buffer = ByteBuffer.allocate(9);
     buffer.put((byte) 4).put((byte) 1).putShort(port);
     for (int i = 1; i <= 4; ++i) buffer.put((byte) Integer.parseInt(matcher.group(i)));
     buffer.put((byte) 0);
     buffer.flip();
     getEndPoint().write(this, buffer);
   } else {
     // SOCKS 4A
     byte[] hostBytes = host.getBytes(StandardCharsets.UTF_8);
     ByteBuffer buffer = ByteBuffer.allocate(9 + hostBytes.length + 1);
     buffer.put((byte) 4).put((byte) 1).putShort(port);
     buffer.put((byte) 0).put((byte) 0).put((byte) 0).put((byte) 1).put((byte) 0);
     buffer.put(hostBytes).put((byte) 0);
     buffer.flip();
     getEndPoint().write(this, buffer);
   }
 }
Пример #12
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();
  }
Пример #13
0
 private DecimalType extractStateFromRegisters(InputRegister[] registers, int index, String type) {
   if (type.equals(ModbusBindingProvider.VALUE_TYPE_BIT)) {
     return new DecimalType((registers[index / 16].toUnsignedShort() >> (index % 16)) & 1);
   } else if (type.equals(ModbusBindingProvider.VALUE_TYPE_INT8)) {
     return new DecimalType(registers[index / 2].toBytes()[1 - (index % 2)]);
   } else if (type.equals(ModbusBindingProvider.VALUE_TYPE_UINT8)) {
     return new DecimalType((registers[index / 2].toUnsignedShort() >> (8 * (index % 2))) & 0xff);
   } else if (type.equals(ModbusBindingProvider.VALUE_TYPE_INT16)) {
     ByteBuffer buff = ByteBuffer.allocate(2);
     buff.put(registers[index].toBytes());
     return new DecimalType(buff.order(ByteOrder.BIG_ENDIAN).getShort(0));
   } else if (type.equals(ModbusBindingProvider.VALUE_TYPE_UINT16)) {
     return new DecimalType(registers[index].toUnsignedShort());
   } else if (type.equals(ModbusBindingProvider.VALUE_TYPE_INT32)) {
     ByteBuffer buff = ByteBuffer.allocate(4);
     buff.put(registers[index * 2 + 0].toBytes());
     buff.put(registers[index * 2 + 1].toBytes());
     return new DecimalType(buff.order(ByteOrder.BIG_ENDIAN).getInt(0));
   } else if (type.equals(ModbusBindingProvider.VALUE_TYPE_UINT32)) {
     ByteBuffer buff = ByteBuffer.allocate(8);
     buff.position(4);
     buff.put(registers[index * 2 + 0].toBytes());
     buff.put(registers[index * 2 + 1].toBytes());
     return new DecimalType(buff.order(ByteOrder.BIG_ENDIAN).getLong(0));
   } else if (type.equals(ModbusBindingProvider.VALUE_TYPE_FLOAT32)) {
     ByteBuffer buff = ByteBuffer.allocate(4);
     buff.put(registers[index * 2 + 0].toBytes());
     buff.put(registers[index * 2 + 1].toBytes());
     return new DecimalType(buff.order(ByteOrder.BIG_ENDIAN).getFloat(0));
   } else {
     throw new IllegalArgumentException();
   }
 }
Пример #14
0
  private void calculate() throws UnknownHostException {

    ByteBuffer maskBuffer;
    int targetSize;
    if (inetAddress.getAddress().length == 4) {
      maskBuffer = ByteBuffer.allocate(4).putInt(-1);
      targetSize = 4;
    } else {
      maskBuffer = ByteBuffer.allocate(16).putLong(-1L).putLong(-1L);
      targetSize = 16;
    }

    BigInteger mask = (new BigInteger(1, maskBuffer.array())).not().shiftRight(prefixLength);

    ByteBuffer buffer = ByteBuffer.wrap(inetAddress.getAddress());
    BigInteger ipVal = new BigInteger(1, buffer.array());

    BigInteger startIp = ipVal.and(mask);
    BigInteger endIp = startIp.add(mask.not());

    byte[] startIpArr = toBytes(startIp.toByteArray(), targetSize);
    byte[] endIpArr = toBytes(endIp.toByteArray(), targetSize);

    this.startAddress = InetAddress.getByAddress(startIpArr);
    this.endAddress = InetAddress.getByAddress(endIpArr);
  }
Пример #15
0
  public static void main(String[] argv) throws Exception {
    Pipe[] pipes = new Pipe[PIPES_COUNT];
    Pipe pipe = Pipe.open();
    Pipe.SinkChannel sink = pipe.sink();
    Pipe.SourceChannel source = pipe.source();
    Selector sel = Selector.open();
    source.configureBlocking(false);
    source.register(sel, SelectionKey.OP_READ);

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i] = Pipe.open();
      Pipe.SourceChannel sc = pipes[i].source();
      sc.configureBlocking(false);
      sc.register(sel, SelectionKey.OP_READ);
      Pipe.SinkChannel sc2 = pipes[i].sink();
      sc2.configureBlocking(false);
      sc2.register(sel, SelectionKey.OP_WRITE);
    }

    for (int i = 0; i < LOOPS; i++) {
      sink.write(ByteBuffer.allocate(BUF_SIZE));
      int x = sel.selectNow();
      sel.selectedKeys().clear();
      source.read(ByteBuffer.allocate(BUF_SIZE));
    }

    for (int i = 0; i < PIPES_COUNT; i++) {
      pipes[i].sink().close();
      pipes[i].source().close();
    }
    pipe.sink().close();
    pipe.source().close();
    sel.close();
  }
Пример #16
0
 @Test
 public void testReadAndWriteCaptureChannels() throws IOException {
   String blobName = "test-read-and-write-capture-channels-blob";
   BlobInfo blob = BlobInfo.builder(BUCKET, blobName).build();
   byte[] stringBytes;
   BlobWriteChannel writer = storage.writer(blob);
   stringBytes = BLOB_STRING_CONTENT.getBytes(UTF_8);
   writer.write(ByteBuffer.wrap(BLOB_BYTE_CONTENT));
   RestorableState<BlobWriteChannel> writerState = writer.capture();
   BlobWriteChannel secondWriter = writerState.restore();
   secondWriter.write(ByteBuffer.wrap(stringBytes));
   secondWriter.close();
   ByteBuffer readBytes;
   ByteBuffer readStringBytes;
   BlobReadChannel reader = storage.reader(blob.blobId());
   reader.chunkSize(BLOB_BYTE_CONTENT.length);
   readBytes = ByteBuffer.allocate(BLOB_BYTE_CONTENT.length);
   reader.read(readBytes);
   RestorableState<BlobReadChannel> readerState = reader.capture();
   BlobReadChannel secondReader = readerState.restore();
   readStringBytes = ByteBuffer.allocate(stringBytes.length);
   secondReader.read(readStringBytes);
   reader.close();
   secondReader.close();
   assertArrayEquals(BLOB_BYTE_CONTENT, readBytes.array());
   assertEquals(BLOB_STRING_CONTENT, new String(readStringBytes.array(), UTF_8));
   assertTrue(storage.delete(BUCKET, blobName));
 }
Пример #17
0
  public StreamEngine(SocketChannel fd_, final Options options_, final String endpoint_) {
    handle = fd_;
    inbuf = null;
    insize = 0;
    io_enabled = false;
    outbuf = null;
    outsize = 0;
    handshaking = true;
    session = null;
    options = options_;
    plugged = false;
    terminating = false;
    endpoint = endpoint_;
    socket = null;
    greeting = ByteBuffer.allocate(GREETING_SIZE);
    greeting_output_buffer = ByteBuffer.allocate(GREETING_SIZE);
    encoder = null;
    decoder = null;

    //  Put the socket into non-blocking mode.
    try {
      Utils.unblock_socket(handle);

      //  Set the socket buffer limits for the underlying socket.
      if (options.sndbuf != 0) {
        handle.socket().setSendBufferSize((int) options.sndbuf);
      }
      if (options.rcvbuf != 0) {
        handle.socket().setReceiveBufferSize((int) options.rcvbuf);
      }

    } catch (IOException e) {
      throw new ZError.IOException(e);
    }
  }
Пример #18
0
  @Test
  public void testGenerate125ByteBinaryCase1_2_2() {
    int length = 125;

    ByteBuffer bb = ByteBuffer.allocate(length);

    for (int i = 0; i < length; ++i) {
      bb.put("*".getBytes());
    }

    bb.flip();

    WebSocketFrame binaryFrame = WebSocketFrame.binary().setPayload(bb);

    Generator generator = new UnitGenerator();
    ByteBuffer actual = generator.generate(binaryFrame);

    ByteBuffer expected = ByteBuffer.allocate(length + 5);

    expected.put(new byte[] {(byte) 0x82});

    byte b = 0x00; // no masking
    b |= length & 0x7F;
    expected.put(b);

    for (int i = 0; i < length; ++i) {
      expected.put("*".getBytes());
    }

    BufferUtil.flipToFlush(expected, 0);

    ByteBufferAssert.assertEquals("buffers do not match", expected, actual);
  }
Пример #19
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;
  }
Пример #20
0
 // Read a frame containing a piece of the message body
 final void readNextFrame(ByteBuffer frame, int flags) {
   this.flags = flags;
   ByteBuffer body = this.body;
   if (this.isCompressed) {
     GZIPInputStream gzip = (GZIPInputStream) this.gzipIn;
     // gzip.
   } else {
     if (frame.remaining() > body.remaining()) {
       ByteBuffer newBuffer;
       int neededSize = body.position() + frame.remaining();
       if ((flags & MORECOMING) == 0) {
         newBuffer = ByteBuffer.allocate(neededSize);
       } else {
         int capacity = body.capacity();
         while (capacity < neededSize) capacity <<= 1;
         newBuffer = ByteBuffer.allocate(capacity);
       }
       newBuffer.put((ByteBuffer) body.limit(body.position()).rewind()).put(frame);
       this.body = newBuffer;
     } else {
       body.put(frame);
       if ((flags & MORECOMING) == 0) body.limit(body.position()).rewind();
     }
   }
 }
Пример #21
0
 private void insertRecord(int recordPosition, long value) throws IOException {
   try {
     FileChannel channel = getFileChannel();
     long previousPosition = channel.position();
     channel.position(RECORD_SIZE * recordPosition);
     int trail = (int) (channel.size() - channel.position());
     ByteBuffer trailBuffer = null;
     if (trail > 0) {
       trailBuffer = ByteBuffer.allocate(trail);
       channel.read(trailBuffer);
       trailBuffer.flip();
     }
     ByteBuffer buffer = ByteBuffer.allocate(RECORD_SIZE);
     buffer.put(Record.IN_USE.byteValue());
     buffer.putLong(value);
     buffer.flip();
     channel.position(RECORD_SIZE * recordPosition);
     channel.write(buffer);
     if (trail > 0) {
       channel.write(trailBuffer);
     }
     channel.position(previousPosition);
   } catch (IOException e) {
     throw new RuntimeException(e);
   }
 }
Пример #22
0
  @Override
  public List<Framedata> translateFrame(ByteBuffer buffer)
      throws LimitExedeedException, InvalidDataException {
    List<Framedata> frames = new LinkedList<Framedata>();
    Framedata cur;

    if (incompleteframe != null) {
      // complete an incomplete frame
      while (true) {
        try {
          buffer.mark();
          int available_next_byte_count = buffer.remaining(); // The number of bytes received
          int expected_next_byte_count =
              incompleteframe.remaining(); // The number of bytes to complete the incomplete frame

          if (expected_next_byte_count > available_next_byte_count) {
            // did not receive enough bytes to complete the frame
            incompleteframe.put(buffer.array(), buffer.position(), available_next_byte_count);
            buffer.position(buffer.position() + available_next_byte_count);
            return Collections.emptyList();
          }
          incompleteframe.put(buffer.array(), buffer.position(), expected_next_byte_count);
          buffer.position(buffer.position() + expected_next_byte_count);

          cur = translateSingleFrame((ByteBuffer) incompleteframe.duplicate().position(0));
          frames.add(cur);
          incompleteframe = null;
          break; // go on with the normal frame receival
        } catch (IncompleteException e) {
          // extending as much as suggested
          @SuppressWarnings("unused")
          int oldsize = incompleteframe.limit();
          ByteBuffer extendedframe = ByteBuffer.allocate(checkAlloc(e.getPreferedSize()));
          assert (extendedframe.limit() > incompleteframe.limit());
          incompleteframe.rewind();
          extendedframe.put(incompleteframe);
          incompleteframe = extendedframe;

          return translateFrame(buffer);
        }
      }
    }

    while (buffer.hasRemaining()) { // Read as much as possible full frames
      buffer.mark();
      try {
        cur = translateSingleFrame(buffer);
        frames.add(cur);
      } catch (IncompleteException e) {
        // remember the incomplete data
        buffer.reset();
        int pref = e.getPreferedSize();
        incompleteframe = ByteBuffer.allocate(checkAlloc(pref));
        incompleteframe.put(buffer);
        break;
      }
    }
    return frames;
  }
Пример #23
0
  @Override
  public void establish() throws IOException {
    discoverySelector = Selector.open();
    serverSocketChannel.register(discoverySelector, SelectionKey.OP_ACCEPT);
    int slaveCount = 0;
    while (slaveCount < slaves.length) {
      log.info("Awaiting registration from " + (slaves.length - slaveCount) + " slaves.");
      discoverySelector.select();
      Set<SelectionKey> keySet = discoverySelector.selectedKeys();
      Iterator<SelectionKey> it = keySet.iterator();
      while (it.hasNext()) {
        SelectionKey selectionKey = it.next();
        it.remove();
        if (!selectionKey.isValid()) {
          continue;
        }
        ServerSocketChannel srvSocketChannel = (ServerSocketChannel) selectionKey.channel();
        SocketChannel socketChannel = srvSocketChannel.accept();

        int slaveIndex = readInt(socketChannel);
        if (slaveIndex < 0) {
          for (int i = 0; i < slaves.length; ++i) {
            if (slaves[i] == null) {
              slaveIndex = i;
              break;
            }
          }
        } else if (slaveIndex >= slaves.length) {
          throw new IllegalArgumentException(
              "Slave requests invalid slaveIndex "
                  + slaveIndex
                  + " (expected "
                  + slaves.length
                  + " slaves)");

        } else if (slaves[slaveIndex] != null) {
          throw new IllegalArgumentException(
              "Slave requests slaveIndex "
                  + slaveIndex
                  + " but this was already assigned to "
                  + slaves[slaveIndex].getRemoteAddress());
        }
        writeInt(socketChannel, slaveIndex);
        writeInt(socketChannel, slaves.length);
        slaves[slaveIndex] = socketChannel;
        slaveCount++;
        slave2Index.put(socketChannel, slaveIndex);
        this.readBufferMap.put(socketChannel, ByteBuffer.allocate(DEFAULT_READ_BUFF_CAPACITY));
        socketChannel.configureBlocking(false);
        log.trace(
            "Added new slave connection "
                + slaveIndex
                + " from: "
                + socketChannel.socket().getInetAddress());
      }
    }
    mcastBuffer = ByteBuffer.allocate(DEFAULT_WRITE_BUFF_CAPACITY);
    log.info("Connection established from " + slaveCount + " slaves.");
  }
 public FrameSpiller(FrameDistributor distributor, FeedId feedId, int frameSize)
     throws IOException {
   this.feedId = feedId;
   this.frameDistributor = distributor;
   reusableLengthBuffer = ByteBuffer.allocate(4);
   reusableDataBuffer = ByteBuffer.allocate(frameSize);
   this.offset = 0;
 }
Пример #25
0
  @Test
  public void testSomeContentAvailableAfterServiceReturns() throws Exception {
    final AtomicInteger count = new AtomicInteger();
    start(
        new HttpServlet() {
          @Override
          protected void service(final HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
            final AsyncContext asyncContext = request.startAsync();
            asyncContext.setTimeout(0);
            request
                .getInputStream()
                .setReadListener(
                    new EmptyReadListener() {
                      @Override
                      public void onDataAvailable() throws IOException {
                        count.incrementAndGet();
                        ServletInputStream input = request.getInputStream();
                        while (input.isReady()) {
                          int read = input.read();
                          if (read < 0) break;
                        }
                        if (input.isFinished()) asyncContext.complete();
                      }
                    });
          }
        });

    Session session = newClient(new Session.Listener.Adapter());

    HttpFields fields = new HttpFields();
    MetaData.Request metaData = newRequest("GET", fields);
    HeadersFrame frame = new HeadersFrame(metaData, null, false);
    final CountDownLatch latch = new CountDownLatch(1);
    FuturePromise<Stream> promise = new FuturePromise<>();
    session.newStream(
        frame,
        promise,
        new Stream.Listener.Adapter() {
          @Override
          public void onHeaders(Stream stream, HeadersFrame frame) {
            if (frame.isEndStream()) latch.countDown();
          }
        });
    Stream stream = promise.get(5, TimeUnit.SECONDS);

    // Wait until service() returns.
    Thread.sleep(1000);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), false), Callback.NOOP);

    // Wait until onDataAvailable() returns.
    Thread.sleep(1000);
    stream.data(new DataFrame(stream.getId(), ByteBuffer.allocate(1), true), Callback.NOOP);

    Assert.assertTrue(latch.await(5, TimeUnit.SECONDS));
    // Make sure onDataAvailable() has been called twice
    Assert.assertEquals(2, count.get());
  }
Пример #26
0
 public void init() {
   if (State.STOPPED != _state) return;
   _state = State.STARTING;
   readBuffer = ByteBuffer.allocate(bufLength);
   sendBuffer = ByteBuffer.allocate(bufLength);
   peerAddr = hostIp + ":" + hostPort;
   reader = new ReadThread();
   reader.start();
 }
Пример #27
0
 @Override
 public ByteVector dispatch(Player player) {
   if (player.getUpdateFlags().contains("map-region")) {
     player.send(new RegionalUpdatePacket());
   }
   ByteVector packet = new ByteVector(ByteBuffer.allocate(16384));
   ByteVector update = new ByteVector(ByteBuffer.allocate(8192));
   packet.writeVariableShortPacketHeader(player.getConnection().getSecureWrite(), getOpcode());
   packet.startBitAccess();
   player.getListener().updateThisPlayerMovement(player, packet);
   if (player.isUpdateRequired()) {
     player.getListener().updatePlayerState(player, update, false, true);
   }
   packet.writeBits(8, player.getLocalPlayers().size());
   for (Iterator<Player> iterator = player.getLocalPlayers().iterator(); iterator.hasNext(); ) {
     Player otherPlayer = iterator.next();
     if (World.getInstance().getPlayers().containsValue(otherPlayer)
         && otherPlayer.getPosition().isWithinDistance(player.getPosition())
         && !otherPlayer.getUpdateFlags().contains("teleporting")) {
       player.getListener().updateOtherPlayerMovement(otherPlayer, packet);
       if (otherPlayer.isUpdateRequired()) {
         player.getListener().updatePlayerState(otherPlayer, update, false, false);
       }
     } else {
       iterator.remove();
       packet.writeBits(1, 1);
       packet.writeBits(2, 3);
     }
   }
   for (Player otherPlayer : World.getInstance().getPlayers().values()) {
     if (otherPlayer.getLocalPlayers().size() >= 255) {
       break;
     }
     if (otherPlayer == player || player.getLocalPlayers().contains(otherPlayer)) {
       continue;
     }
     if (otherPlayer.getPosition().isWithinDistance(player.getPosition())) {
       player.getLocalPlayers().add(otherPlayer);
       packet.writeBits(11, otherPlayer.getIndex());
       packet.writeBits(1, 1);
       packet.writeBits(1, 1);
       packet.writeBits(5, otherPlayer.getPosition().getY() - player.getPosition().getY());
       packet.writeBits(5, otherPlayer.getPosition().getX() - player.getPosition().getX());
       player.getListener().updatePlayerState(otherPlayer, update, true, false);
     }
   }
   if (update.getBuffer().hasRemaining()) {
     packet.writeBits(11, 2047);
     packet.startByteAcces();
     packet.writeBytes(update.getBuffer());
   } else {
     packet.startByteAcces();
   }
   packet.finishVariableShortPacketHeader();
   return packet;
 }
Пример #28
0
  public QueueIndexUpdate batchUpdateQueueIndex(
      QueueIndexUpdate indexUpdate, UUID subcriptionQueueId) throws Exception {

    logger.info("batchUpdateQueueIndex");

    Mutator<ByteBuffer> batch = indexUpdate.getBatch();

    // queue_id,prop_name
    Object index_key = key(subcriptionQueueId, indexUpdate.getEntryName());

    // subscription_queue_id,subscriber_queue_id,prop_name

    for (QueueIndexEntry entry : indexUpdate.getPrevEntries()) {

      if (entry.getValue() != null) {

        index_key = key(subcriptionQueueId, entry.getPath());

        batch.addDeletion(
            bytebuffer(index_key),
            PROPERTY_INDEX.getColumnFamily(),
            entry.getIndexComposite(),
            dce,
            indexUpdate.getTimestamp());
      } else {
        logger.error("Unexpected condition - deserialized property value is null");
      }
    }

    if (indexUpdate.getNewEntries().size() > 0) {

      for (QueueIndexEntry indexEntry : indexUpdate.getNewEntries()) {

        index_key = key(subcriptionQueueId, indexEntry.getPath());

        batch.addInsertion(
            bytebuffer(index_key),
            PROPERTY_INDEX.getColumnFamily(),
            createColumn(
                indexEntry.getIndexComposite(),
                ByteBuffer.allocate(0),
                indexUpdate.getTimestamp(),
                dce,
                be));
      }
    }

    for (String index : indexUpdate.getIndexesSet()) {
      batch.addInsertion(
          bytebuffer(key(subcriptionQueueId, DICTIONARY_SUBSCRIBER_INDEXES)),
          QUEUE_DICTIONARIES.getColumnFamily(),
          createColumn(index, ByteBuffer.allocate(0), indexUpdate.getTimestamp(), se, be));
    }

    return indexUpdate;
  }
  public RadSecClientTransport(KeyManager keyManagers[], TrustManager trustManagers[]) {
    this.keyManagers = keyManagers;
    this.trustManagers = trustManagers;

    buffer_in = ByteBuffer.allocate(25000);
    buffer_in.order(ByteOrder.BIG_ENDIAN);

    buffer_out = ByteBuffer.allocate(25000);
    buffer_out.order(ByteOrder.BIG_ENDIAN);
  }
Пример #30
0
 public SSLHandler(SSLEngine engine, SocketChannel channle, Selector selector)
     throws SSLException {
   this.engine = engine;
   this.channel = channle;
   this.selector = selector;
   this.appSendBuffer = ByteBuffer.allocate(engine.getSession().getApplicationBufferSize());
   this.netSendBuffer = ByteBuffer.allocate(engine.getSession().getPacketBufferSize());
   this.appRecvBuffer = ByteBuffer.allocate(engine.getSession().getApplicationBufferSize());
   this.netRecvBuffer = ByteBuffer.allocate(engine.getSession().getPacketBufferSize());
   engine.beginHandshake();
 }