public static void compress(InputStream in, OutputStream out) throws IOException { LZFOutputStream compressor = null; boolean success = true; try { compressor = new LZFOutputStream(out); byte[] buf = ThreadLocalByteBuffer.getBuffer(); int len; while ((len = in.read(buf)) != -1) { compressor.write(buf, 0, len); } compressor.close(); } catch (IOException e) { success = false; throw new IllegalStateException(e.getMessage(), e); } finally { if (!success && (compressor != null)) { compressor.close(); } } }
@Override public boolean put(ObjectId objectId, InputStream raw) { Preconditions.checkNotNull(objectId); Preconditions.checkNotNull(raw); Preconditions.checkArgument(!objectId.isNull(), "ObjectId is NULL"); ByteArrayOutputStream rawOut = new ByteArrayOutputStream(); LZFOutputStream cOut = new LZFOutputStream(rawOut); try { ByteStreams.copy(raw, cOut); cOut.flush(); cOut.close(); } catch (IOException e) { throw Throwables.propagate(e); } final byte[] rawData = rawOut.toByteArray(); final boolean inserted = putInternal(objectId, rawData); return inserted; }
private byte[] toBytes(RevObject object) { ObjectWriter<RevObject> writer = serializers.createObjectWriter(object.getType()); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); LZFOutputStream cOut = new LZFOutputStream(byteStream); try { writer.write(object, cOut); } catch (IOException e) { throw new RuntimeException(e); } try { cOut.close(); } catch (IOException e) { throw Throwables.propagate(e); } return byteStream.toByteArray(); }
protected void writeObject(RevObject object, OutputStream target) { ObjectWriter<RevObject> writer = serializationFactory.createObjectWriter(object.getType()); LZFOutputStream cOut = new LZFOutputStream(target); try { writer.write(object, cOut); } catch (IOException e) { throw Throwables.propagate(e); } finally { try { cOut.flush(); cOut.close(); } catch (Exception e) { throw Throwables.propagate(e); } } int size = ((ByteArrayOutputStream) target).size(); //System.err.printf("%d,%s,%s\n", size, object.getId(), object.getType()); }