/** * Save the content of a NBT compound to a stream. * <p> * Use {@link Files#newOutputStreamSupplier(java.io.File)} to provide a stream supplier to a file. * @param source - the NBT compound to save. * @param stream - the stream. * @param option - whether or not to compress the output. * @throws IOException If anything went wrong. */ public static void saveStream(NbtCompound source, OutputSupplier<? extends OutputStream> stream, StreamOptions option) throws IOException { OutputStream output = null; DataOutputStream data = null; boolean suppress = true; try { output = stream.getOutput(); data = new DataOutputStream( option == StreamOptions.GZIP_COMPRESSION ? new GZIPOutputStream(output) : output ); invokeMethod(get().SAVE_COMPOUND, null, source.getHandle(), data); suppress = false; } finally { if (data != null) Closeables.close(data, suppress); else if (output != null) Closeables.close(output, suppress); } }
private void saveJvmOptions(Map<String, LocalFile> localFiles) throws IOException { if ((extraOptions == null || extraOptions.isEmpty()) && JvmOptions.DebugOptions.NO_DEBUG.equals(debugOptions)) { // If no vm options, no need to localize the file. return; } LOG.debug("Create and copy {}", Constants.Files.JVM_OPTIONS); final Location location = createTempLocation(Constants.Files.JVM_OPTIONS); JvmOptionsCodec.encode(new JvmOptions(extraOptions, debugOptions), new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return new OutputStreamWriter(location.getOutputStream(), Charsets.UTF_8); } }); LOG.debug("Done {}", Constants.Files.JVM_OPTIONS); localFiles.put(Constants.Files.JVM_OPTIONS, createLocalFile(Constants.Files.JVM_OPTIONS, location)); }
@Test public void testNoNulls() throws Exception { JvmOptions options = new JvmOptions("-version", new JvmOptions.DebugOptions(true, false, ImmutableSet.of("one", "two"))); final StringWriter writer = new StringWriter(); JvmOptionsCodec.encode(options, new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return writer; } }); JvmOptions options1 = JvmOptionsCodec.decode(new InputSupplier<Reader>() { @Override public Reader getInput() throws IOException { return new StringReader(writer.toString()); } }); Assert.assertEquals(options.getExtraOptions(), options1.getExtraOptions()); Assert.assertEquals(options.getDebugOptions().doDebug(), options1.getDebugOptions().doDebug()); Assert.assertEquals(options.getDebugOptions().doSuspend(), options1.getDebugOptions().doSuspend()); Assert.assertEquals(options.getDebugOptions().getRunnables(), options1.getDebugOptions().getRunnables()); }
@Test public void testSomeNulls() throws Exception { JvmOptions options = new JvmOptions(null, new JvmOptions.DebugOptions(false, false, null)); final StringWriter writer = new StringWriter(); JvmOptionsCodec.encode(options, new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return writer; } }); JvmOptions options1 = JvmOptionsCodec.decode(new InputSupplier<Reader>() { @Override public Reader getInput() throws IOException { return new StringReader(writer.toString()); } }); Assert.assertEquals(options.getExtraOptions(), options1.getExtraOptions()); Assert.assertEquals(options.getDebugOptions().doDebug(), options1.getDebugOptions().doDebug()); Assert.assertEquals(options.getDebugOptions().doSuspend(), options1.getDebugOptions().doSuspend()); Assert.assertEquals(options.getDebugOptions().getRunnables(), options1.getDebugOptions().getRunnables()); }
@Test public void testNoRunnables() throws Exception { List<String> noRunnables = Collections.emptyList(); JvmOptions options = new JvmOptions(null, new JvmOptions.DebugOptions(true, false, noRunnables)); final StringWriter writer = new StringWriter(); JvmOptionsCodec.encode(options, new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return writer; } }); JvmOptions options1 = JvmOptionsCodec.decode(new InputSupplier<Reader>() { @Override public Reader getInput() throws IOException { return new StringReader(writer.toString()); } }); Assert.assertEquals(options.getExtraOptions(), options1.getExtraOptions()); Assert.assertEquals(options.getDebugOptions().doDebug(), options1.getDebugOptions().doDebug()); Assert.assertEquals(options.getDebugOptions().doSuspend(), options1.getDebugOptions().doSuspend()); Assert.assertEquals(options.getDebugOptions().getRunnables(), options1.getDebugOptions().getRunnables()); }
/** * Save the content of a NBT compound to a stream. * <p> * Use {@link Files#newOutputStreamSupplier(java.io.File)} to provide a stream supplier to a file. * * @param source - the NBT compound to save. * @param stream - the stream. * @param option - whether or not to compress the output. * @throws IOException If anything went wrong. */ public static void saveStream(NbtCompound source, OutputSupplier<? extends OutputStream> stream, StreamOptions option) throws IOException { OutputStream output = null; DataOutputStream data = null; boolean suppress = true; try { output = stream.getOutput(); data = new DataOutputStream( option == StreamOptions.GZIP_COMPRESSION ? new GZIPOutputStream(output) : output ); invokeMethod(get().SAVE_COMPOUND, null, source.getHandle(), data); suppress = false; } finally { if (data != null) Closeables.close(data, suppress); else if (output != null) Closeables.close(output, suppress); } }
private void createArchive(URI baseDir) throws IOException { File directory = new File(java.net.URI.create(baseDir.toString())); File lib = new File(directory, "lib"); assertTrue(lib.mkdir()); File nfar = new File(lib, host.archiveProjectId + ".nfar"); final ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(nfar)); zipOutputStream.putNextEntry(new ZipEntry("src/A.js")); zipOutputStream.putNextEntry(new ZipEntry("src/B.js")); zipOutputStream.putNextEntry(new ZipEntry("src/sub/B.js")); zipOutputStream.putNextEntry(new ZipEntry("src/sub/C.js")); zipOutputStream.putNextEntry(new ZipEntry("src/sub/leaf/D.js")); zipOutputStream.putNextEntry(new ZipEntry(IN4JSProject.N4MF_MANIFEST)); // this will close the stream CharStreams.write("ProjectId: " + host.archiveProjectId + "\n" + "ProjectType: library\n" + "ProjectVersion: 0.0.1-SNAPSHOT\n" + "VendorId: org.eclipse.n4js\n" + "VendorName: \"Eclipse N4JS Project\"\n" + "Output: \"src-gen\"\n" + "Sources {\n" + " source {" + " \"src\"\n" + " }\n" + "}", CharStreams.newWriterSupplier(new OutputSupplier<ZipOutputStream>() { @Override public ZipOutputStream getOutput() throws IOException { return zipOutputStream; } }, Charsets.UTF_8)); host.setArchiveFileURI(URI.createURI(nfar.toURI().toString())); }
private void createArchive(String projectName) throws CoreException, IOException { IProject project = workspace.getProject(projectName); IFolder libFolder = project.getFolder(LIB_FOLDER_NAME); libFolder.create(false, true, null); IFile archiveFile = libFolder.getFile(host.archiveProjectId + ".nfar"); ByteArrayOutputStream byteArrayOutputSteam = new ByteArrayOutputStream(); final ZipOutputStream zipOutputStream = new ZipOutputStream(byteArrayOutputSteam); zipOutputStream.putNextEntry(new ZipEntry("src/A.js")); zipOutputStream.putNextEntry(new ZipEntry("src/B.js")); zipOutputStream.putNextEntry(new ZipEntry("src/sub/B.js")); zipOutputStream.putNextEntry(new ZipEntry("src/sub/C.js")); zipOutputStream.putNextEntry(new ZipEntry("src/sub/leaf/D.js")); zipOutputStream.putNextEntry(new ZipEntry(IN4JSProject.N4MF_MANIFEST)); // this will close the stream CharStreams.write("ProjectId: " + host.archiveProjectId + "\n" + "ProjectType: library\n" + "ProjectVersion: 0.0.1-SNAPSHOT\n" + "VendorId: org.eclipse.n4js\n" + "VendorName: \"Eclipse N4JS Project\"\n" + "Libraries { \"" + LIB_FOLDER_NAME + "\"\n }\n" + "Output: \"src-gen\"" + "Sources {\n" + " source { " + " \"src\"\n" + " }\n" + "}\n", CharStreams.newWriterSupplier(new OutputSupplier<ZipOutputStream>() { @Override public ZipOutputStream getOutput() throws IOException { return zipOutputStream; } }, Charsets.UTF_8)); archiveFile.create(new ByteArrayInputStream(byteArrayOutputSteam.toByteArray()), false, null); host.setArchiveFileURI(URI.createPlatformResourceURI(archiveFile.getFullPath().toString(), true)); }
private void saveArguments(Arguments arguments, final Path targetPath) throws IOException { LOG.debug("Creating {}", targetPath); ArgumentsCodec.encode(arguments, new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return Files.newBufferedWriter(targetPath, StandardCharsets.UTF_8); } }); LOG.debug("Done {}", targetPath); }
public static void serialize(final Persister persister, final Object object, final OutputSupplier<? extends OutputStream> outputSupplier) throws IOException { final OutputStream output = outputSupplier.getOutput(); boolean threw = true; try { persister.serialize(object, output); threw = false; } finally { Closeables.close(output, threw); } }
private void doFileExtract(File path) throws IOException { InputStream inputStream = getClass().getResourceAsStream("/"+getContainedFile()); OutputSupplier<FileOutputStream> outputSupplier = Files.newOutputStreamSupplier(path); System.out.println("doFileExtract path = " + path.getAbsolutePath() + ", inputStream = " + inputStream + ", outputSupplier = " + outputSupplier); ByteStreams.copy(inputStream, outputSupplier); }
private void saveArguments(Arguments arguments, Map<String, LocalFile> localFiles) throws IOException { LOG.debug("Create and copy {}", Constants.Files.ARGUMENTS); final Location location = createTempLocation(Constants.Files.ARGUMENTS); ArgumentsCodec.encode(arguments, new OutputSupplier<Writer>() { @Override public Writer getOutput() throws IOException { return new OutputStreamWriter(location.getOutputStream(), Charsets.UTF_8); } }); LOG.debug("Done {}", Constants.Files.ARGUMENTS); localFiles.put(Constants.Files.ARGUMENTS, createLocalFile(Constants.Files.ARGUMENTS, location)); }
public static void encode(JvmOptions jvmOptions, OutputSupplier<? extends Writer> writerSupplier) throws IOException { Writer writer = writerSupplier.getOutput(); try { GSON.toJson(jvmOptions, writer); } finally { writer.close(); } }
public static void encode(Arguments arguments, OutputSupplier<? extends Writer> writerSupplier) throws IOException { Writer writer = writerSupplier.getOutput(); try { GSON.toJson(arguments, writer); } finally { writer.close(); } }
/** * returns a byte array representing the input byte array, gzipped. Use this method with caution - for large arrays, * you should use streams instead. * * @param input the content to compress * @return the gzipped input * @throws IOException if there is a problem zipping the content */ public static byte[] gzip(byte[] input) throws IOException { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteStreams.write(input, new OutputSupplier<OutputStream>() { @Override public OutputStream getOutput() throws IOException { return new GZIPOutputStream(baos); } }); baos.close(); return baos.toByteArray(); }
public static void encode(Arguments arguments, OutputSupplier<? extends Writer> writerSupplier) throws IOException { try (Writer writer = writerSupplier.getOutput()) { GSON.toJson(arguments, writer); } }
@Override // Saves a string remotely using tee. public void saveStringToFile(String content, String filename, boolean useSudo) throws RemoteConnectionException { try { String cmd; if (!useSudo) { cmd = "tee " + filename; } else { cmd = "sudo tee " + filename; } final RemoteProcess teeProcess = execCmd(cmd, false); try { CharStreams.write(content, new OutputSupplier<OutputStreamWriter>() { @Override public OutputStreamWriter getOutput() throws IOException { return new OutputStreamWriter(teeProcess.getOutputStream()); } }); teeProcess.waitFor(); String str = CharStreams.toString(new InputStreamReader(teeProcess.getInputStream())); String errStr = CharStreams.toString(new InputStreamReader(teeProcess.getErrorStream())); // Check Exit-Status if (teeProcess.getExitStatus() != 0) { LOGGER.error("Could not write to file %s: %s", filename, errStr); throw new RemoteConnectionException("Could not write to file " + filename); } // Check everything correctly written if (!content.equals(str)) { LOGGER.error("Write failed: Expected %s but got %s", content, str); throw new RemoteConnectionException("Could not write to file " + filename); } } finally { if (teeProcess != null) { teeProcess.finish(); } } } catch (IOException e) { LOGGER.error("saveStringToFile failed", e); throw new RemoteConnectionException(e); } }
/** * Save the content of a NBT compound to a stream. * <p> * Use {@link Files#newOutputStreamSupplier(java.io.File)} to provide a stream supplier to a file. * @param stream - the output stream. * @param option - whether or not to compress the output. * @throws IOException If anything went wrong. */ public void saveTo(OutputSupplier<? extends OutputStream> stream, StreamOptions option) throws IOException { saveStream(this, stream, option); }
/** * Save the content of a NBT compound to a stream. * <p> * Use {@link Files#newOutputStreamSupplier(java.io.File)} to provide a stream supplier to a file. * * @param stream - the output stream. * @param option - whether or not to compress the output. * @throws IOException If anything went wrong. */ public void saveTo(OutputSupplier<? extends OutputStream> stream, StreamOptions option) throws IOException { saveStream(this, stream, option); }