A class to provide compression features to Jervis, specifically GZip. GZip provides groovy-like leftShift for compressing strings. This class also uses maximum compression by default and provides an easy means to change the compression level.
To run this example, clone Jervis and execute ./gradlew console to bring up a Groovy Console with the classpath set up.
import net.gleske.jervis.tools.GZip
new File('test.gz').withOutputStream { OutputStream os ->
try {
(new GZip(os)).withCloseable {
it << 'hello world\n\nMy friend\n'
}
} finally {
os.close()
}
}
This example uploads compressed data directly to Sonatype Nexus. Compare this example with SimpleRestService
import net.gleske.jervis.tools.GZip
import net.gleske.jervis.tools.SecurityIO
String username = 'your user'
String password = 'your pass'
URL api_url = new URL('http://localhost:8081/repository/hosted-raw-repo/file.gz')
HttpURLConnection response = api_url.openConnection().with { conn ->
conn.doOutput = true
conn.setRequestMethod('PUT')
conn.setRequestProperty('Authorization', "Basic " + SecurityIO.encodeBase64("${username}:${password}"))
conn.setRequestProperty('Accept', '*/*')
conn.setRequestProperty('Expect', '100-continue')
conn.outputStream.withCloseable { nexus_os ->
new GZip(nexus_os).withCloseable { gzip ->
gzip << 'hello world\n\nMy friend\n'
}
}
conn
}
// getting the response code completes the setup and makes the network connection
assert response.responseCode == 201
Some APIs, such as uploading to GitHub analysis as SARIF data, may require compressing data and including the compressed payload as part of a plain text JSON request. This example highlights getting base64 encoded compressed data. You can find a more advanced example in GitHub class documentation.
import net.gleske.jervis.tools.GZip
import net.gleske.jervis.tools.SecurityIO
import java.util.zip.GZIPInputStream
ByteArrayOutputStream compressed = new ByteArrayOutputStream()
// best speed compression
new GZip(compressed, 1).withCloseable {
it << 'hello'
it << ' world'
}
// COMPRESSED data encoded as base64
String data = SecurityIO.encodeBase64(compressed.toByteArray())
// DECOMPRESS example
ByteArrayOutputStream plain = new ByteArrayOutputStream()
new ByteArrayInputStream(SecurityIO.decodeBase64Bytes(data)).withCloseable { is ->
new GZIPInputStream(is).withCloseable { gunzip ->
plain << gunzip
}
}
assert plain.toString() == 'hello world'
| Constructor and description |
|---|
GZip
(OutputStream os, Integer level = Deflater.BEST_COMPRESSION)Creates a GZIPOutputStream with maximum compression enabled. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
OutputStream |
leftShift(String shiftString)Write a String similar to how you would write to a file with leftShift on a Writer. |
|
void |
setLevel(Integer level)This can change the compression level before you write data to GZip after it was instantiated. |
| Methods inherited from class | Name |
|---|---|
class GZIPOutputStream |
write, finish, flush, write, close, write, nullOutputStream, equals, toString, hashCode, getClass, notify, notifyAll, wait, wait, wait |
class DeflaterOutputStream |
flush, write, write, close, finish, write, nullOutputStream, equals, toString, hashCode, getClass, notify, notifyAll, wait, wait, wait |
Creates a GZIPOutputStream with maximum compression enabled.
os - An OutputStream to wrap which will write out compressed data.level - A compression level between fastest (1) and best compression (9).Write a String similar to how you would write to a file with leftShift on a Writer.
shiftString - A string which will be written out after being compressed.This can change the compression level before you write data to GZip after it was instantiated.
level - A compression level between fastest (1) and best compression (9).Jervis API documentation.