Example usage for java.io Writer flush

List of usage examples for java.io Writer flush

Introduction

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

Prototype

public abstract void flush() throws IOException;

Source Link

Document

Flushes the stream.

Usage

From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java

/**
 * Test robustness.//from  w ww  . j a  v a2 s.c  o m
 * 
 * @throws Exception
 *             When the test failed.
 */
@Test
public void testMissingClose() throws Exception {
    final Agent agent = new org.kjkoster.zapcat.zabbix.ZabbixAgent();
    // give the agent some time to open the port
    Thread.sleep(100);
    final Socket socket = new Socket(InetAddress.getLocalHost(),
            org.kjkoster.zapcat.zabbix.ZabbixAgent.DEFAULT_PORT);

    final Writer out = new OutputStreamWriter(socket.getOutputStream());
    out.write("jmx[foo\n");
    out.flush();

    final InputStream in = socket.getInputStream();
    final byte[] buffer = new byte[1024];
    final int read = in.read(buffer);
    assertEquals(29, read);

    assertEquals('Z', buffer[0]);
    assertEquals('B', buffer[1]);
    assertEquals('X', buffer[2]);
    assertEquals('D', buffer[3]);

    assertEquals('N', buffer[17]);
    assertEquals('O', buffer[18]);
    assertEquals('T', buffer[19]);

    // we'll take the rest for granted...

    socket.close();
    agent.stop();
}

From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java

/**
 * Test the we can ping the agent.//  w ww .ja v  a  2 s  .  co m
 * 
 * @throws Exception
 *             When the test failed.
 */
@Test
public void testPing() throws Exception {
    final Agent agent = new org.kjkoster.zapcat.zabbix.ZabbixAgent();
    // give the agent some time to open the port
    Thread.sleep(100);
    final Socket socket = new Socket(InetAddress.getLocalHost(),
            org.kjkoster.zapcat.zabbix.ZabbixAgent.DEFAULT_PORT);

    final Writer out = new OutputStreamWriter(socket.getOutputStream());
    out.write("agent.ping\n");
    out.flush();

    final InputStream in = socket.getInputStream();
    final byte[] buffer = new byte[1024];
    final int read = in.read(buffer);
    assertEquals(14, read);

    assertEquals('Z', buffer[0]);
    assertEquals('B', buffer[1]);
    assertEquals('X', buffer[2]);
    assertEquals('D', buffer[3]);

    assertEquals('1', buffer[13]);

    // we'll take the rest for granted...

    socket.close();
    agent.stop();
}

From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java

/**
 * Test robustness./*from   www.  jav a 2  s  . com*/
 * 
 * @throws Exception
 *             When the test failed.
 */
@Test
public void testMissingOpen() throws Exception {
    final Agent agent = new org.kjkoster.zapcat.zabbix.ZabbixAgent();
    // give the agent some time to open the port
    Thread.sleep(100);
    final Socket socket = new Socket(InetAddress.getLocalHost(),
            org.kjkoster.zapcat.zabbix.ZabbixAgent.DEFAULT_PORT);

    final Writer out = new OutputStreamWriter(socket.getOutputStream());
    out.write("jmx(foo]\n");
    out.flush();

    final InputStream in = socket.getInputStream();
    final byte[] buffer = new byte[1024];
    final int read = in.read(buffer);
    assertEquals(29, read);

    assertEquals('Z', buffer[0]);
    assertEquals('B', buffer[1]);
    assertEquals('X', buffer[2]);
    assertEquals('D', buffer[3]);

    assertEquals('N', buffer[17]);
    assertEquals('O', buffer[18]);
    assertEquals('T', buffer[19]);

    // we'll take the rest for granted...

    socket.close();
    agent.stop();
}

From source file:kr.co.skysoft.framework.taglib.JSR303JSCodebaseTag.java

/**
 * Copies the chars from in to out and then closes in but
 * leaves out open./* w  ww.jav a 2  s .  c  om*/
 *
 * @param in the input reader
 * @param out the output writer
 * @throws IOException if there is an io exception
 */
private void copy(Reader in, Writer out) throws IOException {

    Assert.notNull(in, "No Reader specified");
    Assert.notNull(out, "No Writer specified");
    try {
        char[] buffer = new char[1024];
        int bytesRead;
        while ((bytesRead = in.read(buffer)) != -1) {
            out.write(buffer, 0, bytesRead);
        }
        out.flush();
    } finally {
        try {
            in.close();
        } catch (IOException ex) {
            logger.warn("Could not close Reader", ex);
        }
    }
}

From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java

private void query(int port, String key, byte[] expected) throws IOException {
    final Socket socket = new Socket(InetAddress.getLocalHost(), port);

    final Writer out = new OutputStreamWriter(socket.getOutputStream());
    out.write(key);// w w  w.  j  a  v  a  2  s.  com
    out.write('\n');
    out.flush();

    final InputStream in = socket.getInputStream();
    final byte[] buffer = new byte[1024];
    in.read(buffer);
    socket.close();
    System.out.println(new String(buffer));
    assertEquals('Z', buffer[0]);
    assertEquals('B', buffer[1]);
    assertEquals('X', buffer[2]);
    assertEquals('D', buffer[3]);
    assertEquals((byte) 0x01, buffer[4]);
    byte[] actual = Arrays.copyOfRange(buffer, 13, 13 + expected.length);
    assertArrayEquals(expected, actual);
    //        assertTrue(Arrays.equals(expected, actual));
}

From source file:com.openedit.error.JsonErrorHandler.java

/**
 * @see org.jpublish.ErrorHandler#handleError(JPublishError)
 *//*from   ww  w  . j a  v a  2s .co  m*/
public boolean handleError(Throwable error, WebPageRequest context) {
    if (context.getResponse() != null && context.getResponse().getContentType() != null
            && !context.getResponse().getContentType().contains("json")) {
        return false;
    }

    OpenEditException exception = null;
    if (context != null) {
        try {
            if (!(error instanceof OpenEditException)) {
                exception = new OpenEditException(error); //we need the toStacktrace method
            } else {
                exception = (OpenEditException) error;
            }
            if (!context.hasRedirected() && context.getResponse() != null) {
                try {
                    context.getResponse().setStatus(500);
                } catch (Exception ex) {
                    //ignored
                    log.debug("Ignored:" + ex);
                }
            }
            error.printStackTrace();
            String pathWithError = exception.getPathWithError();
            if (pathWithError == null) {
                pathWithError = context.getPage().getPath();
                exception.setPathWithError(pathWithError);

            }
            context.putPageValue("editPath", exception.getPathWithError());
            context.putPageValue("oe-exception", exception); //must be a top level thing since we create a new context
            PageStreamer pages = (PageStreamer) context.getPageValue(PageRequestKeys.PAGES);

            Writer out = context.getWriter();
            out.append("{ \"reponse\": {\n");
            out.append(" \"status\":\"error\",");
            out.append("{ \"path\":\"" + pathWithError + "\",");
            out.append("{ \"details\":\"" + error + "\"");
            out.append("\n}");
            //error.printStackTrace( new PrintWriter( writer ) );
            out.flush();
        } catch (Exception ex) {
            //Do not throw an error here is it will be infinite
            log.error(ex);
            ex.printStackTrace();
            try {
                context.getWriter().write("Check error logs: " + ex);
                //throw new OpenEditRuntimeException(ex);
            } catch (Throwable ex1) {
                log.error(ex1);
            }
        }
        return true;
    }
    return false;
}

From source file:functionalTests.vfsprovider.TestProActiveProviderAutoclosing.java

@Test
public void testOutputStreamOpenWriteAutocloseFlush() throws Exception {
    final FileObject fo = openFileObject("out.txt");
    final Writer writer = openWriter(fo);
    try {/*from www .j  av a2s  .c o  m*/
        writer.write("ghi");
        Thread.sleep(SLEEP_TIME);
        writer.flush();
        assertContentEquals(fo, "ghi");
    } finally {
        writer.close();
    }
    fo.close();
}

From source file:br.msf.maven.compressor.processor.CssCompressor.java

@Override
protected CharSequence proccessMinify(final CharSequence originalContent, final CompressorSettings settings)
        throws Exception {
    Reader reader = null;/*from ww  w.  j  ava  2 s .com*/
    Writer writer = null;

    try {
        reader = new CharSequenceReader(originalContent);
        com.yahoo.platform.yui.compressor.CssCompressor compressor = new com.yahoo.platform.yui.compressor.CssCompressor(
                reader);

        final StringBuilder out = new StringBuilder(originalContent.length());
        writer = new StringBuilderWriter(out);
        compressor.compress(writer, LINE_BREAK_POS);
        writer.flush();
        return out;
    } finally {
        IOUtils.closeQuietly(reader);
        IOUtils.closeQuietly(writer);
    }
}

From source file:org.kjkoster.zapcat.test.ZabbixAgentProtocolTest.java

/**
 * Test that by default we have protocol version 1.4.
 * /*from  www. j av a  2  s  .c o m*/
 * @throws Exception
 *             When the test failed.
 */
@Test
public void testDefault() throws Exception {
    final Agent agent = new ZabbixAgent();
    // give the agent some time to open the port
    Thread.sleep(100);
    final Socket socket = new Socket(InetAddress.getLocalHost(), ZabbixAgent.DEFAULT_PORT);

    final Writer out = new OutputStreamWriter(socket.getOutputStream());
    out.write("system.property[java.version]\n");
    out.flush();

    final InputStream in = socket.getInputStream();
    final byte[] buffer = new byte[1024];
    in.read(buffer);

    assertEquals('Z', buffer[0]);
    assertEquals('B', buffer[1]);
    assertEquals('X', buffer[2]);
    assertEquals('D', buffer[3]);
    // we'll take the rest for granted...

    socket.close();
    agent.stop();
}

From source file:com.intel.cosbench.exporter.ScriptsLogExporter.java

private void exportStageLog(Writer writer, StageInfo stage) throws IOException {
    writer.write("=========================");
    writer.write("=========================");
    writer.write(" stage: " + stage.getId() + ' ');
    writer.write("=========================");
    writer.write("=========================");
    writer.write('\n');
    String wsId = workload.getId() + stage.getId();
    doExportLog(writer, wsId);/*  w w  w. j  av a2  s. co m*/
    writer.flush();
}