/** * @param level the compression level (0-9), The default setting is DEFAULT_COMPRESSION. * @throws IllegalArgumentException if the compression level is invalid */ public CompressedMarshObjectConvertor(int level, ISmartLengthBasedCacheCallback cacheCallback) { _byteArrayCache = createSerializationByteArrayCache(cacheCallback); _level = level; try { _bao = new GSByteArrayOutputStream(); _zo = new ZipOutputStream(_bao); _zo.setLevel(_level); _zo.putNextEntry(new ZipEntry(Integer.toString(zipEntryCounter++))); _oo = getObjectOutputStream(_zo); _bai = new GSByteArrayInputStream(new byte[0]); _zi = new ZipInputStream(_bai); getObject(getMarshObjectInternal("", true)); // remove header from // in/out } catch (Exception e) { if (_logger.isLoggable(Level.SEVERE)) { _logger.log(Level.SEVERE, e.getMessage(), e); } } }
private MarshObject getMarshObjectInternal(Object o, boolean init) throws IOException { byte[] bc; if (init) { bc = serializeToByteArray(o); } else { _bao.setBuffer(_byteArrayCache.get()); _bao.reset(); // check for next time if (++zipEntryCounter < MAX_ENTRIES) { _zo.putNextEntry(new ZipEntry(Integer.toString(zipEntryCounter))); _oo.reset(); } else // open new zip OutputStream for next time { zipEntryCounter = 0; _zo = new ZipOutputStream(_bao); _zo.setLevel(_level); _zo.putNextEntry(new ZipEntry(Integer.toString(zipEntryCounter))); _oo = getObjectOutputStream(_zo); // remove ObjectOutputStream header from zip stream _zo.closeEntry(); _bao.reset(); _zo.putNextEntry(new ZipEntry(Integer.toString(++zipEntryCounter))); _oo.reset(); } bc = serializeToByteArray(o); _byteArrayCache.notifyUsedSize(bc.length); _bao.setBuffer(DUMMY_BUFFER); } return new CompressedMarshObject(bc); }