Example usage for java.io PrintStream flush

List of usage examples for java.io PrintStream flush

Introduction

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

Prototype

public void flush() 

Source Link

Document

Flushes the stream.

Usage

From source file:org.cloudcoder.app.server.servlet.RegisterStudents.java

public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Connection conn = null;/*  ww w  .j a v a2s  .  c  om*/
    try {
        conn = DBUtil.getConnection();

        //Create a temporary file in a platform-independent manner
        File tempFile = File.createTempFile("registerstudentsupload", ".tmp");
        tempFile.delete();
        tempFile.mkdirs();
        tempFile.deleteOnExit();

        DiskFileItemFactory factory = new DiskFileItemFactory(10 * 1024 * 1024, tempFile);
        ServletFileUpload upload = new ServletFileUpload(factory);

        int courseId = -1;
        InputStream in = null;
        int num = 0;

        List<FileItem> items = upload.parseRequest(request);
        logger.info("num file items: " + items.size());
        for (FileItem item : items) {
            if (item.isFormField() && item.getFieldName().equals("courseId")) {
                courseId = Integer.parseInt(item.getString());
            } else {
                in = item.getInputStream();
            }
        }
        if (courseId == -1 || in == null) {
            logger.error("courseId " + courseId + ", file input stream is null? " + (in == null));
            throw new ServletException("courseId " + courseId);
        }
        num = ConfigurationUtil.registerStudentsForCourseId(in, courseId, conn);

        response.setStatus(HttpServletResponse.SC_OK);
        response.setContentType("text/plain");
        PrintStream out = new PrintStream(response.getOutputStream());
        out.println("Registered " + num + " students");
        out.flush();
        out.close();
    } catch (SQLException e) {
        throw new ServletException(e);
    } catch (FileUploadException e) {
        throw new ServletException(e);
    } finally {
        DBUtil.closeQuietly(conn);
    }
}

From source file:org.apache.karaf.shell.tabletest.ShellTableTest.java

@Test
public void testTable() {
    ShellTable table = new ShellTable();
    table.column(new Col("id").alignRight().maxSize(5));
    table.column(new Col("Name").maxSize(20));
    table.column(new Col("Centered").alignCenter());

    table.addRow().addContent(1, "Test", "Description");
    table.addRow().addContent(20, "My name", "Description");

    Row row = table.addRow();//from   www .j a va2  s. c o m
    row.addContent(123456789);
    row.addContent("A very long text that should be cut");
    row.addContent("A very long text that should not be cut");

    StringWriter writer = new StringWriter();
    PrintStream out = new PrintStream(new WriterOutputStream(writer));
    table.print(out, true);
    out.flush();
    String expected = "   id | Name                 |                Centered                \n"
            + "----------------------------------------------------------------------\n"
            + "    1 | Test                 |               Description              \n"
            + "   20 | My name              |               Description              \n"
            + "12345 | A very long text tha | A very long text that should not be cut\n";
    Assert.assertEquals(expected, getString(writer));
}

From source file:eu.europa.ec.fisheries.uvms.plugins.inmarsat.twostage.Connect.java

private void write(String value, PrintStream out) throws IOException {
    out.println(value);/*  w w  w  .ja v  a2  s  . com*/
    out.flush();
    LOG.debug("write:" + value);
}

From source file:ren.hankai.cordwood.core.test.http.DoNothingHandler.java

@Override
public void handle(Request request, Response response) throws Exception {
    response.setValue("Server", "TinyServer/1.0");
    response.setDate("Date", new Date().getTime());
    response.setDate("Last-Modified", new Date().getTime());

    final PrintStream output = response.getPrintStream();
    final ByteArrayInputStream input = new ByteArrayInputStream(this.rawResponse.getBytes("UTF-8"));
    IOUtils.copy(input, output);/*from   w  w w .  java  2 s. com*/
    output.flush();
    response.close();
}

From source file:hudson.cli.GetJobCommandTest.java

@Issue("JENKINS-20236")
@Test//w  w  w .j  a  v  a  2  s.  c o  m
public void withFolders() throws Exception {
    MockFolder d = j.createFolder("d");
    FreeStyleProject p = d.createProject(FreeStyleProject.class, "p");
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    PrintStream outS = new PrintStream(out);
    int result = new GetJobCommand().main(Collections.singletonList("d/p"), Locale.ENGLISH,
            new NullInputStream(0), outS, outS);
    outS.flush();
    String output = out.toString();
    assertEquals(output, 0, result);
    assertEquals(p.getConfigFile().asString(), output);
    out = new ByteArrayOutputStream();
    outS = new PrintStream(out);
    result = new GetJobCommand().main(Collections.singletonList("d"), Locale.ENGLISH, new NullInputStream(0),
            outS, outS);
    outS.flush();
    output = out.toString();
    assertEquals(output, 0, result);
    assertEquals(d.getConfigFile().asString(), output);
}

From source file:li.klass.fhem.fhem.TelnetConnection.java

private void writeCommand(PrintStream printStream, String command) {
    printStream.println(command);
    printStream.flush();
}

From source file:com.adaptris.http.HttpResponse.java

/** Write the request line to the supplied outputstream.
 *  @param out the outputstream//from   w w  w.j av a 2  s . c  o  m
 *  @throws HttpException on error.
 *  @see DataTransfer#writeTo(OutputStream)
 */
public void writeTo(OutputStream out) throws HttpException {
    try {
        logR.trace("Writing Response:- " + toString());

        PrintStream p = new PrintStream(out);
        p.print(toString());
        p.print(Http.CRLF);
        p.flush();
    } catch (Exception e) {
        throw new HttpException(e);
    }
}

From source file:com.adaptris.http.HttpHeaders.java

/** Write this set of headers to the supplied outputstream.
 *  @see DataTransfer#writeTo(OutputStream)
 *//*  w w  w .jav a2  s.  c o m*/
public void writeTo(OutputStream out) throws HttpException {
    try {

        PrintStream p = new PrintStream(out);
        p.print(toString());
        p.flush();
    } catch (Exception e) {
        throw new HttpException(e);
    }
}

From source file:org.paxle.tools.logging.impl.gui.LogView.java

/**
 * Function to convert a {@link Throwable} into a string
 *//*from   w w  w. ja  v a  2 s .  c  o  m*/
public String toString(Throwable e) {
    try {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        PrintStream errorOut = new PrintStream(bout, false, "UTF-8");
        e.printStackTrace(errorOut);

        errorOut.flush();
        errorOut.close();
        return bout.toString("UTF-8");
    } catch (Exception ex) {
        // should not occur
        this.logger.error(e);
        return null;
    }
}

From source file:eu.europa.ec.fisheries.uvms.plugins.inmarsat.twostage.Connect.java

private void sendPsw(PrintStream output, String psw) throws IOException {
    output.print(psw + "\r\n");
    output.flush();
    LOG.info("Sent psw");
}