当前位置: 首页>>代码示例>>Java>>正文


Java StreamInput.readFully方法代码示例

本文整理汇总了Java中org.elasticsearch.common.io.stream.StreamInput.readFully方法的典型用法代码示例。如果您正苦于以下问题:Java StreamInput.readFully方法的具体用法?Java StreamInput.readFully怎么用?Java StreamInput.readFully使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.elasticsearch.common.io.stream.StreamInput的用法示例。


在下文中一共展示了StreamInput.readFully方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testStreamInputBulkReadWithOffset

import org.elasticsearch.common.io.stream.StreamInput; //导入方法依赖的package包/类
public void testStreamInputBulkReadWithOffset() throws IOException {
    final int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    StreamInput si = pbr.streamInput();
    assertNotNull(si);

    // read a bunch of single bytes one by one
    int offset = randomIntBetween(1, length / 2);
    for (int i = 0; i < offset; i++) {
        assertEquals(si.available(), length - i);
        assertEquals(pbr.get(i), si.readByte());
    }

    // now do NOT reset the stream - keep the stream's offset!

    // buffer to compare remaining bytes against bulk read
    byte[] pbrBytesWithOffset = Arrays.copyOfRange(BytesReference.toBytes(pbr), offset, length);
    // randomized target buffer to ensure no stale slots
    byte[] targetBytes = new byte[pbrBytesWithOffset.length];
    random().nextBytes(targetBytes);

    // bulk-read all
    si.readFully(targetBytes);
    assertArrayEquals(pbrBytesWithOffset, targetBytes);
    assertEquals(si.available(), 0);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:27,代码来源:AbstractBytesReferenceTestCase.java

示例2: TransportAddress

import org.elasticsearch.common.io.stream.StreamInput; //导入方法依赖的package包/类
/**
 * Read from a stream and use the {@code hostString} when creating the InetAddress if the input comes from a version prior
 * {@link Version#V_5_0_3_UNRELEASED} as the hostString was not serialized
 */
public TransportAddress(StreamInput in, @Nullable String hostString) throws IOException {
    if (in.getVersion().before(Version.V_6_0_0_alpha1_UNRELEASED)) { // bwc layer for 5.x where we had more than one transport address
        final short i = in.readShort();
        if(i != 1) { // we fail hard to ensure nobody tries to use some custom transport address impl even if that is difficult to add
            throw new AssertionError("illegal transport ID from node of version: " + in.getVersion()  + " got: " + i + " expected: 1");
        }
    }
    final int len = in.readByte();
    final byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6)
    in.readFully(a);
    final InetAddress inetAddress;
    if (in.getVersion().onOrAfter(Version.V_5_0_3_UNRELEASED)) {
        String host = in.readString(); // the host string was serialized so we can ignore the passed in version
        inetAddress = InetAddress.getByAddress(host, a);
    } else {
        // prior to this version, we did not serialize the host string so we used the passed in version
        inetAddress = InetAddress.getByAddress(hostString, a);
    }
    int port = in.readInt();
    this.address = new InetSocketAddress(inetAddress, port);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:26,代码来源:TransportAddress.java

示例3: readMessage

import org.elasticsearch.common.io.stream.StreamInput; //导入方法依赖的package包/类
private void readMessage(MockChannel mockChannel, StreamInput input) throws IOException {
    Socket socket = mockChannel.activeChannel;
    byte[] minimalHeader = new byte[TcpHeader.MARKER_BYTES_SIZE];
    int firstByte = input.read();
    if (firstByte == -1) {
        throw new IOException("Connection reset by peer");
    }
    minimalHeader[0] = (byte) firstByte;
    minimalHeader[1] = (byte) input.read();
    int msgSize = input.readInt();
    if (msgSize == -1) {
        socket.getOutputStream().flush();
    } else {
        BytesStreamOutput output = new BytesStreamOutput();
        final byte[] buffer = new byte[msgSize];
        input.readFully(buffer);
        output.write(minimalHeader);
        output.writeInt(msgSize);
        output.write(buffer);
        final BytesReference bytes = output.bytes();
        if (TcpTransport.validateMessageHeader(bytes)) {
            InetSocketAddress remoteAddress = (InetSocketAddress) socket.getRemoteSocketAddress();
            messageReceived(bytes.slice(TcpHeader.MARKER_BYTES_SIZE + TcpHeader.MESSAGE_LENGTH_SIZE, msgSize),
                mockChannel, mockChannel.profile, remoteAddress, msgSize);
        } else {
            // ping message - we just drop all stuff
        }
    }
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:30,代码来源:MockTcpTransport.java

示例4: InetSocketTransportAddress

import org.elasticsearch.common.io.stream.StreamInput; //导入方法依赖的package包/类
public InetSocketTransportAddress(StreamInput in) throws IOException {
    final int len = in.readByte();
    final byte[] a = new byte[len]; // 4 bytes (IPv4) or 16 bytes (IPv6)
    in.readFully(a);
    InetAddress inetAddress = InetAddress.getByAddress(a);
    int port = in.readInt();
    this.address = new InetSocketAddress(inetAddress, port);
}
 
开发者ID:baidu,项目名称:Elasticsearch,代码行数:9,代码来源:InetSocketTransportAddress.java

示例5: testStreamInput

import org.elasticsearch.common.io.stream.StreamInput; //导入方法依赖的package包/类
public void testStreamInput() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);
    StreamInput si = pbr.streamInput();
    assertNotNull(si);

    // read single bytes one by one
    assertEquals(pbr.get(0), si.readByte());
    assertEquals(pbr.get(1), si.readByte());
    assertEquals(pbr.get(2), si.readByte());

    // reset the stream for bulk reading
    si.reset();

    // buffer for bulk reads
    byte[] origBuf = new byte[length];
    random().nextBytes(origBuf);
    byte[] targetBuf = Arrays.copyOf(origBuf, origBuf.length);

    // bulk-read 0 bytes: must not modify buffer
    si.readBytes(targetBuf, 0, 0);
    assertEquals(origBuf[0], targetBuf[0]);
    si.reset();

    // read a few few bytes as ints
    int bytesToRead = randomIntBetween(1, length / 2);
    for (int i = 0; i < bytesToRead; i++) {
        int b = si.read();
        assertEquals(pbr.get(i) & 0xff, b);
    }
    si.reset();

    // bulk-read all
    si.readFully(targetBuf);
    assertArrayEquals(BytesReference.toBytes(pbr), targetBuf);

    // continuing to read should now fail with EOFException
    try {
        si.readByte();
        fail("expected EOF");
    } catch (EOFException | IndexOutOfBoundsException eof) {
        // yay
    }

    // try to read more than the stream contains
    si.reset();
    expectThrows(IndexOutOfBoundsException.class, () ->
        si.readBytes(targetBuf, 0, length * 2));
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:50,代码来源:AbstractBytesReferenceTestCase.java

示例6: testSliceStreamInput

import org.elasticsearch.common.io.stream.StreamInput; //导入方法依赖的package包/类
public void testSliceStreamInput() throws IOException {
    int length = randomIntBetween(10, scaledRandomIntBetween(PAGE_SIZE * 2, PAGE_SIZE * 20));
    BytesReference pbr = newBytesReference(length);

    // test stream input over slice (upper half of original)
    int sliceOffset = randomIntBetween(1, length / 2);
    int sliceLength = length - sliceOffset;
    BytesReference slice = pbr.slice(sliceOffset, sliceLength);
    StreamInput sliceInput = slice.streamInput();
    assertEquals(sliceInput.available(), sliceLength);

    // single reads
    assertEquals(slice.get(0), sliceInput.readByte());
    assertEquals(slice.get(1), sliceInput.readByte());
    assertEquals(slice.get(2), sliceInput.readByte());
    assertEquals(sliceInput.available(), sliceLength - 3);

    // reset the slice stream for bulk reading
    sliceInput.reset();
    assertEquals(sliceInput.available(), sliceLength);

    // bulk read
    byte[] sliceBytes = new byte[sliceLength];
    sliceInput.readFully(sliceBytes);
    assertEquals(sliceInput.available(), 0);

    // compare slice content with upper half of original
    byte[] pbrSliceBytes = Arrays.copyOfRange(BytesReference.toBytes(pbr), sliceOffset, length);
    assertArrayEquals(pbrSliceBytes, sliceBytes);

    // compare slice bytes with bytes read from slice via streamInput :D
    byte[] sliceToBytes = BytesReference.toBytes(slice);
    assertEquals(sliceBytes.length, sliceToBytes.length);
    assertArrayEquals(sliceBytes, sliceToBytes);

    sliceInput.reset();
    assertEquals(sliceInput.available(), sliceLength);
    byte[] buffer = new byte[sliceLength + scaledRandomIntBetween(1, 100)];
    int offset = scaledRandomIntBetween(0, Math.max(1, buffer.length - sliceLength - 1));
    int read = sliceInput.read(buffer, offset, sliceLength / 2);
    assertEquals(sliceInput.available(), sliceLength - read);
    sliceInput.read(buffer, offset + read, sliceLength - read);
    assertArrayEquals(sliceBytes, Arrays.copyOfRange(buffer, offset, offset + sliceLength));
    assertEquals(sliceInput.available(), 0);
}
 
开发者ID:justor,项目名称:elasticsearch_my,代码行数:46,代码来源:AbstractBytesReferenceTestCase.java


注:本文中的org.elasticsearch.common.io.stream.StreamInput.readFully方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。