Search in sources :

Example 6 with IByteBuffer

use of de.invesdwin.util.streams.buffer.bytes.IByteBuffer in project invesdwin-context-persistence by subes.

the class SerializingCollection method add.

@Override
public boolean add(final E element) {
    if (this.size == READ_ONLY_FILE_SIZE) {
        throw new IllegalStateException("File [" + file + "] is in read only mode since it contained data when it was opened!");
    }
    try {
        final IByteBuffer writeBuffer = getWriteBuffer();
        final int length = serde.toBuffer(writeBuffer, element);
        if (length == 0) {
            throw new IllegalStateException("bytes should contain actual data: " + element);
        }
        final OutputStream fos = getFos();
        if (fixedLength == null) {
            OutputStreams.writeInt(fos, length);
        } else {
            if (length != fixedLength) {
                throw new IllegalArgumentException("Serialized object [" + element + "] has unexpected byte length of [" + length + "] while fixed length [" + fixedLength + "] was expected!");
            }
        }
        writeBuffer.getBytesTo(0, fos, length);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }
    size++;
    return true;
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) IByteBuffer(de.invesdwin.util.streams.buffer.bytes.IByteBuffer)

Aggregations

IByteBuffer (de.invesdwin.util.streams.buffer.bytes.IByteBuffer)6 OutputStream (java.io.OutputStream)2 RetryLaterRuntimeException (de.invesdwin.context.integration.retry.RetryLaterRuntimeException)1 ATimeSeriesUpdater (de.invesdwin.context.persistence.timeseriesdb.updater.ATimeSeriesUpdater)1 TextDescription (de.invesdwin.util.lang.description.TextDescription)1 ISerde (de.invesdwin.util.marshallers.serde.ISerde)1 PreLockedDelegateInputStream (de.invesdwin.util.streams.PreLockedDelegateInputStream)1 MemoryMappedFile (de.invesdwin.util.streams.buffer.MemoryMappedFile)1 ArrayExpandableByteBuffer (de.invesdwin.util.streams.buffer.bytes.extend.ArrayExpandableByteBuffer)1 PooledFastByteArrayOutputStream (de.invesdwin.util.streams.pool.PooledFastByteArrayOutputStream)1 BufferedFileDataInputStream (de.invesdwin.util.streams.pool.buffered.BufferedFileDataInputStream)1 PreLockedBufferedFileDataInputStream (de.invesdwin.util.streams.pool.buffered.PreLockedBufferedFileDataInputStream)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1