List of usage examples for com.google.common.hash Hashing crc32
public static HashFunction crc32()
From source file:org.etourdot.vertx.mods.XmlQueryHandler.java
public void handle(Message message) { final JsonObject messageBody = (JsonObject) message.body(); final String query = messageBody.getString(QUERY); final String xml = messageBody.getString(XML); final String url_xml = messageBody.getString(URL_XML); final JsonArray params = messageBody.getArray(PARAMS); if (Strings.isNullOrEmpty(xml) && Strings.isNullOrEmpty(url_xml)) { sendError(message, "xml ou url_xml must be specified"); return;// www . ja va2s . c om } if (Strings.isNullOrEmpty(query)) { sendError(message, "query must be specified"); return; } try { final Source xml_source; if (!Strings.isNullOrEmpty(xml)) { if (!Strings.isNullOrEmpty(url_xml)) { sendError(message, "xml either url_xml must be specified"); return; } xml_source = new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))); } else { xml_source = new SAXSource(new InputSource(url_xml)); } final HashCode key = Hashing.crc32().hashString(query, Charsets.UTF_8); final XQueryEvaluator xQueryEvaluator = cacheXQueryEvaluator.get(key, new Callable<XQueryEvaluator>() { @Override public XQueryEvaluator call() throws Exception { final XQueryCompiler xQueryCompiler = processor.newXQueryCompiler(); final XQueryExecutable xQueryExecutable = xQueryCompiler.compile(query); return xQueryExecutable.load(); } }); final StringWriter writer = new StringWriter(); final Serializer out = processor.newSerializer(writer); xQueryEvaluator.setSource(xml_source); xQueryEvaluator.setDestination(out); if (params != null) { for (Object param : params) { final JsonObject object = (JsonObject) param; for (String fieldName : object.getFieldNames()) { xQueryEvaluator.setExternalVariable(new QName(fieldName), new XdmAtomicValue(object.getString(fieldName))); } } } xQueryEvaluator.run(); JsonObject outputObject = new JsonObject(); outputObject.putString("output", writer.toString()); sendOK(message, outputObject); } catch (SaxonApiException | UnsupportedEncodingException | ExecutionException e) { sendError(message, e.getMessage()); } }
From source file:com.facebook.buck.util.zip.Zip.java
/** Walks the file tree rooted in baseDirectory to create zip entries */ public static void walkBaseDirectoryToCreateEntries(ProjectFilesystem filesystem, Map<String, Pair<CustomZipEntry, Optional<Path>>> entries, Path baseDir, ImmutableSet<Path> paths, boolean junkPaths, ZipCompressionLevel compressionLevel) throws IOException { // Since filesystem traversals can be non-deterministic, sort the entries we find into // a tree map before writing them out. FileVisitor<Path> pathFileVisitor = new SimpleFileVisitor<Path>() { private boolean isSkipFile(Path file) { return !paths.isEmpty() && !paths.contains(file); }//from w ww .j a v a 2s. co m private String getEntryName(Path path) { Path relativePath = junkPaths ? path.getFileName() : baseDir.relativize(path); return MorePaths.pathWithUnixSeparators(relativePath); } private CustomZipEntry getZipEntry(String entryName, Path path, BasicFileAttributes attr) throws IOException { boolean isDirectory = filesystem.isDirectory(path); if (isDirectory) { entryName += "/"; } CustomZipEntry entry = new CustomZipEntry(entryName); // We want deterministic ZIPs, so avoid mtimes. entry.setFakeTime(); entry.setCompressionLevel( isDirectory ? ZipCompressionLevel.NONE.getValue() : compressionLevel.getValue()); // If we're using STORED files, we must manually set the CRC, size, and compressed size. if (entry.getMethod() == ZipEntry.STORED && !isDirectory) { entry.setSize(attr.size()); entry.setCompressedSize(attr.size()); entry.setCrc(new ByteSource() { @Override public InputStream openStream() throws IOException { return filesystem.newFileInputStream(path); } }.hash(Hashing.crc32()).padToLong()); } long externalAttributes = filesystem.getFileAttributesForZipEntry(path); LOG.verbose("Setting mode for entry %s path %s to 0x%08X", entryName, path, externalAttributes); entry.setExternalAttributes(externalAttributes); return entry; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { if (!isSkipFile(file)) { CustomZipEntry entry = getZipEntry(getEntryName(file), file, attrs); entries.put(entry.getName(), new Pair<>(entry, Optional.of(file))); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException { if (!dir.equals(baseDir) && !isSkipFile(dir)) { CustomZipEntry entry = getZipEntry(getEntryName(dir), dir, attrs); entries.put(entry.getName(), new Pair<>(entry, Optional.empty())); } return FileVisitResult.CONTINUE; } }; filesystem.walkRelativeFileTree(baseDir, pathFileVisitor); }
From source file:com.facebook.buck.zip.RepackZipEntriesStep.java
@Override public int execute(ExecutionContext context) { ProjectFilesystem filesystem = context.getProjectFilesystem(); File inputFile = filesystem.getFileForRelativePath(inputPath); File outputFile = filesystem.getFileForRelativePath(outputPath); try (ZipInputStream in = new ZipInputStream(new BufferedInputStream(new FileInputStream(inputFile))); CustomZipOutputStream out = ZipOutputStreams.newOutputStream(outputFile)) { for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) { CustomZipEntry customEntry = new CustomZipEntry(entry); if (entries.contains(customEntry.getName())) { customEntry.setCompressionLevel(compressionLevel); }/*from www . j a v a 2s.c o m*/ InputStream toUse; // If we're using STORED files, we must pre-calculate the CRC. if (customEntry.getMethod() == ZipEntry.STORED) { try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) { ByteStreams.copy(in, bos); byte[] bytes = bos.toByteArray(); customEntry.setCrc(Hashing.crc32().hashBytes(bytes).padToLong()); customEntry.setSize(bytes.length); customEntry.setCompressedSize(bytes.length); toUse = new ByteArrayInputStream(bytes); } } else { toUse = in; } out.putNextEntry(customEntry); ByteStreams.copy(toUse, out); out.closeEntry(); } return 0; } catch (IOException e) { context.logError(e, "Unable to repack zip"); return 1; } }
From source file:org.etourdot.vertx.mods.XmlTransformHandler.java
public void handle(Message message) { final JsonObject messageBody = (JsonObject) message.body(); final String xsl = messageBody.getString(XSL); final String url_xsl = messageBody.getString(URL_XSL); final String xml = messageBody.getString(XML); final String url_xml = messageBody.getString(URL_XML); final JsonArray params = messageBody.getArray(PARAMS); if (Strings.isNullOrEmpty(xml) && Strings.isNullOrEmpty(url_xml)) { sendError(message, "xml ou url_xml must be specified"); return;/* w w w . j a va 2 s.c om*/ } if (Strings.isNullOrEmpty(xsl) && Strings.isNullOrEmpty(url_xsl)) { sendError(message, "xsl or url_xsl must be specified"); return; } try { final Source xml_source; if (!Strings.isNullOrEmpty(xml)) { if (!Strings.isNullOrEmpty(url_xml)) { sendError(message, "xml either url_xml must be specified"); return; } xml_source = new StreamSource(new ByteArrayInputStream(xml.getBytes("UTF-8"))); } else { xml_source = new SAXSource(new InputSource(url_xml)); } final Source xsl_source; if (!Strings.isNullOrEmpty(xsl)) { if (!Strings.isNullOrEmpty(url_xsl)) { sendError(message, "xsl either url_xsl must be specified"); return; } xsl_source = new StreamSource(new ByteArrayInputStream(xsl.getBytes("UTF-8"))); } else { xsl_source = new SAXSource(new InputSource(url_xsl)); } final HashCode key = Hashing.crc32().hashString(Joiner.on("").skipNulls().join(xsl, url_xsl), Charsets.UTF_8); final XsltTransformer xsltTransformer = cacheTransformers.get(key, new Callable<XsltTransformer>() { @Override public XsltTransformer call() throws Exception { final XsltCompiler xsltCompiler = processor.newXsltCompiler(); final XsltExecutable xsltExecutable = xsltCompiler.compile(xsl_source); return xsltExecutable.load(); } }); final XdmNode source = processor.newDocumentBuilder().build(xml_source); final StringWriter writer = new StringWriter(); final Serializer out = processor.newSerializer(writer); xsltTransformer.setInitialContextNode(source); xsltTransformer.setDestination(out); if (params != null) { for (Object param : params) { final JsonObject object = (JsonObject) param; for (String fieldName : object.getFieldNames()) { xsltTransformer.setParameter(new QName(fieldName), new XdmAtomicValue(object.getString(fieldName))); } } } xsltTransformer.transform(); JsonObject outputObject = new JsonObject(); outputObject.putString("output", writer.toString()); sendOK(message, outputObject); } catch (SaxonApiException | UnsupportedEncodingException | ExecutionException e) { sendError(message, e.getMessage()); } }
From source file:com.android.builder.internal.utils.CachedFileContents.java
/** * Computes the hashcode of the cached file. * * @return the hash code/*from ww w .j a va2s . c om*/ */ @Nullable private HashCode hashFile() { try { return Files.hash(mFile, Hashing.crc32()); } catch (IOException e) { return null; } }
From source file:org.lightjason.agentspeak.action.builtin.crypto.CHash.java
/** * runs hashing function with difference between Google Guava hashing and Java default digest * * @param p_context execution context/* w w w. ja va 2 s. c om*/ * @param p_algorithm algorithm name * @param p_data byte data representation * @return hash value */ private static String hash(@Nonnull final IContext p_context, @Nonnull final String p_algorithm, @Nonnull final byte[] p_data) { switch (p_algorithm.trim().toLowerCase(Locale.ROOT)) { case "adler-32": return Hashing.adler32().newHasher().putBytes(p_data).hash().toString(); case "crc-32": return Hashing.crc32().newHasher().putBytes(p_data).hash().toString(); case "crc-32c": return Hashing.crc32c().newHasher().putBytes(p_data).hash().toString(); case "murmur3-32": return Hashing.murmur3_32().newHasher().putBytes(p_data).hash().toString(); case "murmur3-128": return Hashing.murmur3_128().newHasher().putBytes(p_data).hash().toString(); case "siphash-2-4": return Hashing.sipHash24().newHasher().putBytes(p_data).hash().toString(); default: try { return BaseEncoding.base16().encode(MessageDigest.getInstance(p_algorithm).digest(p_data)) .toLowerCase(Locale.ROOT); } catch (final NoSuchAlgorithmException l_exception) { throw new CRuntimeException(l_exception, p_context); } } }
From source file:org.lightjason.agentspeak.action.buildin.crypto.CHash.java
/** * runs hashing function with difference between Google Guava hashing and Java default digest * * @param p_algorithm algorithm name//from ww w . ja v a 2 s. co m * @param p_data byte data representation * @return hash value * * @throws NoSuchAlgorithmException on unknown hashing algorithm */ private String hash(final String p_algorithm, final byte[] p_data) throws NoSuchAlgorithmException { switch (p_algorithm.trim().toLowerCase(Locale.ROOT)) { case "adler-32": return Hashing.adler32().newHasher().putBytes(p_data).hash().toString(); case "crc-32": return Hashing.crc32().newHasher().putBytes(p_data).hash().toString(); case "crc-32c": return Hashing.crc32c().newHasher().putBytes(p_data).hash().toString(); case "murmur3-32": return Hashing.murmur3_32().newHasher().putBytes(p_data).hash().toString(); case "murmur3-128": return Hashing.murmur3_128().newHasher().putBytes(p_data).hash().toString(); case "siphash-2-4": return Hashing.sipHash24().newHasher().putBytes(p_data).hash().toString(); default: return String.format("%032x", new BigInteger(1, MessageDigest.getInstance(p_algorithm).digest(p_data))); } }
From source file:com.facebook.buck.zip.ZipDirectoryWithMaxDeflateStep.java
private void addDirectoryToZipEntryList(File directory, String currentPath, ImmutableMap.Builder<File, ZipEntry> zipEntriesBuilder) throws IOException { Preconditions.checkNotNull(currentPath); for (File inputFile : directory.listFiles()) { String childPath = currentPath + (currentPath.isEmpty() ? "" : "/") + inputFile.getName(); if (inputFile.isDirectory()) { addDirectoryToZipEntryList(inputFile, childPath, zipEntriesBuilder); } else {/*from w ww.j av a 2 s. c om*/ ZipEntry nextEntry = new ZipEntry(childPath); long fileLength = inputFile.length(); if (fileLength > maxDeflatedBytes || EXTENSIONS_NOT_TO_DEFLATE.contains(Files.getFileExtension(inputFile.getName()))) { nextEntry.setMethod(ZipEntry.STORED); nextEntry.setCompressedSize(inputFile.length()); nextEntry.setSize(inputFile.length()); HashCode crc = ByteStreams.hash(Files.newInputStreamSupplier(inputFile), Hashing.crc32()); nextEntry.setCrc(crc.padToLong()); } zipEntriesBuilder.put(inputFile, nextEntry); } } }
From source file:com.google.devtools.build.buildjar.JarHelper.java
/** * Writes an entry with specific contents to the jar. Directory entries must * include the trailing '/'.//from w w w. j a v a2s .co m */ protected void writeEntry(JarOutputStream out, String name, byte[] content) throws IOException { if (names.add(name)) { // Create a new entry JarEntry entry = new JarEntry(name); entry.setTime(newEntryTimeMillis(name)); int size = content.length; entry.setSize(size); if (size == 0) { entry.setMethod(JarEntry.STORED); entry.setCrc(0); out.putNextEntry(entry); } else { entry.setMethod(storageMethod); if (storageMethod == JarEntry.STORED) { entry.setCrc(Hashing.crc32().hashBytes(content).padToLong()); } out.putNextEntry(entry); out.write(content); } out.closeEntry(); } }
From source file:org.jclouds.ec2.compute.options.EC2TemplateOptions.java
@Override public ToStringHelper string() { ToStringHelper toString = super.string(); if (groupNames.size() != 0) toString.add("groupNames", groupNames); if (noKeyPair) toString.add("noKeyPair", noKeyPair); toString.add("keyPair", keyPair); if (userData != null && userData.size() > 0) toString.add("userDataCksum", Hashing.crc32().hashBytes(Bytes.toArray(userData))); ImmutableSet<BlockDeviceMapping> mappings = blockDeviceMappings.build(); if (mappings.size() != 0) toString.add("blockDeviceMappings", mappings); return toString; }