Example usage for java.io PrintStream close

List of usage examples for java.io PrintStream close

Introduction

In this page you can find the example usage for java.io PrintStream close.

Prototype

public void close() 

Source Link

Document

Closes the stream.

Usage

From source file:com.marketplace.Utils.java

/**
 * Converts a list of Android permissions to a corresponding int value in
 * database. If a new permission is found, then that permission gets added
 * to an external file 'permission' found in the directory config.
 * // w w w  .j  a  va  2  s.c  o m
 * @param permissions
 *            a list of permissions that needs to be converted.
 * 
 * @return a list of corresponding int value.
 */
synchronized public static List<Integer> permissionToInt(List<String> permissions) {
    List<Integer> pListArr = new ArrayList<Integer>(permissions.size());

    for (String permission : permissions) {
        if (pMap.containsKey(permission)) {
            pListArr.add(pMap.get(permission));
        } else {
            try {
                log.info("New permission found. Adding to the database");

                PrintStream printStream = new PrintStream(new FileOutputStream(Utils.fileName, true), true);
                printStream.print(permission + "\t" + (pMap.size() + 1) + "\n");
                printStream.close();

                pMap.put(permission, pMap.size() + 1);
                new Sender().doBasicHttpPost("{\"permission\":\"" + permission + "\"}",
                        Constants.newPermissionUrlJson);

            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (ConnectivityException e) {
                log.info("There was a problem connecting to the database");
            }
        }
    }

    return pListArr;
}

From source file:com.sap.prd.mobile.ios.mios.CodeSignManager.java

private static ExecResult exec(String[] cmd) throws IOException {
    String cmdStr = StringUtils.join(cmd, " ");
    System.out.println("Invoking " + cmdStr);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintStream ps = new PrintStream(baos, true, "UTF-8");
    try {//  ww w  .ja  v a2  s  . c  om
        int exitValue = Forker.forkProcess(ps, null, "bash", "-c", cmdStr);
        return new ExecResult(cmdStr, baos.toString("UTF-8"), exitValue);
    } finally {
        ps.close();
    }
}

From source file:net.sourceforge.jweb.maven.mojo.InWarMinifyMojo.java

public static String buildStack(Throwable t) {
    ByteArrayOutputStream arrayOutputStream = new ByteArrayOutputStream();
    PrintStream printStream = new PrintStream(arrayOutputStream);
    t.printStackTrace(printStream);/*w  ww. j  av  a 2 s .  c o  m*/
    String ret = arrayOutputStream.toString();
    try {
        arrayOutputStream.close();
        printStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:com.moss.greenshell.wizard.catastrophe.PostMortemScreen.java

public static void submitErrorReport(final Throwable cause, final ErrorReportDecorator... decorators)
        throws Exception {

    List<ErrorReportChunk> chunks = new LinkedList<ErrorReportChunk>();

    try {/* w w  w.  j  a v  a2  s .  c om*/
        if (cause instanceof InternalErrorException) {
            InternalErrorException ie = (InternalErrorException) cause;
            ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain",
                    ie.id().getBytes("UTF8"));
            chunks.add(chunk);
        } else if (cause instanceof SOAPFaultException) {
            SOAPFaultException soapFault = (SOAPFaultException) cause;
            String content = soapFault.getFault().getFirstChild().getTextContent();
            String prefix = "Internal Service Error Occurred: ";
            if (content.startsWith(prefix)) {
                String id = content.substring(prefix.length());
                ErrorReportChunk chunk = new ErrorReportChunk("internal-error-id", "text/plain",
                        id.getBytes("UTF8"));
                chunks.add(chunk);
            }
        }
    } catch (Throwable t) {
        t.printStackTrace();
    }

    // STACK TRACE
    ByteArrayOutputStream stackBytes = new ByteArrayOutputStream();
    PrintStream stackPrintStream = new PrintStream(stackBytes);
    cause.printStackTrace(stackPrintStream);
    stackPrintStream.close();
    stackBytes.close();

    ErrorReportChunk chunk = new ErrorReportChunk("stack trace", "text/plain", stackBytes.toByteArray());
    chunks.add(chunk);

    // THREAD DUMP

    ByteArrayOutputStream dumpBytes = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(dumpBytes);
    Map<Thread, StackTraceElement[]> traceMap = Thread.getAllStackTraces();
    for (Map.Entry<Thread, StackTraceElement[]> next : traceMap.entrySet()) {
        out.println();
        out.println(next.getKey().getName());
        for (StackTraceElement line : next.getValue()) {
            String className = emptyIfNull(line.getClassName());
            String methodName = emptyIfNull(line.getMethodName());
            String fileName = emptyIfNull(line.getFileName());

            out.println("    " + className + "." + methodName + " (" + fileName + " line "
                    + line.getLineNumber() + ")");
        }
    }
    out.flush();
    out.close();
    ErrorReportChunk stackDump = new ErrorReportChunk("thread dump", "text/plain", dumpBytes.toByteArray());
    chunks.add(stackDump);

    // SYSTEM PROPERTIES
    ByteArrayOutputStream propsBytes = new ByteArrayOutputStream();
    PrintStream propsOut = new PrintStream(propsBytes);
    for (Map.Entry<Object, Object> next : System.getProperties().entrySet()) {
        propsOut.println(" " + next.getKey() + "=" + next.getValue());
    }
    propsOut.flush();
    propsOut.close();
    chunks.add(new ErrorReportChunk("system properties", "text/plain", propsBytes.toByteArray()));

    // LOCAL CLOCK
    chunks.add(new ErrorReportChunk("local clock", "text/plain", new DateTime().toString().getBytes()));

    // NETWORKING
    StringBuffer networking = new StringBuffer();
    Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
    while (ifaces.hasMoreElements()) {
        NetworkInterface iface = ifaces.nextElement();
        networking.append("INTERFACE: " + iface.getName() + " (" + iface.getDisplayName() + ")\n");
        Enumeration<InetAddress> addresses = iface.getInetAddresses();
        while (addresses.hasMoreElements()) {
            InetAddress address = addresses.nextElement();
            networking.append("  Address:" + address.getHostAddress() + "\n");
            networking.append("      Cannonical Host Name: " + address.getCanonicalHostName() + "\n");
            networking.append("                 Host Name: " + address.getHostName() + "\n");
        }
    }
    chunks.add(new ErrorReportChunk("network configuration", "text/plain", networking.toString().getBytes()));

    // DECORATORS
    if (decorators != null) {
        for (ErrorReportDecorator decorator : decorators) {
            chunks.addAll(decorator.makeChunks(cause));
        }
    }
    ErrorReport report = new ErrorReport(chunks);
    Reporter reporter = new Reporter();
    ReportId id = reporter.submitReport(report);
}

From source file:org.apache.hadoop.hdfs.TestFsShellPermission.java

static String execCmd(FsShell shell, final String[] args) throws Exception {
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baout, true);
    PrintStream old = System.out;
    System.setOut(out);/*from ww w.jav  a  2s.  c o m*/
    int ret = shell.run(args);
    out.close();
    System.setOut(old);
    return String.valueOf(ret);
}

From source file:fr.certu.chouette.plugin.report.Report.java

/**
 * pretty print a report in a stream/*w  ww.ja v  a  2  s  . c om*/
 * 
 * @param stream
 *           target stream
 * @param report
 *           report to print
 * @param closeOnExit
 *           close stream after print
 */
public static void print(PrintStream stream, Report report, boolean closeOnExit) {
    stream.println(report.getLocalizedMessage());
    printItems(stream, "", report.getItems());
    if (closeOnExit)
        stream.close();

}

From source file:gov.nih.nci.cagrid.identifiers.test.StressTestUtil.java

private synchronized static void writeToFile(String line) {
    FileOutputStream fout = null;
    try {//  w  w  w.j av a  2 s. c  o m
        fout = new FileOutputStream(fileName, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    if (fout != null) {
        PrintStream p = new PrintStream(fout);
        p.println(line);
        p.close();
    }
}

From source file:org.apache.hadoop.fs.CopyFilesBase.java

protected static String execCmd(FsShell shell, String... args) throws Exception {
    ByteArrayOutputStream baout = new ByteArrayOutputStream();
    PrintStream out = new PrintStream(baout, true);
    PrintStream old = System.out;
    System.setOut(out);//from   w  w  w .ja v  a2s.c  o  m
    shell.run(args);
    out.close();
    System.setOut(old);
    return baout.toString();
}

From source file:edu.stanford.muse.graph.directed.Digraph.java

/** input is a set of docid -> terms in the doc map 
 * @throws FileNotFoundException */
public static void doIt(Map<Integer, Collection<Collection<String>>> docMap, String outfile)
        throws FileNotFoundException {
    // index stores for each term, count of how many times it co-occurs with another in a doc.
    Map<String, Map<String, Integer>> index = new LinkedHashMap<String, Map<String, Integer>>();
    Map<String, Integer> termFreq = new LinkedHashMap<String, Integer>();

    // compute index
    for (Integer num : docMap.keySet()) {
        Collection<Collection<String>> paras = docMap.get(num);

        for (Collection<String> paraNames : paras) {
            System.out.println(num + ". " + paraNames.size() + " names " + " prev index size " + index.size()
                    + " term freq size " + termFreq.size());
            if (paraNames.size() > 100) {
                log.warn("skipping long para" + paraNames);
                continue;
            }/*  w  w  w.ja  v a2  s.  c om*/

            for (String s : paraNames) {
                s = s.toLowerCase();
                // bump up term freq for this term
                Integer X = termFreq.get(s);
                termFreq.put(s, (X == null) ? 1 : X + 1);

                // bump up counts for co-occurring terms... 
                // unfortunately n^2 operation here
                for (String s1 : paraNames) {
                    if (s == s1)
                        continue;

                    Map<String, Integer> termMap = index.get(s);
                    if (termMap == null) {
                        // allocate termMap if this is the first time we've seen s
                        termMap = new LinkedHashMap<String, Integer>(1);
                        index.put(s, termMap);
                    }

                    // bump the count
                    Integer I = termMap.get(s1);
                    termMap.put(s1, (I == null) ? 1 : I + 1);
                }
            }
        }
    }

    // process index and store it as a graph structure

    Digraph<String> graph = new Digraph<String>();
    for (String term : index.keySet()) {
        Map<String, Integer> map = index.get(term);
        if (map == null) {
            // no edges, just add it to the graph and continue
            graph.add(term);
            continue;
        }

        // compute total co-occurrence across all other terms this term is associated with
        int total = 0;
        for (Integer x : map.values())
            total += x;
        // proportionately allocate weight
        for (String x : map.keySet())
            graph.addEdge(term, x, ((float) map.get(x)));
        //      graph.addEdge(term, x, ((float) map.get(x))/total);
    }
    String s = graph.dump();
    PrintStream pw = new PrintStream(new FileOutputStream(outfile));
    pw.print(s);
    pw.close();
}

From source file:ja.lingo.application.util.messages.ErrorDumper.java

public static String dump(Throwable t) {
    LOG.error("Internal error occured", t);

    String fileName = EngineFiles.calculateInWorking("log");

    try {/* w w w. j  a v a 2  s . co m*/
        Files.ensureDirectoryExists(fileName);
    } catch (IOException e) {
        log(fileName, e, t);
    }
    fileName = new File(fileName, FILE_NAME_FORMAT.format(new Date())).toString();

    PrintStream ps = null;
    try {
        ps = new PrintStream(new FileOutputStream(fileName));
        ps.println("JaLingo Internal Error Log");
        ps.println("==========================");
        ps.println("Version  : " + JaLingoInfo.VERSION);
        ps.println("Exception: ...");
        t.printStackTrace(ps);
    } catch (IOException e) {
        log(fileName, e, t);
    } finally {
        if (ps != null) {
            ps.close();
        }
    }

    return fileName;
}