use of java.util.zip.GZIPOutputStream in project hadoop by apache.
the class TestCombineFileInputFormat method writeGzipFile.
// Creates the gzip file and return the FileStatus
static FileStatus writeGzipFile(Configuration conf, Path name, short replication, int numBlocks) throws IOException, TimeoutException, InterruptedException {
FileSystem fileSys = FileSystem.get(conf);
GZIPOutputStream out = new GZIPOutputStream(fileSys.create(name, true, conf.getInt("io.file.buffer.size", 4096), replication, (long) BLOCKSIZE));
writeDataAndSetReplication(fileSys, name, out, replication, numBlocks);
return fileSys.getFileStatus(name);
}
use of java.util.zip.GZIPOutputStream in project hbase by apache.
the class Base64 method encodeBytes.
// end encodeBytes
/**
* Encodes a byte array into Base64 notation.
* <p>
* Valid options:
* <ul>
* <li>GZIP: gzip-compresses object before encoding it.</li>
* <li>DONT_BREAK_LINES: don't break lines at 76 characters. <i>Note:
* Technically, this makes your encoding non-compliant.</i></li>
* </ul>
*
* <p>
* Example: <code>encodeBytes( myData, Base64.GZIP )</code> or
* <p>
* Example:
* <code>encodeBytes( myData, Base64.GZIP | Base64.DONT_BREAK_LINES )</code>
*
* @param source The data to convert
* @param off Offset in array where conversion should begin
* @param len Length of data to convert
* @param options Specified options
* @see Base64#GZIP
* @see Base64#DONT_BREAK_LINES
* @see Base64#URL_SAFE
* @see Base64#ORDERED
* @return encoded byte array
* @since 2.0
*/
public static String encodeBytes(byte[] source, int off, int len, int options) {
if ((options & GZIP) == GZIP) {
// Compress?
// GZip -> Base64 -> ByteArray
ByteArrayOutputStream baos = new ByteArrayOutputStream();
GZIPOutputStream gzos = null;
try {
gzos = new GZIPOutputStream(new Base64OutputStream(baos, ENCODE | options));
gzos.write(source, off, len);
gzos.close();
gzos = null;
return new String(baos.toByteArray(), PREFERRED_ENCODING);
} catch (UnsupportedEncodingException uue) {
return new String(baos.toByteArray());
} catch (IOException e) {
LOG.error("error encoding byte array", e);
return null;
} finally {
if (gzos != null) {
try {
gzos.close();
} catch (Exception e) {
LOG.error("error closing GZIPOutputStream", e);
}
}
try {
baos.close();
} catch (Exception e) {
LOG.error("error closing ByteArrayOutputStream", e);
}
}
// end finally
}
// end Compress
// Don't compress. Better not to use streams at all then.
boolean breakLines = ((options & DONT_BREAK_LINES) == 0);
int len43 = len * 4 / 3;
byte[] outBuff = new byte[// Main 4:3
(len43) + // padding
((len % 3) > 0 ? 4 : 0) + // New lines
(breakLines ? (len43 / MAX_LINE_LENGTH) : 0)];
int d = 0;
int e = 0;
int len2 = len - 2;
int lineLength = 0;
for (; d < len2; d += 3, e += 4) {
encode3to4(source, d + off, 3, outBuff, e, options);
lineLength += 4;
if (breakLines && lineLength == MAX_LINE_LENGTH) {
outBuff[e + 4] = NEW_LINE;
e++;
lineLength = 0;
}
// end if: end of line
}
if (d < len) {
encode3to4(source, d + off, len - d, outBuff, e, options);
e += 4;
}
// Return value according to relevant encoding.
try {
return new String(outBuff, 0, e, PREFERRED_ENCODING);
} catch (UnsupportedEncodingException uue) {
return new String(outBuff, 0, e);
}
}
use of java.util.zip.GZIPOutputStream in project hbase by apache.
the class TestGzipFilter method testGzipFilter.
@Test
public void testGzipFilter() throws Exception {
String path = "/" + TABLE + "/" + ROW_1 + "/" + COLUMN_1;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
GZIPOutputStream os = new GZIPOutputStream(bos);
os.write(VALUE_1);
os.close();
byte[] value_1_gzip = bos.toByteArray();
// input side filter
Header[] headers = new Header[2];
headers[0] = new BasicHeader("Content-Type", Constants.MIMETYPE_BINARY);
headers[1] = new BasicHeader("Content-Encoding", "gzip");
Response response = client.put(path, headers, value_1_gzip);
assertEquals(response.getCode(), 200);
Table table = TEST_UTIL.getConnection().getTable(TABLE);
Get get = new Get(Bytes.toBytes(ROW_1));
get.addColumn(Bytes.toBytes(CFA), Bytes.toBytes("1"));
Result result = table.get(get);
byte[] value = result.getValue(Bytes.toBytes(CFA), Bytes.toBytes("1"));
assertNotNull(value);
assertTrue(Bytes.equals(value, VALUE_1));
// output side filter
headers[0] = new BasicHeader("Accept", Constants.MIMETYPE_BINARY);
headers[1] = new BasicHeader("Accept-Encoding", "gzip");
response = client.get(path, headers);
assertEquals(response.getCode(), 200);
ByteArrayInputStream bis = new ByteArrayInputStream(response.getBody());
GZIPInputStream is = new GZIPInputStream(bis);
value = new byte[VALUE_1.length];
is.read(value, 0, VALUE_1.length);
assertTrue(Bytes.equals(value, VALUE_1));
is.close();
table.close();
testScannerResultCodes();
}
use of java.util.zip.GZIPOutputStream in project tomcat by apache.
the class GzipInterceptor method compress.
public static byte[] compress(byte[] data) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
GZIPOutputStream gout = new GZIPOutputStream(bout);
gout.write(data);
gout.flush();
gout.close();
return bout.toByteArray();
}
use of java.util.zip.GZIPOutputStream in project tomcat by apache.
the class GzipOutputFilter method doWrite.
// --------------------------------------------------- OutputBuffer Methods
@Override
public int doWrite(ByteBuffer chunk) throws IOException {
if (compressionStream == null) {
compressionStream = new GZIPOutputStream(fakeOutputStream, true);
}
int len = chunk.remaining();
if (chunk.hasArray()) {
compressionStream.write(chunk.array(), chunk.arrayOffset() + chunk.position(), len);
} else {
byte[] bytes = new byte[len];
chunk.put(bytes);
compressionStream.write(bytes, 0, len);
}
return len;
}
Aggregations