public synchronized void write (Connection connection, ByteBuffer buffer, Object object) { byteBufferOutputStream.setByteBuffer(buffer); int start = buffer.position(); try { json.writeValue(object, Object.class, null); writer.flush(); } catch (Exception ex) { throw new JsonException("Error writing object: " + object, ex); } if (INFO && logging) { int end = buffer.position(); buffer.position(start); buffer.limit(end); int length = end - start; if (logBuffer.length < length) logBuffer = new byte[length]; buffer.get(logBuffer, 0, length); buffer.position(end); buffer.limit(buffer.capacity()); String message = new String(logBuffer, 0, length); if (prettyPrint) message = json.prettyPrint(message); info("Wrote: " + message); } }
public void write (byte[] bytes, int offset, int length) throws IOException { if (byteBuffer.remaining() < length) flush(); byteBuffer.put(bytes, offset, length); } }
/** 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); } }
/** * Flushes the stream by taking the current {@link ByteBuffer} using for writing. Buffer is flip * and put to the list of cached buffers. If the takeNewBuffer is <code>true</code> then a new * buffer will be placed and ready for write. * * @param takeNewBuffer * If the new buffers should be placed the super {@link OutputStream}. * @exception IOException * if an I/O error occurs. */ public void flush(boolean takeNewBuffer) throws IOException { ByteBuffer fullBuffer = super.getByteBuffer(); fullBuffer.flip(); byteBuffers.add(fullBuffer); totalWriteSize += fullBuffer.limit() - fullBuffer.position(); if (takeNewBuffer) { ByteBuffer byteBuffer = byteBufferProvider.acquireByteBuffer(); byteBuffer.clear(); super.setByteBuffer(byteBuffer); } else { super.setByteBuffer(null); } }
/** * {@inheritDoc} */ @Override public void write(byte[] b, int off, int len) throws IOException { int remaining = super.getByteBuffer().remaining(); if (remaining >= len) { super.write(b, off, len); } else { super.write(b, off, remaining); this.flush(); this.write(b, off + remaining, len - remaining); } }
/** * Returns the total number of {@link ByteBuffer}s used including the one that might be used for * writing at the moment. * * @return Returns the total number of {@link ByteBuffer}s used including the one that might be * used for writing at the moment. */ public int getBuffersCount() { int count = byteBuffers.size(); ByteBuffer byteBuffer = super.getByteBuffer(); if (null != byteBuffer) { count++; } return count; }
bostr.close(); } catch (IOException e) {
public void write (byte[] bytes, int offset, int length) throws IOException { if (byteBuffer.remaining() < length) flush(); byteBuffer.put(bytes, offset, length); } }
/** * @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; } }
/** * Releases all buffers in the stream. */ private void releaseAllBuffers() { for (ByteBuffer byteBuffer : byteBuffers) { byteBufferProvider.releaseByteBuffer(byteBuffer); } byteBuffers.clear(); ByteBuffer currentBuffer = super.getByteBuffer(); if (null != currentBuffer) { byteBufferProvider.releaseByteBuffer(currentBuffer); super.setByteBuffer(null); } } }
/** * Returns the total writing size including the size that is currently available in the * ByteBuffer that is currently used for write. * * @return Returns the total writing size including the size that is currently available in the * ByteBuffer that is currently used for write. */ public long getTotalWriteSize() { long size = totalWriteSize; ByteBuffer byteBuffer = super.getByteBuffer(); if (null != byteBuffer) { size += byteBuffer.position(); } return size; }
public void write (byte[] bytes, int offset, int length) throws IOException { if (byteBuffer.remaining() < length) flush(); byteBuffer.put(bytes, offset, length); } }
/** * 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); }
/** * Prepares for the write. This method will get a {@link ByteBuffer} from the * {@link ByteBufferProvider} and set it as the current output. * * @throws IOException * if preparation fails due to inability to obtain a byte buffers */ public void prepare() throws IOException { releaseAllBuffers(); totalWriteSize = 0L; ByteBuffer byteBuffer = byteBufferProvider.acquireByteBuffer(); byteBuffer.clear(); super.setByteBuffer(byteBuffer); }
public void write (int b) throws IOException { if (!byteBuffer.hasRemaining()) flush(); byteBuffer.put((byte)b); }
/** * 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); }
public void write (Connection connection, ByteBuffer buffer, Object object) { byteBufferOutputStream.setByteBuffer(buffer); int start = buffer.position(); try { json.writeValue(object, Object.class, null); writer.flush(); } catch (Exception ex) { throw new JsonException("Error writing object: " + object, ex); } if (INFO && logging) { int end = buffer.position(); buffer.position(start); buffer.limit(end); int length = end - start; if (logBuffer.length < length) logBuffer = new byte[length]; buffer.get(logBuffer, 0, length); buffer.position(end); buffer.limit(buffer.capacity()); String message = new String(logBuffer, 0, length); if (prettyPrint) message = json.prettyPrint(message); info("Wrote: " + message); } }
public void write (byte[] bytes, int offset, int length) throws IOException { if (byteBuffer.remaining() < length) flush(); byteBuffer.put(bytes, offset, length); } }
public void write (int b) throws IOException { if (!byteBuffer.hasRemaining()) flush(); byteBuffer.put((byte)b); }