/** This method produces a lot of garbage. Use it wisely! */ public static int calculateObjectSize(Object object, int bufferSize) { Kryo kryo = new Kryo(); ByteBufferOutputStream bbos = new ByteBufferOutputStream(bufferSize); Output output = new Output(bbos, bufferSize); kryo.writeClassAndObject(output, object); output.flush(); output.close(); return bbos.getByteBuffer().position(); }
public void openForWriting() throws IOException { OpenedFile of = new OpenedFile(this, Long.MIN_VALUE, Long.MAX_VALUE, false); memoryMappedFile = new RandomAccessFile(file, "rw"); out = memoryMappedFile.getChannel().map(MapMode.READ_WRITE, 0, FILE_CHUNK_SIZE); out.position(limit); out.putInt(LIMIT_POSITION, FIRST_RECORD_POSITION); bostr = new ByteBufferOutputStream(out); output = new Output(bostr); kryo = SerializeUtil.createKryo(); MonitoringData md = null; Output o = new Output(nullOut); while ((md = of.pop()) != null) { kryo.writeClassAndObject(o, md); } }
/** * @param file * @throws IOException */ public TargetFile(File file, boolean readOnly) throws IOException { this.file = file; if (!readOnly) { memoryMappedFile = new RandomAccessFile(file, "rw"); out = memoryMappedFile.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, FILE_CHUNK_SIZE); fileSize = FILE_CHUNK_SIZE; limit = FIRST_RECORD_POSITION; out.putLong(LIMIT_POSITION, limit); out.putLong(EARLIEST_POSITION, earliestTimestamp); out.putLong(LATEST_POSITION, latestTimestamp); out.position(FIRST_RECORD_POSITION); bostr = new ByteBufferOutputStream(out); output = new Output(bostr); } else { memoryMappedFile = new RandomAccessFile(file, "r"); fileSize = (int) file.length(); MappedByteBuffer bb = memoryMappedFile.getChannel().map(MapMode.READ_ONLY, 0, HEADER_END); earliestTimestamp = bb.getLong(EARLIEST_POSITION); latestTimestamp = bb.getLong(LATEST_POSITION); limit = bb.getInt(LIMIT_POSITION); memoryMappedFile.close(); memoryMappedFile = null; } }
/** * Performs the serialization of the given object to bytes and then performs de-serialization * from those bytes and returns the de-serialized object back. * * @param original * Original object. * @return De-serialized objects from bytes gotten from the serialization of original. * @throws SerializationException * If serialization fails. */ @SuppressWarnings("unchecked") private <T> T serializeBackAndForth(Object original) throws SerializationException { ByteBufferOutputStream byteBufferOutputStream = new ByteBufferOutputStream(byteBuffer); Output output = new Output(byteBufferOutputStream); serializer.serialize(original, output); byteBuffer.flip(); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(byteBuffer); Input input = new Input(byteBufferInputStream); return (T) serializer.deserialize(input); }
/** * Performs the serialization of the given object to bytes and then performs de-serialization * from those bytes and returns the de-serialized object back. * * @param original * Original object. * @return De-serialized objects from bytes gotten from the serialization of original. * @throws SerializationException * If serialization fails. */ @SuppressWarnings("unchecked") private <T> T serializeBackAndForth(Object original) throws SerializationException { ByteBufferOutputStream byteBufferOutputStream = new ByteBufferOutputStream(byteBuffer); Output output = new Output(byteBufferOutputStream); serializer.serialize(original, output); byteBuffer.flip(); ByteBufferInputStream byteBufferInputStream = new ByteBufferInputStream(byteBuffer); Input input = new Input(byteBufferInputStream); return (T) serializer.deserialize(input); }