Java Code Examples for org.elasticsearch.common.io.Streams#readFully()

The following examples show how to use org.elasticsearch.common.io.Streams#readFully() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChecksumBlobStoreFormat.java    From crate with Apache License 2.0 6 votes vote down vote up
/**
 * Reads blob with specified name without resolving the blobName using using {@link #blobName} method.
 *
 * @param blobContainer blob container
 * @param blobName blob name
 */
public T readBlob(BlobContainer blobContainer, String blobName) throws IOException {
    final BytesReference bytes = Streams.readFully(blobContainer.readBlob(blobName));
    final String resourceDesc = "ChecksumBlobStoreFormat.readBlob(blob=\"" + blobName + "\")";
    try (ByteArrayIndexInput indexInput =
             new ByteArrayIndexInput(resourceDesc, BytesReference.toBytes(bytes))) {
        CodecUtil.checksumEntireFile(indexInput);
        CodecUtil.checkHeader(indexInput, codec, VERSION, VERSION);
        long filePointer = indexInput.getFilePointer();
        long contentSize = indexInput.length() - CodecUtil.footerLength() - filePointer;
        try (XContentParser parser = XContentHelper.createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE,
                                                                 bytes.slice((int) filePointer, (int) contentSize), XContentType.SMILE)) {
            return reader.apply(parser);
        }
    } catch (CorruptIndexException | IndexFormatTooOldException | IndexFormatTooNewException ex) {
        // we trick this into a dedicated exception with the original stacktrace
        throw new CorruptStateException(ex);
    }
}
 
Example 2
Source File: NumericTokenizer.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public final boolean incrementToken() throws IOException {
    if (!started) {
        // reset() must be idempotent, this is why we read data in incrementToken
        final int len = Streams.readFully(input, buffer);
        if (len == buffer.length && input.read() != -1) {
            throw new IOException("Cannot read numeric data larger than " + buffer.length + " chars");
        }
        setValue(numericTokenStream, new String(buffer, 0, len));
        numericTokenStream.reset();
        started = true;
    }
    return numericTokenStream.incrementToken();
}
 
Example 3
Source File: XContentFactory.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Guesses the content type based on the provided input stream without consuming it.
 */
public static XContentType xContentType(InputStream si) throws IOException {
    if (si.markSupported() == false) {
        throw new IllegalArgumentException("Cannot guess the xcontent type without mark/reset support on " + si.getClass());
    }
    si.mark(GUESS_HEADER_LENGTH);
    try {
        final byte[] firstBytes = new byte[GUESS_HEADER_LENGTH];
        final int read = Streams.readFully(si, firstBytes);
        return xContentType(new BytesArray(firstBytes, 0, read));
    } finally {
        si.reset();
    }
}
 
Example 4
Source File: InputStreamStreamInput.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
    if (len < 0)
        throw new IndexOutOfBoundsException();
    final int read = Streams.readFully(is, b, offset, len);
    if (read != len) {
        throw new EOFException();
    }
}
 
Example 5
Source File: FSABuilder.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public FSA load(DataInputStream inputStream) throws IOException {
    this.size = inputStream.readInt();
    this.epsilon = inputStream.readInt();
    this.serialized = new byte[this.size];
    try {
        Streams.readFully(inputStream, serialized);
    } finally {
        inputStream.close();
    }
    final FSA fsa = new ConstantArcSizeFSA(Arrays.copyOf(this.serialized, this.size), this.epsilon);
    this.serialized = null;
    return fsa;
}
 
Example 6
Source File: InputStreamStreamInput.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void readBytes(byte[] b, int offset, int len) throws IOException {
    if (len < 0)
        throw new IndexOutOfBoundsException();
    final int read = Streams.readFully(is, b, offset, len);
    if (read != len) {
        throw new EOFException();
    }
}