List of usage examples for com.google.common.io ByteStreams toByteArray
public static byte[] toByteArray(InputStream in) throws IOException
From source file:demo.producer.ThumbnailPublisher.java
public static void main(String[] args) { Properties properties = new Properties(); properties.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092"); properties.setProperty(ProducerConfig.CLIENT_ID_CONFIG, "thumbnail-publisher"); Path path = FileSystems.getDefault().getPath("/run/media/tsachev/Hand-On Kaf/images_2016_08/train", "images.csv"); try (Producer<String, Thumbnail> producer = new KafkaProducer<>(properties, new StringSerializer(), new ThumbnailSerializer())) { ThumbnailPublisher publisher = new ThumbnailPublisher(producer); Files.lines(path).skip(1).limit(1_000_000) //.parallel() .forEach(line -> {/*from w ww. j av a2s . com*/ // ImageID,Subset,OriginalURL,OriginalLandingURL,License,AuthorProfileURL,Author,Title,OriginalSize,OriginalMD5,Thumbnail300KURL String[] values = line.split("[,]"); String imageId = values[0]; String title = values[7]; String url = values[2]; String thumbnailUrl; byte[] content; try { thumbnailUrl = values[10]; content = ByteStreams.toByteArray(new URL(thumbnailUrl).openStream()); } catch (Exception e) { return; } publisher.publishThumbnail(imageId, title, url, content); }); } catch (Exception e) { e.printStackTrace(); } }
From source file:org.prebake.service.Main.java
public static final void main(String[] argv) { // The prebakery does not read stdin and neither should any execed process. Closeables.closeQuietly(System.in); final Logger logger = Logger.getLogger(Main.class.getName()); CommandLineArgs args = new CommandLineArgs(argv); if (!CommandLineArgs.setUpLogger(args, logger)) { System.out.println(USAGE); System.exit(0);//from w ww.j a va 2 s . c om } FileSystem fs = FileSystems.getDefault(); Config config; { MessageQueue mq = new MessageQueue(); config = new CommandLineConfig(fs, mq, args); if (mq.hasErrors()) { System.err.println(USAGE); System.err.println(); for (String msg : mq.getMessages()) { System.err.println(msg); } System.exit(-1); } } ImmutableMap<String, ?> env = CommonEnvironment.makeEnvironment(config.getClientRoot().getRoot(), getSystemPropertyMap()); ScheduledExecutorService execer = MoreExecutors .getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(16)); OperatingSystem os = new RealOperatingSystem(fs, execer); final String token; { byte[] bytes = new byte[256]; new SecureRandom().nextBytes(bytes); token = new BigInteger(bytes).toString(Character.MAX_RADIX); } final Clock clock = SystemClock.instance(); final Path clientRoot = config.getClientRoot(); final LogHydra hydra = new LogHydra(clientRoot.resolve(FileNames.DIR).resolve(FileNames.LOGS), clock) { @Override protected void doInstall(OutputStream[] wrappedInheritedProcessStreams, Handler logHandler) { System.setOut(new PrintStream(wrappedInheritedProcessStreams[0], true)); System.setErr(new PrintStream(wrappedInheritedProcessStreams[1], true)); logger.addHandler(logHandler); } }; hydra.install(new FileOutputStream(FileDescriptor.out), new FileOutputStream(FileDescriptor.err)); final HighLevelLog highLevelLog = new HighLevelLog(clock); final Logs logs = new Logs(highLevelLog, logger, hydra); final Prebakery pb = new Prebakery(config, env, execer, os, logs) { @Override protected String makeToken() { return token; } @Override protected int openChannel(int portHint, final BlockingQueue<Commands> q) throws IOException { final ServerSocket ss = new ServerSocket(portHint); Thread th = new Thread(new Runnable() { public void run() { while (true) { try { boolean closeSock = true; Socket sock = ss.accept(); // TODO: move sock handling to a worker or use java.nio stuff. try { byte[] bytes = ByteStreams.toByteArray(sock.getInputStream()); sock.shutdownInput(); String commandText = new String(bytes, Charsets.UTF_8); try { q.put(Commands.fromJson(clientRoot, new JsonSource(new StringReader(commandText)), sock.getOutputStream())); // Closing sock is now the service's responsibility. closeSock = false; } catch (InterruptedException ex) { continue; } } finally { if (closeSock) { sock.close(); } } } catch (IOException ex) { logger.log(Level.WARNING, "Connection failed", ex); } } } }, Main.class.getName() + "#command_receiver"); th.setDaemon(true); th.start(); return ss.getLocalPort(); } @Override protected Environment createDbEnv(Path dir) { EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); return new Environment(new File(dir.toUri()), envConfig); } @Override protected Map<?, ?> getSysProps() { return Collections.unmodifiableMap(System.getProperties()); } @Override protected Map<String, String> getSysEnv() { return Collections.unmodifiableMap(new ProcessBuilder().environment()); } }; final Server server; if (config.getWwwPort() > 0) { server = new Server(config.getWwwPort()) { @Override public String toString() { return "[Prebake Web Server]"; } }; server.setSendServerVersion(false); server.setHandler(new AbstractHandler() { MainServlet servlet = new MainServlet(token, pb, TimeZone.getDefault()); public void handle(String tgt, Request r, HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException { try { servlet.service(req, resp); } catch (RuntimeException ex) { logger.log(Level.WARNING, "Web request failed", ex); throw ex; } } }); server.setStopAtShutdown(true); try { server.start(); } catch (Exception ex) { logger.log(Level.SEVERE, "Failed to start http server on port " + config.getWwwPort(), ex); } } else { server = null; } // If an interrupt signal is received, Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { public void run() { pb.close(); try { if (server != null && !(server.isStopping() || server.isStopped())) { server.stop(); } } catch (Throwable th) { logger.log(Level.SEVERE, "Error shutting down http server", th); // Don't propagate since the process will soon be dead anyway. } } })); final boolean[] exitMutex = new boolean[1]; synchronized (exitMutex) { // Start the prebakery with a handler that will cause the main thread to // complete when it receives a shutdown command or is programatically // closed. pb.start(new Runnable() { public void run() { // When a shutdown command is received, signal the main thread so // it can complete. synchronized (exitMutex) { exitMutex[0] = true; exitMutex.notifyAll(); } } }); // The loop below gets findbugs to shut up about a wait outside a loop. while (!exitMutex[0]) { try { exitMutex.wait(); logger.log(Level.INFO, "Shut down cleanly"); } catch (InterruptedException ex) { // Just exit below if the main thread is interrupted. break; } } } System.exit(0); }
From source file:net.fabricmc.base.util.mixin.MixinPrebaker.java
public static void main(String[] args) { boolean hasMappingsFile = false; if (args.length < 3) { System.out.println("usage: MixinPrebaker [-m mapping-file] <input-jar> <output-jar> <mod-jars...>"); return;/*from ww w . j a va 2 s.c om*/ } int argOffset; for (argOffset = 0; argOffset < args.length; argOffset++) { if ("-m".equals(args[argOffset])) { hasMappingsFile = true; FabricMixinBootstrap.setMappingFile(new File(args[++argOffset])); } else { break; } } Set<File> modFiles = new HashSet<>(); for (int i = argOffset + 2; i < args.length; i++) { modFiles.add(new File(args[i])); } URLClassLoader ucl = (URLClassLoader) MixinPrebaker.class.getClassLoader(); Launch.classLoader = new LaunchClassLoader(ucl.getURLs()); Launch.blackboard = new HashMap<>(); Launch.blackboard.put(Blackboard.Keys.TWEAKS, Collections.emptyList()); MixinLoader mixinLoader = new MixinLoader(); mixinLoader.load(modFiles); FabricMixinBootstrap.init(Side.UNIVERSAL, mixinLoader); MixinEnvironment.EnvironmentStateTweaker tweaker = new MixinEnvironment.EnvironmentStateTweaker(); tweaker.getLaunchArguments(); tweaker.injectIntoClassLoader(Launch.classLoader); MixinTransformer mixinTransformer = Blackboard.get(Blackboard.Keys.TRANSFORMER); try { JarInputStream input = new JarInputStream(new FileInputStream(new File(args[argOffset + 0]))); JarOutputStream output = new JarOutputStream(new FileOutputStream(new File(args[argOffset + 1]))); JarEntry entry; while ((entry = input.getNextJarEntry()) != null) { if (entry.getName().equals(FabricMixinBootstrap.APPLIED_MIXIN_CONFIGS_FILENAME)) { continue; } if (hasMappingsFile && entry.getName().equals(FabricMixinBootstrap.MAPPINGS_FILENAME)) { continue; } if (entry.getName().endsWith(".class")) { byte[] classIn = ByteStreams.toByteArray(input); String className = entry.getName().substring(0, entry.getName().length() - 6).replace('/', '.'); byte[] classOut = mixinTransformer.transform(className, className, classIn); if (classIn != classOut) { System.out.println("Transformed " + className); classOut = desprinkle(classOut); } JarEntry newEntry = new JarEntry(entry.getName()); newEntry.setComment(entry.getComment()); newEntry.setSize(classOut.length); newEntry.setLastModifiedTime(FileTime.from(Instant.now())); output.putNextEntry(newEntry); output.write(classOut); } else { output.putNextEntry(entry); ByteStreams.copy(input, output); } } output.putNextEntry(new JarEntry(FabricMixinBootstrap.APPLIED_MIXIN_CONFIGS_FILENAME)); output.write( Strings.join(FabricMixinBootstrap.getAppliedMixinConfigs(), "\n").getBytes(Charsets.UTF_8)); if (hasMappingsFile) { output.putNextEntry(new JarEntry(FabricMixinBootstrap.MAPPINGS_FILENAME)); Files.copy(FabricMixinBootstrap.getMappingFile().toPath(), output); } input.close(); output.close(); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:me.lucko.luckperms.common.dependencies.Dependency.java
public static void main(String[] args) throws Exception { MessageDigest digest = MessageDigest.getInstance("SHA-256"); for (Dependency dependency : values()) { URL url = new URL(dependency.getUrl()); try (InputStream in = url.openStream()) { byte[] bytes = ByteStreams.toByteArray(in); if (bytes.length == 0) { throw new RuntimeException("Empty stream"); }//from w w w. jav a 2 s. c o m byte[] hash = digest.digest(bytes); if (Arrays.equals(hash, dependency.getChecksum())) { System.out.println( "MATCH " + dependency.name() + ": " + Base64.getEncoder().encodeToString(hash)); } else { System.out.println( "NO MATCH " + dependency.name() + ": " + Base64.getEncoder().encodeToString(hash)); } } } }
From source file:mvm.rya.accumulo.pig.SparqlQueryPigEngine.java
public static void main(String[] args) { try {//from ww w.jav a 2 s . c om Preconditions.checkArgument(args.length == 7, "Usage: java -cp <jar>:$PIG_LIB <class> sparqlFile hdfsSaveLocation cbinstance cbzk cbuser cbpassword rdfTablePrefix.\n " + "Sample command: java -cp java -cp cloudbase.pig-2.0.0-SNAPSHOT-shaded.jar:/usr/local/hadoop-etc/hadoop-0.20.2/hadoop-0.20.2-core.jar:/srv_old/hdfs-tmp/pig/pig-0.9.2/pig-0.9.2.jar:$HADOOP_HOME/conf mvm.rya.accumulo.pig.SparqlQueryPigEngine " + "tstSpqrl.query temp/engineTest stratus stratus13:2181 root password l_"); String sparql = new String(ByteStreams.toByteArray(new FileInputStream(args[0]))); String hdfsSaveLocation = args[1]; SparqlToPigTransformVisitor visitor = new SparqlToPigTransformVisitor(); visitor.setTablePrefix(args[6]); visitor.setInstance(args[2]); visitor.setZk(args[3]); visitor.setUser(args[4]); visitor.setPassword(args[5]); SparqlQueryPigEngine engine = new SparqlQueryPigEngine(); engine.setSparqlToPigTransformVisitor(visitor); engine.setInference(false); engine.setStats(false); engine.init(); engine.runQuery(sparql, hdfsSaveLocation); engine.destroy(); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.google.android.mail.common.html.parser.HtmlTreeBuilder.java
/** For testing */ public static void main(String[] args) throws IOException { logger.setLevel(Level.FINEST); String html = new String(ByteStreams.toByteArray(System.in)); HtmlParser parser = new HtmlParser(); HtmlDocument doc = parser.parse(html); HtmlTreeBuilder builder = new HtmlTreeBuilder(); doc.accept(builder);//from www. j a v a2 s.com String outputHtml = builder.getTree().getHtml(); System.out.println(outputHtml); }
From source file:org.freelancertech.poc.itext.utils.MyFileUtils.java
private static byte[] fileStreamToGson(String filePath) throws IOException { final ClassLoader classLoader = MyFileUtils.class.getClassLoader(); return ByteStreams.toByteArray(classLoader.getResourceAsStream(filePath)); }
From source file:org.onos.yangtools.yang.parser.util.NamedByteArrayInputStream.java
public static ByteArrayInputStream create(InputStream originalIS) throws IOException { final byte[] data = ByteStreams.toByteArray(originalIS); if (originalIS instanceof NamedInputStream) { return new NamedByteArrayInputStream(data, originalIS.toString()); } else {//from ww w . ja v a 2s .c o m return new ByteArrayInputStream(data); } }
From source file:org.opendaylight.yangtools.yang.parser.util.NamedByteArrayInputStream.java
public static ByteArrayInputStream create(final InputStream originalIS) throws IOException { final byte[] data = ByteStreams.toByteArray(originalIS); if (originalIS instanceof NamedInputStream) { return new NamedByteArrayInputStream(data, originalIS.toString()); }//w w w . j a v a2s .co m return new ByteArrayInputStream(data); }
From source file:com.github.danzx.zekke.test.util.FilesUtil.java
public static String readClasspathFileToString(String resourceName) { try {/*from w w w . j a v a 2s. c om*/ byte[] bytes = ByteStreams.toByteArray(FilesUtil.class.getResourceAsStream(resourceName)); return new String(bytes); } catch (IOException e) { throw new RuntimeException(e); } }