org.rx.io.GZIPStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rxlib Show documentation
Show all versions of rxlib Show documentation
A set of utilities for Java
package org.rx.io;
import io.netty.buffer.ByteBuf;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.rx.core.Constants;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
@RequiredArgsConstructor
public class GZIPStream extends IOStream {
private static final long serialVersionUID = 5949731591101041212L;
private final IOStream baseStream;
private final boolean leaveOpen;
private transient GZIPInputStream reader;
private transient GZIPOutputStream writer;
@Override
public String getName() {
return baseStream.getName();
}
@SneakyThrows
@Override
public GZIPInputStream getReader() {
if (reader == null) {
reader = new GZIPInputStream(baseStream.getReader(), Constants.HEAP_BUF_SIZE);
}
return reader;
}
@SneakyThrows
@Override
public GZIPOutputStream getWriter() {
if (writer == null) {
writer = new GZIPOutputStream(baseStream.getWriter(), Constants.HEAP_BUF_SIZE);
}
return writer;
}
@Override
public boolean canSeek() {
return baseStream.canSeek();
}
@Override
public long getPosition() {
return baseStream.getPosition();
}
@Override
public void setPosition(long position) {
baseStream.setPosition(position);
}
@Override
public long getLength() {
return baseStream.getLength();
}
public GZIPStream(IOStream stream) {
this(stream, false);
}
@Override
protected void freeObjects() throws Throwable {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
if (!leaveOpen) {
baseStream.close();
}
}
@SneakyThrows
public void finish() {
if (writer != null) {
writer.finish();
}
}
@Override
public byte[] toArray() {
long pos = getPosition();
setPosition(0);
ByteBuf buf = Bytes.heapBuffer();
while (read(buf, Constants.HEAP_BUF_SIZE) != Constants.IO_EOF) {
}
setPosition(pos);
return Bytes.getBytes(buf);
}
}