List of usage examples for java.io OutputStream close
public void close() throws IOException
From source file:com.openmeap.util.ServletUtils.java
static final public File tempFileFromFileItem(String temporaryStoragePath, FileItem item) throws IOException { File destinationFile = File.createTempFile("uploadArchive", "", new File(temporaryStoragePath)); InputStream is = item.getInputStream(); OutputStream os = new FileOutputStream(destinationFile); try {/*from w w w . j a va 2 s. com*/ Utils.pipeInputStreamIntoOutputStream(is, os); } finally { is.close(); os.close(); } return destinationFile; }
From source file:com.jivesoftware.os.jive.utils.shell.utils.Untar.java
public static List<File> unTar(boolean verbose, final File outputDir, final File inputFile, boolean deleteOriginal) throws FileNotFoundException, IOException, ArchiveException { if (verbose) { System.out.println(String.format("untaring %s to dir %s.", inputFile.getAbsolutePath(), outputDir.getAbsolutePath())); }/* w ww .j a va 2 s .com*/ final List<File> untaredFiles = new LinkedList<>(); final InputStream is = new FileInputStream(inputFile); final TarArchiveInputStream debInputStream = (TarArchiveInputStream) new ArchiveStreamFactory() .createArchiveInputStream("tar", is); TarArchiveEntry entry = null; while ((entry = (TarArchiveEntry) debInputStream.getNextEntry()) != null) { String entryName = entry.getName(); entryName = entryName.substring(entryName.indexOf("/") + 1); final File outputFile = new File(outputDir, entryName); if (entry.isDirectory()) { if (verbose) { System.out.println(String.format("Attempting to write output directory %s.", getRelativePath(outputDir, outputFile))); } if (!outputFile.exists()) { if (verbose) { System.out.println(String.format("Attempting to create output directory %s.", getRelativePath(outputDir, outputFile))); } if (!outputFile.mkdirs()) { throw new IllegalStateException(String.format("Couldn't create directory %s.", getRelativePath(outputDir, outputFile))); } } } else { try { if (verbose) { System.out.println( String.format("Creating output file %s.", getRelativePath(outputDir, outputFile))); } outputFile.getParentFile().mkdirs(); final OutputStream outputFileStream = new FileOutputStream(outputFile); IOUtils.copy(debInputStream, outputFileStream); outputFileStream.close(); if (getRelativePath(outputDir, outputFile).contains("bin/") || outputFile.getName().endsWith(".sh")) { // Hack! if (verbose) { System.out.println( String.format("chmod +x file %s.", getRelativePath(outputDir, outputFile))); } outputFile.setExecutable(true); } } catch (Exception x) { System.err.println("failed to untar " + getRelativePath(outputDir, outputFile) + " " + x); } } untaredFiles.add(outputFile); } debInputStream.close(); if (deleteOriginal) { FileUtils.forceDelete(inputFile); if (verbose) { System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath())); } } return untaredFiles; }
From source file:io.undertow.server.handlers.FixedLengthRequestTestCase.java
@BeforeClass public static void setup() { final BlockingHandler blockingHandler = new BlockingHandler(); DefaultServer.setRootHandler(blockingHandler); blockingHandler.setRootHandler(new HttpHandler() { @Override// w w w .ja v a 2s . c om public void handleRequest(final HttpServerExchange exchange) { try { if (connection == null) { connection = exchange.getConnection(); } else if (!DefaultServer.isAjp() && !DefaultServer.isProxy() && connection != exchange.getConnection()) { exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR); final OutputStream outputStream = exchange.getOutputStream(); outputStream.write("Connection not persistent".getBytes()); outputStream.close(); return; } final OutputStream outputStream = exchange.getOutputStream(); final InputStream inputStream = exchange.getInputStream(); String m = HttpClientUtils.readResponse(inputStream); Assert.assertEquals(message, m); inputStream.close(); outputStream.close(); } catch (IOException e) { exchange.getResponseHeaders().put(Headers.CONNECTION, "close"); exchange.setStatusCode(StatusCodes.INTERNAL_SERVER_ERROR); throw new RuntimeException(e); } } }); }
From source file:com.cdd.bao.Main.java
private static void compileSchema(String[] options) throws Exception { List<String> inputFiles = new ArrayList<>(); for (int n = 0; n < options.length - 1; n++) inputFiles.add(Util.expandFileHome(options[n])); String outputFile = Util.expandFileHome(options[options.length - 1]); Util.writeln("Compiling schema files:"); for (int n = 0; n < inputFiles.size(); n++) Util.writeln(" " + inputFiles.get(n)); Util.writeln("Output to:"); Util.writeln(" " + outputFile); //loadupVocab(); Vocabulary vocab = new Vocabulary(); Util.writeFlush("Loading ontologies "); vocab.addListener(new Vocabulary.Listener() { public void vocabLoadingProgress(Vocabulary vocab, float progress) { Util.writeFlush("."); }// www . j a v a2 s .c o m public void vocabLoadingException(Exception ex) { ex.printStackTrace(); } }); vocab.load(null, null); Util.writeln(); Schema[] schemata = new Schema[inputFiles.size()]; for (int n = 0; n < schemata.length; n++) schemata[n] = ModelSchema.deserialise(new File(inputFiles.get(n))); SchemaVocab schvoc = new SchemaVocab(vocab, schemata); Util.writeln("Loaded: " + schvoc.numTerms() + " terms."); OutputStream ostr = new FileOutputStream(outputFile); schvoc.serialise(ostr); ostr.close(); Util.writeln("Done."); }
From source file:com.jivesoftware.os.jive.utils.shell.utils.Unzip.java
public static File unGzip(boolean verbose, File outputDir, String outName, File inputFile, boolean deleteOriginal) throws FileNotFoundException, IOException { String inFilePath = inputFile.getAbsolutePath(); if (verbose) { System.out.println("unzipping " + inFilePath); }//from w w w . j a v a 2 s . c om GZIPInputStream gzipInputStream = new GZIPInputStream(new FileInputStream(inFilePath)); File outFile = new File(outputDir, outName); outFile.getParentFile().mkdirs(); String outFilePath = outFile.getAbsolutePath(); OutputStream out = new FileOutputStream(outFilePath); byte[] buf = new byte[1024]; int len; while ((len = gzipInputStream.read(buf)) > 0) { out.write(buf, 0, len); } gzipInputStream.close(); out.close(); if (deleteOriginal) { FileUtils.forceDelete(inputFile); if (verbose) { System.out.println(String.format("deleted original file %s.", inputFile.getAbsolutePath())); } } if (verbose) { System.out.println("unzipped " + inFilePath); } return new File(outFilePath); }
From source file:Main.java
public static void copyAssets(Context context, String dir, String fileName) { // String[] files; File mWorkingPath = new File(dir); if (!mWorkingPath.exists()) { if (!mWorkingPath.mkdirs()) { }// w w w. ja va 2 s . com } try { InputStream in = context.getAssets().open(fileName); System.err.println(""); File outFile = new File(mWorkingPath, fileName); OutputStream out = new FileOutputStream(outFile); // Transfer bytes from in to out byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (IOException e1) { e1.printStackTrace(); } }
From source file:Main.java
public static void inputStreamToFile(InputStream is, File file) { OutputStream os = null; try {// www.j a va 2 s . c om os = new FileOutputStream(file); int bytesRead = 0; byte[] buffer = new byte[3072]; while ((bytesRead = is.read(buffer, 0, 3072)) != -1) { os.write(buffer, 0, bytesRead); } os.close(); is.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
From source file:GoogleImages.java
public static void saveImage(String imageUrl, String destinationFile) throws IOException { URL url = new URL(imageUrl); InputStream is = url.openStream(); OutputStream os = new FileOutputStream(destinationFile); byte[] b = new byte[2048]; int length;/*from w w w .jav a2 s.c om*/ while ((length = is.read(b)) != -1) { os.write(b, 0, length); } is.close(); os.close(); }
From source file:Main.java
private static File createFileFromInputStream(InputStream inputStream, String name) { try {//from ww w . j a v a2 s .c o m File f = File.createTempFile("font", null); OutputStream outputStream = new FileOutputStream(f); byte buffer[] = new byte[1024]; int length = 0; while ((length = inputStream.read(buffer)) > 0) { outputStream.write(buffer, 0, length); } outputStream.close(); inputStream.close(); return f; } catch (Exception e) { // Logging exception e.printStackTrace(); } return null; }
From source file:chen.android.toolkit.network.HttpConnection.java
/** * </br><b>title : </b> ????POST?? * </br><b>description :</b>????POST?? * </br><b>time :</b> 2012-7-8 ?4:34:08 * @param method ??//from w w w . j a v a 2 s . c om * @param url POSTURL * @param datas ???? * @return ??? * @throws IOException */ private static InputStream connect(String method, URL url, byte[]... datas) throws IOException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoOutput(true); conn.setDoInput(true); conn.setRequestMethod(method); conn.setUseCaches(false); conn.setInstanceFollowRedirects(true); conn.setConnectTimeout(6 * 1000); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Charset", "UTF-8"); OutputStream outputStream = conn.getOutputStream(); for (byte[] data : datas) { outputStream.write(data); } outputStream.close(); return conn.getInputStream(); }