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:at.ac.tuwien.big.moea.print.PopulationWriter.java

@Override
public String write(final Iterable<S> population) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final PrintStream ps = new PrintStream(baos);
    write(ps, population);//from  w ww  . j av a  2s .  c  om
    ps.close();
    try {
        return baos.toString("UTF-8");
    } catch (final UnsupportedEncodingException e) {
        return e.getMessage();
    }
}

From source file:at.ac.tuwien.big.moea.print.PopulationWriter.java

@Override
public String write(final Population population) {
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final PrintStream ps = new PrintStream(baos);
    write(ps, population);//from  ww w  .  ja  v a  2 s.co  m
    ps.close();
    try {
        return baos.toString("UTF-8");
    } catch (final UnsupportedEncodingException e) {
        return e.getMessage();
    }
}

From source file:MultiThreadServer.java

public void run() {
    try {//from  w  w  w .ja v  a2  s .  com
        PrintStream pstream = new PrintStream(csocket.getOutputStream());
        for (int i = 100; i >= 0; i--) {
            pstream.println(i + " bottles of beer on the wall");
        }
        pstream.close();
        csocket.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:org.apache.hadoop.hdfs.server.namenode.RenewDelegationTokenServlet.java

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp)
        throws ServletException, IOException {
    final UserGroupInformation ugi;
    final ServletContext context = getServletContext();
    final Configuration conf = (Configuration) context.getAttribute(JspHelper.CURRENT_CONF);
    try {/* ww w .j  a  v  a 2 s .  c o  m*/
        ugi = getUGI(req, conf);
    } catch (IOException ioe) {
        LOG.info("Request for token received with no authentication from " + req.getRemoteAddr(), ioe);
        resp.sendError(HttpServletResponse.SC_FORBIDDEN, "Unable to identify or authenticate user");
        return;
    }
    final NameNode nn = (NameNode) context.getAttribute("name.node");
    String tokenString = req.getParameter(TOKEN);
    if (tokenString == null) {
        resp.sendError(HttpServletResponse.SC_MULTIPLE_CHOICES, "Token to renew not specified");
    }
    final Token<DelegationTokenIdentifier> token = new Token<DelegationTokenIdentifier>();
    token.decodeFromUrlString(tokenString);

    try {
        long result = ugi.doAs(new PrivilegedExceptionAction<Long>() {
            public Long run() throws Exception {
                return nn.renewDelegationToken(token);
            }
        });
        PrintStream os = new PrintStream(resp.getOutputStream());
        os.println(result);
        os.close();
    } catch (Exception e) {
        // transfer exception over the http
        String exceptionClass = e.getClass().getCanonicalName();
        String exceptionMsg = e.getLocalizedMessage();
        String strException = exceptionClass + ";" + exceptionMsg;
        LOG.info("Exception while renewing token. Re-throwing. s=" + strException, e);

        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, strException);
    }
}

From source file:edu.wfu.inotado.WakeServMockServer.java

public void handle(Request request, Response response) {
    try {// www . j a v  a 2 s  . co  m
        String signature = request.getValue("Signature");
        if (StringUtils.isNotBlank(signature)) {
            log.info("Signature received on server: " + signature);
        }
        PrintStream body = response.getPrintStream();
        response.setValue("Content-Type", "application/xml");
        body.println(this.bodyText);
        body.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.clematis.jsmodify.JSExecutionTracer.java

/**
 * This method parses the JSON file containing the trace objects and extracts the objects
 *///w w w . j  ava2s.c  om
public static void extraxtTraceObjects() {
    try {
        ObjectMapper mapper = new ObjectMapper();
        // Register the module that serializes the Guava Multimap
        mapper.registerModule(new GuavaModule());

        Multimap<String, TraceObject> traceMap = mapper.<Multimap<String, TraceObject>>readValue(
                new File("clematis-output/ftrace/function.trace"),
                new TypeReference<TreeMultimap<String, TraceObject>>() {
                });

        Collection<TraceObject> timingTraces = traceMap.get("TimingTrace");
        Collection<TraceObject> domEventTraces = traceMap.get("DOMEventTrace");
        Collection<TraceObject> XHRTraces = traceMap.get("XHRTrace");
        Collection<TraceObject> functionTraces = traceMap.get("FunctionTrace");

        Iterator<TraceObject> it3 = domEventTraces.iterator();
        TraceObject next2;
        ArrayList<TraceObject> removeus = new ArrayList<TraceObject>();
        /**    while (it3.hasNext()) {
        next2 = it3.next();
                
        if (next2 instanceof DOMEventTrace
                && (((DOMEventTrace) next2).getEventType().equals("mouseover") 
                        || (((DOMEventTrace) next2).getEventType().equals("mousemove"))
                        || (((DOMEventTrace) next2).getEventType().equals("mouseout"))
                        || (((DOMEventTrace) next2).getEventType().equals("mousedown"))
                        || (((DOMEventTrace) next2).getEventType().equals("mouseup")))) {
            removeus.add(next2);
                
        }
            }*/
        domEventTraces.removeAll(removeus);

        story = new Story(domEventTraces, functionTraces, timingTraces, XHRTraces);
        story.setOrderedTraceList(sortTraceObjects());

        System.out.println(timingTraces.size());
        Iterator<TraceObject> it = timingTraces.iterator();
        TraceObject next;

        while (it.hasNext()) {
            next = it.next();
            System.out.println("=======");
            System.out.println(next.getCounter());
        }

        /*
         * ArrayList<TraceObject> bookmarkTraceObjects = new ArrayList<TraceObject>(); for
         * (TraceObject to : story.getOrderedTraceList()) { if (to instanceof DOMEventTrace) {
         * if (((DOMEventTrace)to).getEventType().equals("_BOOKMARK_")) {
         * bookmarkTraceObjects.add(to);
         * System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"); } } }
         */

        story.setEpisodes(buildEpisodes());

        ArrayList<Episode> ss = story.getEpisodes();
        Iterator<Episode> it2 = ss.iterator();
        System.out.println("hhhmmm");

        while (it2.hasNext()) {
            System.out.println("--------");
            System.out.println(it2.next().getSource().getClass());
        }

        System.out.println("# of trace objects: " + story.getOrderedTraceList().size());
        System.out.println("# of episodes: " + story.getEpisodes().size());
        /*
         * for (int i = 0; i < story.getEpisodes().size(); i ++) { Episode episode =
         * story.getEpisodes().get(i); if (episode.getSource() instanceof DOMEventTrace) {
         * DOMEventTrace source = (DOMEventTrace)episode.getSource();
         * if(source.getTargetElement().contains("bookmarkButton")) {
         * System.out.println("***********"); if (i + 1 < story.getEpisodes().size()) {
         * story.getEpisodes().get(i).getSource().setIsBookmarked(true); // move isbookmarked to
         * episode System.out.println("* " + story.getEpisodes().get(i).getSource().toString());
         * } } } }
         */
        /*
         * for (int i = 0; i < story.getEpisodes().size(); i ++) { Episode episode =
         * story.getEpisodes().get(i); ArrayList<TraceObject> bookmarkObjects = new
         * ArrayList<TraceObject>(); for (int j = 0; j < episode.getTrace().getTrace().size(); j
         * ++) { if (episode.getTrace().getTrace().get(j) instanceof DOMEventTrace) {
         * DOMEventTrace domEventTrace = (DOMEventTrace)episode.getTrace().getTrace().get(j); if
         * (domEventTrace.getEventType().equals("_BOOKMARK_")) {
         * bookmarkObjects.add(domEventTrace); System.out.println("bookmark"); if (i + 1 <
         * story.getEpisodes().size()) { story.getEpisodes().get(i +
         * 1).getSource().setIsBookmarked(true); } } } }
         * episode.getTrace().getTrace().removeAll(bookmarkObjects); } for (Episode e :
         * story.getEpisodes()) { boolean bookmarkNextEpisode = false; // if
         * (e.getSource().getIsBookmarked()) System.out.println("============ " +
         * e.getSource().getIsBookmarked()); for (TraceObject to : e.getTrace().getTrace()) { if
         * (to instanceof DOMEventTrace) { if
         * (((DOMEventTrace)to).getEventType().equals("_BOOKMARK_"))
         * System.out.println("bookmark"); } } }
         */
        /*
         * for (Episode episode : story.getEpisodes()) { if (episode.getSource() instanceof
         * DOMEventTrace) { if
         * (((DOMEventTrace)episode.getSource()).getTargetElement().contains("bookmarkButton"))
         * { System.out.print("**** " + ((DOMEventTrace)episode.getSource()).getEventType() +
         * " * "); } System.out.println("---- " +
         * ((DOMEventTrace)episode.getSource()).getTargetElement()); } }
         */// TODO TODO TODO project specific for photo gallery. eliminate unwanted episodes
        story.removeUselessEpisodes();

        ss = story.getEpisodes();
        it2 = ss.iterator();
        System.out.println("hhhmmm2");

        while (it2.hasNext()) {
            System.out.println("--------");
            System.out.println(it2.next().getSource().getClass());
        }

        ArrayList<Episode> bookmarkEpisodes = new ArrayList<Episode>();

        for (int i = 0; i < story.getEpisodes().size(); i++) {
            Episode episode = story.getEpisodes().get(i);
            if (episode.getSource() instanceof DOMEventTrace) {
                DOMEventTrace source = (DOMEventTrace) episode.getSource();
                if (source.getTargetElement().contains("bookmarkButton")) {
                    bookmarkEpisodes.add(episode);
                    if (i + 1 < story.getEpisodes().size()) {
                        story.getEpisodes().get(i + 1).setIsBookmarked(true);
                        // story.getEpisodes().get(i).getSource().setIsBookmarked(true); // move
                        // isbookmarked to episode
                        System.out.println("* episode # " + (i + 1) + " bookmarked");
                    }
                }
            }

        }

        story.removeUselessEpisodes(bookmarkEpisodes);

        //    story.removeToolbarEpisodes();

        System.out.println("# of episodes after trimming: " + story.getEpisodes().size());

        DateFormat dateFormat = new SimpleDateFormat("EEE,d,MMM,HH-mm");
        Date date = new Date();
        System.out.println(dateFormat.format(date));
        // dateFormat.format(date).toString()
        theTime = new String(dateFormat.format(date).toString());
        System.out.println(theTime);

        // JavaScript episodes for JSUML2
        Helper.directoryCheck(outputFolder + "/sequence_diagrams/");
        PrintStream JSepisodes = new PrintStream(outputFolder + "/sequence_diagrams/allEpisodes.js");

        for (Episode e : story.getEpisodes()) {
            // Create pic files for each episode's sequence diagram
            designSequenceDiagram(e, JSepisodes);
        }

        // Once all episodes have been saved to JS file, close
        JSepisodes.close();

        // Create graph containing all episodes with embedded sequence diagrams
        EpisodeGraph eg = new EpisodeGraph(getOutputFolder(), story.getEpisodes());
        eg.createGraph();
        writeStoryToDisk();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.diversityarrays.dalclient.DalUtil.java

public static void showXmlResult(String xml, OutputStream out) {
    if (looksLikeDoctype(xml)) {
        // just print it!
        PrintStream ps = new PrintStream(out);
        ps.print(xml);//from  ww  w .  j  a va 2s .  c o m
        ps.close();
    } else {
        try {
            showXmlResult(xml, new OutputStreamWriter(out, ENCODING_UTF_8));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException("Should never happen!", e); //$NON-NLS-1$
        }
    }
}

From source file:org.apache.beam.sdk.io.TextIOReadTest.java

License:asdf

/**
 * Create a zip file with the given lines.
 *
 * @param expected A list of expected lines, populated in the zip file.
 * @param folder A temporary folder used to create files.
 * @param filename Optionally zip file name (can be null).
 * @param fieldsEntries Fields to write in zip entries.
 * @return The zip filename.//from   ww  w .  j a  v  a2  s . c  om
 * @throws Exception In case of a failure during zip file creation.
 */
private static File createZipFile(List<String> expected, TemporaryFolder folder, String filename,
        String[]... fieldsEntries) throws Exception {
    File tmpFile = folder.getRoot().toPath().resolve(filename).toFile();

    ZipOutputStream out = new ZipOutputStream(new FileOutputStream(tmpFile));
    PrintStream writer = new PrintStream(out, true /* auto-flush on write */);

    int index = 0;
    for (String[] entry : fieldsEntries) {
        out.putNextEntry(new ZipEntry(Integer.toString(index)));
        for (String field : entry) {
            writer.println(field);
            expected.add(field);
        }
        out.closeEntry();
        index++;
    }

    writer.close();
    out.close();

    return tmpFile;
}

From source file:de.metanome.backend.result_receiver.ResultPrinter.java

@Override
public void close() throws IOException {
    for (PrintStream stream : openStreams.values()) {
        stream.close();
    }/*  w w w .jav a  2 s .co m*/
}

From source file:PatchyMIDlet.java

public void run() {
    try {//from  w  w w.  j a  va  2 s  .c  o m
        mServerSocketConnection = (ServerSocketConnection) Connector.open("socket://:80");
        SocketConnection sc = null;
        while (mTrucking) {
            sc = (SocketConnection) mServerSocketConnection.acceptAndOpen();
            Reader in = new InputStreamReader(sc.openInputStream());

            PrintStream out = new PrintStream(sc.openOutputStream());
            out.print("HTTP/1.1 200 OK\r\n\r\n");
            out.print("Message");
            out.close();
            in.close();
            sc.close();
        }
    } catch (Exception e) {

    }
}