Example usage for org.apache.lucene.store OutputStreamDataOutput writeInt

List of usage examples for org.apache.lucene.store OutputStreamDataOutput writeInt

Introduction

In this page you can find the example usage for org.apache.lucene.store OutputStreamDataOutput writeInt.

Prototype

public void writeInt(int i) throws IOException 

Source Link

Document

Writes an int as four bytes.

Usage

From source file:org.elasticsearch.index.translog.TranslogWriter.java

License:Apache License

public static TranslogWriter create(Type type, ShardId shardId, String translogUUID, long fileGeneration,
        Path file, Callback<ChannelReference> onClose, int bufferSize, ChannelFactory channelFactory)
        throws IOException {
    final BytesRef ref = new BytesRef(translogUUID);
    final int headerLength = CodecUtil.headerLength(TRANSLOG_CODEC) + ref.length
            + RamUsageEstimator.NUM_BYTES_INT;
    final FileChannel channel = channelFactory.open(file);
    try {/*from w w  w. j ava  2  s .c  o m*/
        // This OutputStreamDataOutput is intentionally not closed because
        // closing it will close the FileChannel
        final OutputStreamDataOutput out = new OutputStreamDataOutput(
                java.nio.channels.Channels.newOutputStream(channel));
        CodecUtil.writeHeader(out, TRANSLOG_CODEC, VERSION);
        out.writeInt(ref.length);
        out.writeBytes(ref.bytes, ref.offset, ref.length);
        channel.force(false);
        writeCheckpoint(headerLength, 0, file.getParent(), fileGeneration, StandardOpenOption.WRITE);
        final TranslogWriter writer = type.create(shardId, fileGeneration,
                new ChannelReference(file, fileGeneration, channel, onClose), bufferSize);
        return writer;
    } catch (Throwable throwable) {
        IOUtils.closeWhileHandlingException(channel);
        try {
            Files.delete(file); // remove the file as well
        } catch (IOException ex) {
            throwable.addSuppressed(ex);
        }
        throw throwable;
    }
}