use of java.util.zip.DeflaterOutputStream in project knox by apache.
the class UrlRewriteResponseTest method testStreamResponse.
private void testStreamResponse(String content, UrlRewriteResponse rewriteResponse, String contentType) throws IOException {
Path inputFile = Files.createTempFile("input", "test");
Path outputFile = Files.createTempFile("output", "test");
try {
try (OutputStream outputStream = Files.newOutputStream(inputFile);
OutputStream outStream = "gzip".equalsIgnoreCase(contentType) ? new GZIPOutputStream(outputStream) : "deflate".equalsIgnoreCase(contentType) ? new DeflaterOutputStream(outputStream) : outputStream) {
outStream.write(content.getBytes(StandardCharsets.UTF_8));
}
try (InputStream input = Files.newInputStream(inputFile);
OutputStream output = Files.newOutputStream(outputFile)) {
rewriteResponse.streamResponse(input, output);
}
try (InputStream inputStream = Files.newInputStream(outputFile);
InputStream inStream = "gzip".equalsIgnoreCase(contentType) ? new GZIPInputStream(inputStream) : "deflate".equalsIgnoreCase(contentType) ? new InflaterInputStream(inputStream) : inputStream) {
assertThat(String.valueOf(IOUtils.toCharArray(inStream, StandardCharsets.UTF_8)), is(content));
}
} finally {
Files.delete(inputFile);
Files.delete(outputFile);
}
}
use of java.util.zip.DeflaterOutputStream in project litiengine by gurkenlabs.
the class TileData method encodeBase64.
private static String encodeBase64(TileData data) throws IOException {
try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
OutputStream out = baos;
if (data.getCompression() != null && Compression.isValid(data.getCompression())) {
if (data.getCompression().equals(Compression.GZIP)) {
out = new GZIPOutputStream(baos);
} else if (data.getCompression().equals(Compression.ZLIB)) {
out = new DeflaterOutputStream(baos);
}
}
for (Tile tile : data.getTiles()) {
int gid = 0;
if (tile != null) {
gid = tile.getGridId();
}
out.write(gid);
out.write(gid >> Byte.SIZE);
out.write(gid >> Byte.SIZE * 2);
out.write(gid >> Byte.SIZE * 3);
}
if (data.getCompression() != null && data.getCompression().equals(Compression.GZIP)) {
((GZIPOutputStream) out).finish();
}
if (data.getCompression() != null && data.getCompression().equals(Compression.ZLIB)) {
((DeflaterOutputStream) out).finish();
}
return Codec.encode(baos.toByteArray());
}
}
use of java.util.zip.DeflaterOutputStream in project iaf by ibissource.
the class JdbcUtil method getBlobWriter.
public static Writer getBlobWriter(IDbmsSupport dbmsSupport, Object blobUpdateHandle, final ResultSet rs, int columnIndex, String charset, boolean compressBlob) throws IOException, JdbcException, SQLException {
Writer result;
OutputStream out = dbmsSupport.getBlobOutputStream(rs, columnIndex, blobUpdateHandle);
if (charset == null) {
charset = StreamUtil.DEFAULT_INPUT_STREAM_ENCODING;
}
if (compressBlob) {
result = new BufferedWriter(new OutputStreamWriter(new DeflaterOutputStream(out), charset));
} else {
result = new BufferedWriter(new OutputStreamWriter(out, charset));
}
return result;
}
use of java.util.zip.DeflaterOutputStream in project iaf by ibissource.
the class JdbcUtil method putByteArrayAsBlob.
public static void putByteArrayAsBlob(IDbmsSupport dbmsSupport, final ResultSet rs, int columnIndex, byte[] content, boolean compressBlob) throws IOException, JdbcException, SQLException {
if (content != null) {
Object blobHandle = dbmsSupport.getBlobHandle(rs, columnIndex);
try (OutputStream out = dbmsSupport.getBlobOutputStream(rs, columnIndex, blobHandle)) {
if (compressBlob) {
try (DeflaterOutputStream dos = new DeflaterOutputStream(out)) {
dos.write(content);
}
} else {
out.write(content);
}
}
dbmsSupport.updateBlob(rs, columnIndex, blobHandle);
} else {
log.warn("content to store in blob was null");
}
}
use of java.util.zip.DeflaterOutputStream in project iaf by ibissource.
the class JdbcTransactionalStorageTest method testQueryTextAndBrowseMessageHelper.
public void testQueryTextAndBrowseMessageHelper(boolean blobsCompressed) throws Exception {
storage.setBlobsCompressed(blobsCompressed);
storage.configure();
// check created query
String expected = "SELECT " + keyField + "," + messageField + " FROM " + tableName + " WHERE " + keyField + "=?";
String query = storage.selectDataQuery;
assertEquals(expected, query);
Message message = createMessage();
String storageKey = null;
// insert a record
try (Connection connection = getConnection()) {
try (PreparedStatement stmt = prepareStatement(connection)) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream out = blobsCompressed ? new DeflaterOutputStream(baos) : baos;
try (ObjectOutputStream oos = new ObjectOutputStream(out)) {
oos.writeObject(message);
}
stmt.setBytes(1, baos.toByteArray());
stmt.execute();
try (ResultSet rs = stmt.getGeneratedKeys()) {
if (rs.next()) {
// check inserted data being correctly retrieved
storageKey = rs.getString(1);
} else {
Assert.fail("The query [" + storage.selectDataQuery + "] returned empty result set expected 1");
}
}
}
}
Message data = storage.browseMessage(storageKey);
assertEquals(message.asString(), data.asString());
}
Aggregations