List of usage examples for java.util.zip GZIPOutputStream close
public void close() throws IOException
From source file:org.mule.util.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>//from w w w .ja va 2 s . co m * Valid options: * * <pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * * <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 * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) throws IOException { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { ByteArrayOutputStream baos = null; GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new ByteArrayOutputStream(4096); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.finish(); gzos.close(); } // end try catch (IOException e) { throw e; } // end catch finally { IOUtils.closeQuietly(gzos); IOUtils.closeQuietly(b64os); IOUtils.closeQuietly(baos); } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for // padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New // lines 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); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }
From source file:org.opensextant.util.TextUtils.java
/** * * @param buf// w w w. jav a 2s . c om * text * @param charset * character set encoding for text * @return byte array for the compressed result * @throws IOException * on error with compression or text encoding */ public static byte[] compress(String buf, String charset) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gz = new GZIPOutputStream(out); gz.write(buf.getBytes(charset)); gz.close(); return out.toByteArray(); }
From source file:org.apache.sling.discovery.base.connectors.ping.TopologyConnectorClient.java
/** ping the server and pass the announcements between the two **/ void ping(final boolean force) { if (autoStopped) { // then we suppress any further pings! logger.debug("ping: autoStopped=true, hence suppressing any further pings."); return;//from w w w .j a v a 2s . c o m } if (force) { backoffPeriodEnd = -1; } else if (backoffPeriodEnd > 0) { if (System.currentTimeMillis() < backoffPeriodEnd) { logger.debug("ping: not issueing a heartbeat due to backoff instruction from peer."); return; } else { logger.debug("ping: backoff period ended, issuing another ping now."); } } final String uri = connectorUrl.toString() + "." + clusterViewService.getSlingId() + ".json"; if (logger.isDebugEnabled()) { logger.debug("ping: connectorUrl=" + connectorUrl + ", complete uri=" + uri); } final HttpClientContext clientContext = HttpClientContext.create(); final CloseableHttpClient httpClient = createHttpClient(); final HttpPut putRequest = new HttpPut(uri); // setting the connection timeout (idle connection, configured in seconds) putRequest.setConfig( RequestConfig.custom().setConnectTimeout(1000 * config.getSocketConnectTimeout()).build()); Announcement resultingAnnouncement = null; try { String userInfo = connectorUrl.getUserInfo(); if (userInfo != null) { Credentials c = new UsernamePasswordCredentials(userInfo); clientContext.getCredentialsProvider().setCredentials( new AuthScope(putRequest.getURI().getHost(), putRequest.getURI().getPort()), c); } Announcement topologyAnnouncement = new Announcement(clusterViewService.getSlingId()); topologyAnnouncement.setServerInfo(serverInfo); final ClusterView clusterView; try { clusterView = clusterViewService.getLocalClusterView(); } catch (UndefinedClusterViewException e) { // SLING-5030 : then we cannot ping logger.warn("ping: no clusterView available at the moment, cannot ping others now: " + e); return; } topologyAnnouncement.setLocalCluster(clusterView); if (force) { logger.debug("ping: sending a resetBackoff"); topologyAnnouncement.setResetBackoff(true); } announcementRegistry.addAllExcept(topologyAnnouncement, clusterView, new AnnouncementFilter() { public boolean accept(final String receivingSlingId, final Announcement announcement) { // filter out announcements that are of old cluster instances // which I dont really have in my cluster view at the moment final Iterator<InstanceDescription> it = clusterView.getInstances().iterator(); while (it.hasNext()) { final InstanceDescription instance = it.next(); if (instance.getSlingId().equals(receivingSlingId)) { // then I have the receiving instance in my cluster view // all fine then return true; } } // looks like I dont have the receiving instance in my cluster view // then I should also not propagate that announcement anywhere return false; } }); final String p = requestValidator.encodeMessage(topologyAnnouncement.asJSON()); if (logger.isDebugEnabled()) { logger.debug("ping: topologyAnnouncement json is: " + p); } requestValidator.trustMessage(putRequest, p); if (config.isGzipConnectorRequestsEnabled()) { // tell the server that the content is gzipped: putRequest.addHeader("Content-Encoding", "gzip"); // and gzip the body: final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final GZIPOutputStream gzipOut = new GZIPOutputStream(baos); gzipOut.write(p.getBytes("UTF-8")); gzipOut.close(); final byte[] gzippedEncodedJson = baos.toByteArray(); putRequest.setEntity(new ByteArrayEntity(gzippedEncodedJson, ContentType.APPLICATION_JSON)); lastRequestEncoding = "gzip"; } else { // otherwise plaintext: final StringEntity plaintext = new StringEntity(p, "UTF-8"); plaintext.setContentType(ContentType.APPLICATION_JSON.getMimeType()); putRequest.setEntity(plaintext); lastRequestEncoding = "plaintext"; } // independent of request-gzipping, we do accept the response to be gzipped, // so indicate this to the server: putRequest.addHeader("Accept-Encoding", "gzip"); final CloseableHttpResponse response = httpClient.execute(putRequest, clientContext); if (logger.isDebugEnabled()) { logger.debug("ping: done. code=" + response.getStatusLine().getStatusCode() + " - " + response.getStatusLine().getReasonPhrase()); } lastStatusCode = response.getStatusLine().getStatusCode(); lastResponseEncoding = null; if (response.getStatusLine().getStatusCode() == HttpServletResponse.SC_OK) { final Header contentEncoding = response.getFirstHeader("Content-Encoding"); if (contentEncoding != null && contentEncoding.getValue() != null && contentEncoding.getValue().contains("gzip")) { lastResponseEncoding = "gzip"; } else { lastResponseEncoding = "plaintext"; } final String responseBody = requestValidator.decodeMessage(putRequest.getURI().getPath(), response); // limiting to 16MB, should be way enough if (logger.isDebugEnabled()) { logger.debug("ping: response body=" + responseBody); } if (responseBody != null && responseBody.length() > 0) { Announcement inheritedAnnouncement = Announcement.fromJSON(responseBody); final long backoffInterval = inheritedAnnouncement.getBackoffInterval(); if (backoffInterval > 0) { // then reset the backoffPeriodEnd: /* minus 1 sec to avoid slipping the interval by a few millis */ this.backoffPeriodEnd = System.currentTimeMillis() + (1000 * backoffInterval) - 1000; logger.debug("ping: servlet instructed to backoff: backoffInterval=" + backoffInterval + ", resulting in period end of " + new Date(backoffPeriodEnd)); } else { logger.debug("ping: servlet did not instruct any backoff-ing at this stage"); this.backoffPeriodEnd = -1; } if (inheritedAnnouncement.isLoop()) { if (logger.isDebugEnabled()) { logger.debug( "ping: connector response indicated a loop detected. not registering this announcement from " + inheritedAnnouncement.getOwnerId()); } if (inheritedAnnouncement.getOwnerId().equals(clusterViewService.getSlingId())) { // SLING-3316 : local-loop detected. Check config to see if we should stop this connector if (config.isAutoStopLocalLoopEnabled()) { inheritedAnnouncement = null; // results in connected -> false and representsloop -> true autoStopped = true; // results in isAutoStopped -> true } } } else { inheritedAnnouncement.setInherited(true); if (announcementRegistry.registerAnnouncement(inheritedAnnouncement) == -1) { if (logger.isDebugEnabled()) { logger.debug( "ping: connector response is from an instance which I already see in my topology" + inheritedAnnouncement); } statusDetails = "receiving side is seeing me via another path (connector or cluster) already (loop)"; return; } } resultingAnnouncement = inheritedAnnouncement; statusDetails = null; } else { statusDetails = "no response body received"; } } else { statusDetails = "got HTTP Status-Code: " + lastStatusCode; } // SLING-2882 : reset suppressPingWarnings_ flag in success case suppressPingWarnings_ = false; } catch (IOException e) { // SLING-2882 : set/check the suppressPingWarnings_ flag if (suppressPingWarnings_) { if (logger.isDebugEnabled()) { logger.debug("ping: got IOException: " + e + ", uri=" + uri); } } else { suppressPingWarnings_ = true; logger.warn("ping: got IOException [suppressing further warns]: " + e + ", uri=" + uri); } statusDetails = e.toString(); } catch (JSONException e) { logger.warn("ping: got JSONException: " + e); statusDetails = e.toString(); } catch (RuntimeException re) { logger.warn("ping: got RuntimeException: " + re, re); statusDetails = re.toString(); } finally { putRequest.releaseConnection(); lastInheritedAnnouncement = resultingAnnouncement; lastPingedAt = System.currentTimeMillis(); try { httpClient.close(); } catch (IOException e) { logger.error("disconnect: could not close httpClient: " + e, e); } } }
From source file:Base64.java
/** * Encodes a byte array into Base64 notation. * <p>//from w w w . jav a2 s. c om * Valid options:<pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * <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 * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { java.io.ByteArrayOutputStream baos = null; java.util.zip.GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new java.io.ByteArrayOutputStream(); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new java.util.zip.GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.close(); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch finally { try { gzos.close(); } catch (Exception e) { } try { b64os.close(); } catch (Exception e) { } try { baos.close(); } catch (Exception e) { } } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines 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); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }
From source file:org.apache.olingo.ext.pojogen.AbstractPOJOGenMojo.java
@Override public void execute() throws MojoExecutionException, MojoFailureException { if (new File(outputDirectory + File.separator + TOOL_DIR).exists()) { getLog().info("Nothing to do because " + TOOL_DIR + " directory already exists. Clean to update."); return;/*from www . ja v a 2s. c o m*/ } Velocity.addProperty(Velocity.RESOURCE_LOADER, "class"); Velocity.addProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName()); try { final Triple<XMLMetadata, String, Edm> metadata = getMetadata(); for (EdmSchema schema : metadata.getRight().getSchemas()) { namespaces.add(schema.getNamespace().toLowerCase()); } final Map<String, String> entityTypeNames = new HashMap<String, String>(); final Map<String, String> complexTypeNames = new HashMap<String, String>(); final Map<String, String> enumTypeNames = new HashMap<String, String>(); final Map<String, String> termNames = new HashMap<String, String>(); final Map<String, Object> objs = new HashMap<String, Object>(); for (EdmSchema schema : metadata.getRight().getSchemas()) { createUtility(metadata.getRight(), schema, basePackage); // write package-info for the base package final String schemaPath = utility.getNamespace().toLowerCase().replace('.', File.separatorChar); final File base = mkPkgDir(schemaPath); final String pkg = StringUtils.isBlank(basePackage) ? utility.getNamespace().toLowerCase() : basePackage + "." + utility.getNamespace().toLowerCase(); parseObj(base, pkg, "package-info", "package-info.java"); // write package-info for types package final File typesBaseDir = mkPkgDir(schemaPath + "/types"); final String typesPkg = pkg + ".types"; parseObj(typesBaseDir, typesPkg, "package-info", "package-info.java"); for (EdmTerm term : schema.getTerms()) { final String className = utility.capitalize(term.getName()); termNames.put(term.getFullQualifiedName().toString(), typesPkg + "." + className); objs.clear(); objs.put("term", term); parseObj(typesBaseDir, typesPkg, "term", className + ".java", objs); } for (EdmEnumType enumType : schema.getEnumTypes()) { final String className = utility.capitalize(enumType.getName()); enumTypeNames.put(enumType.getFullQualifiedName().toString(), typesPkg + "." + className); objs.clear(); objs.put("enumType", enumType); parseObj(typesBaseDir, typesPkg, "enumType", className + ".java", objs); } final List<EdmComplexType> complexes = new ArrayList<EdmComplexType>(); for (EdmComplexType complex : schema.getComplexTypes()) { complexes.add(complex); final String className = utility.capitalize(complex.getName()); complexTypeNames.put(complex.getFullQualifiedName().toString(), typesPkg + "." + className); objs.clear(); objs.put("complexType", complex); parseObj(typesBaseDir, typesPkg, "complexType", className + ".java", objs); parseObj(typesBaseDir, typesPkg, "complexTypeComposableInvoker", className + "ComposableInvoker.java", objs); parseObj(typesBaseDir, typesPkg, "complexCollection", className + "Collection.java", objs); parseObj(typesBaseDir, typesPkg, "complexCollectionComposableInvoker", className + "CollectionComposableInvoker.java", objs); } for (EdmEntityType entity : schema.getEntityTypes()) { final String className = utility.capitalize(entity.getName()); entityTypeNames.put(entity.getFullQualifiedName().toString(), typesPkg + "." + className); objs.clear(); objs.put("entityType", entity); final Map<String, String> keys; EdmEntityType baseType = null; if (entity.getBaseType() == null) { keys = getUtility().getEntityKeyType(entity); } else { baseType = entity.getBaseType(); objs.put("baseType", getUtility().getJavaType(baseType.getFullQualifiedName().toString())); while (baseType.getBaseType() != null) { baseType = baseType.getBaseType(); } keys = getUtility().getEntityKeyType(baseType); } if (keys.size() > 1) { // create compound key class final String keyClassName = utility .capitalize(baseType == null ? entity.getName() : baseType.getName()) + "Key"; objs.put("keyRef", keyClassName); if (entity.getBaseType() == null) { objs.put("keys", keys); parseObj(typesBaseDir, typesPkg, "entityTypeKey", keyClassName + ".java", objs); } } parseObj(typesBaseDir, typesPkg, "entityType", className + ".java", objs); parseObj(typesBaseDir, typesPkg, "entityComposableInvoker", className + "ComposableInvoker.java", objs); parseObj(typesBaseDir, typesPkg, "entityCollection", className + "Collection.java", objs); parseObj(typesBaseDir, typesPkg, "entityCollectionComposableInvoker", className + "CollectionComposableInvoker.java", objs); } // write container and top entity sets into the base package EdmEntityContainer container = schema.getEntityContainer(); if (container != null) { objs.clear(); objs.put("container", container); objs.put("namespace", schema.getNamespace()); objs.put("complexes", complexes); parseObj(base, pkg, "container", utility.capitalize(container.getName()) + ".java", objs); for (EdmEntitySet entitySet : container.getEntitySets()) { objs.clear(); objs.put("entitySet", entitySet); objs.put("container", container); parseObj(base, pkg, "entitySet", utility.capitalize(entitySet.getName()) + ".java", objs); } } } final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final GZIPOutputStream gzos = new GZIPOutputStream(baos); final ObjectOutputStream oos = new ObjectOutputStream(gzos); try { oos.writeObject(metadata.getLeft()); } finally { oos.close(); gzos.close(); baos.close(); } objs.clear(); objs.put("metadata", new String(Base64.encodeBase64(baos.toByteArray()), "UTF-8")); objs.put("metadataETag", metadata.getMiddle()); objs.put("entityTypes", entityTypeNames); objs.put("complexTypes", complexTypeNames); objs.put("enumTypes", enumTypeNames); objs.put("terms", termNames); final String actualBP = StringUtils.isBlank(basePackage) ? StringUtils.EMPTY : basePackage; parseObj(mkdir(actualBP.replace('.', File.separatorChar)), actualBP, "service", "Service.java", objs); } catch (Exception t) { getLog().error(t); throw (t instanceof MojoExecutionException) ? (MojoExecutionException) t : new MojoExecutionException("While executin mojo", t); } }
From source file:org.apache.hadoop.io.compress.TestCodec.java
void GzipConcatTest(Configuration conf, Class<? extends Decompressor> decomClass) throws IOException { Random r = new Random(); long seed = r.nextLong(); r.setSeed(seed);// w w w .j av a2 s . c om LOG.info(decomClass + " seed: " + seed); final int CONCAT = r.nextInt(4) + 3; final int BUFLEN = 128 * 1024; DataOutputBuffer dflbuf = new DataOutputBuffer(); DataOutputBuffer chkbuf = new DataOutputBuffer(); byte[] b = new byte[BUFLEN]; for (int i = 0; i < CONCAT; ++i) { GZIPOutputStream gzout = new GZIPOutputStream(dflbuf); r.nextBytes(b); int len = r.nextInt(BUFLEN); int off = r.nextInt(BUFLEN - len); chkbuf.write(b, off, len); gzout.write(b, off, len); gzout.close(); } final byte[] chk = Arrays.copyOf(chkbuf.getData(), chkbuf.getLength()); CompressionCodec codec = ReflectionUtils.newInstance(GzipCodec.class, conf); Decompressor decom = codec.createDecompressor(); assertNotNull(decom); assertEquals(decomClass, decom.getClass()); DataInputBuffer gzbuf = new DataInputBuffer(); gzbuf.reset(dflbuf.getData(), dflbuf.getLength()); InputStream gzin = codec.createInputStream(gzbuf, decom); dflbuf.reset(); IOUtils.copyBytes(gzin, dflbuf, 4096); final byte[] dflchk = Arrays.copyOf(dflbuf.getData(), dflbuf.getLength()); assertTrue(java.util.Arrays.equals(chk, dflchk)); }
From source file:Base64.java
/** * Encodes a byte array into Base64 notation. * <p>//from w w w .j a v a2 s . c o m * Valid options:<pre> * GZIP: gzip-compresses object before encoding it. * DONT_BREAK_LINES: don't break lines at 76 characters * <i>Note: Technically, this makes your encoding non-compliant.</i> * </pre> * <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 * @return the text node * @since 2.0 */ public static String encodeBytes(byte[] source, int off, int len, int options) { // Isolate options int dontBreakLines = (options & DONT_BREAK_LINES); int gzip = (options & GZIP); // Compress? if (gzip == GZIP) { java.io.ByteArrayOutputStream baos = null; java.util.zip.GZIPOutputStream gzos = null; Base64.OutputStream b64os = null; try { // GZip -> Base64 -> ByteArray baos = new java.io.ByteArrayOutputStream(); b64os = new Base64.OutputStream(baos, ENCODE | dontBreakLines); gzos = new java.util.zip.GZIPOutputStream(b64os); gzos.write(source, off, len); gzos.close(); } // end try catch (java.io.IOException e) { e.printStackTrace(); return null; } // end catch finally { try { gzos.close(); } catch (Exception e) { } try { b64os.close(); } catch (Exception e) { } try { baos.close(); } catch (Exception e) { } } // end finally // Return value according to relevant encoding. try { return new String(baos.toByteArray(), PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(baos.toByteArray()); } // end catch } // end if: compress // Else, don't compress. Better not to use streams at all then. else { // Convert option to boolean in way that code likes it. boolean breakLines = dontBreakLines == 0; int len43 = len * 4 / 3; byte[] outBuff = new byte[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // Account for padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines 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); lineLength += 4; if (breakLines && lineLength == MAX_LINE_LENGTH) { outBuff[e + 4] = NEW_LINE; e++; lineLength = 0; } // end if: end of line } // en dfor: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } // end try catch (java.io.UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } // end catch } // end else: don't compress }
From source file:agilejson.Base64.java
/** * Encodes a byte array into Base64 notation. * <p>// w ww. j ava2s. co m * 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[(len43) // Main 4:3 + ((len % 3) > 0 ? 4 : 0) // padding + (breakLines ? (len43 / MAX_LINE_LENGTH) : 0)]; // New lines 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 } // end for: each piece of array if (d < len) { encode3to4(source, d + off, len - d, outBuff, e, options); e += 4; } // end if: some padding needed // Return value according to relevant encoding. try { return new String(outBuff, 0, e, PREFERRED_ENCODING); } catch (UnsupportedEncodingException uue) { return new String(outBuff, 0, e); } }
From source file:edu.umn.cs.spatialHadoop.visualization.FrequencyMap.java
@Override public void write(DataOutput out) throws IOException { super.write(out); ByteArrayOutputStream baos = new ByteArrayOutputStream(); GZIPOutputStream gzos = new GZIPOutputStream(baos); ByteBuffer bbuffer = ByteBuffer.allocate(getHeight() * 4 + 8); bbuffer.putInt(getWidth());/*from w ww . j a v a 2 s . co m*/ bbuffer.putInt(getHeight()); gzos.write(bbuffer.array(), 0, bbuffer.position()); for (int x = 0; x < getWidth(); x++) { bbuffer.clear(); for (int y = 0; y < getHeight(); y++) { bbuffer.putFloat(frequencies[x][y]); } gzos.write(bbuffer.array(), 0, bbuffer.position()); } gzos.close(); byte[] serializedData = baos.toByteArray(); out.writeInt(serializedData.length); out.write(serializedData); }
From source file:org.apache.sling.discovery.etcd.EtcdServiceTest.java
@Test public void testGetGzipped() throws Exception { HttpServlet servlet = new HttpServlet() { @Override/*from ww w.j a va 2 s .c o m*/ protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { String acceptEncoding = req.getHeader("Accept-Encoding"); if (acceptEncoding != null && acceptEncoding.equalsIgnoreCase("gzip")) { res.setHeader("Content-Encoding", "gzip"); res.setStatus(200); ByteArrayOutputStream out = new ByteArrayOutputStream(); GZIPOutputStream gzip = new GZIPOutputStream(out); String data = IOUtils.toString(getClass().getResourceAsStream("/get-properties.json"), "UTF-8"); gzip.write(data.getBytes("UTF-8")); gzip.close(); res.getOutputStream().write(out.toByteArray()); } else { throw new IllegalArgumentException("accept-encoding not found or not gzip"); } } }; server = startServer(servlet, "/v2/keys/discovery/properties/sling-id"); EtcdService etcdService = buildEtcdService(serverPort(server)); Map<String, String> properties = etcdService.getProperties("sling-id"); Assert.assertNotNull(properties); Assert.assertEquals(1, properties.size()); }