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:com.xqdev.jam.MLJAM.java

private static void sendServerProblemResponse(HttpServletResponse res, String s) throws IOException {
    // Commenting out the status code because we want the client to eval the error() call
    //res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    res.setContentType("x-marklogic/xquery; charset=UTF-8");
    if (s != null && s.length() > 4096) { // Cap super long errors
        s = s.substring(0, 2048) + " ...[trimmed]... " + s.substring(s.length() - 2048);
    }/*from  w  w w  .j  ava  2  s.  c o m*/
    Writer writer = res.getWriter();
    writer.write("error('" + escapeSingleQuotes(s) + "')");
    writer.flush();
}

From source file:com.serotonin.bacnet4j.util.sero.StreamUtils.java

public static void transfer(Reader reader, Writer writer, long limit) throws IOException {
    char[] buf = new char[1024];
    int readcount;
    long total = 0;
    while ((readcount = reader.read(buf)) != -1) {
        if (limit != -1) {
            if (total + readcount > limit)
                readcount = (int) (limit - total);
        }/*from   w w w.ja  va 2s  .  c  o  m*/

        if (readcount > 0)
            writer.write(buf, 0, readcount);

        total += readcount;
        if (limit != -1 && total >= limit)
            break;
    }
    writer.flush();
}

From source file:net.cloudkit.enterprises.infrastructure.utilities.FreemarkerHelper.java

public static String process(String templateName, Map<String, ?> model, String target) {
    String result = null;/*w ww .  j  av  a 2  s . c om*/

    FileOutputStream fileOutputStream = null;
    OutputStreamWriter outputStreamWriter = null;
    Writer writer = null;
    File file = new File(target);
    if (!file.exists()) {
        if (file.isDirectory()) {
            file.mkdirs();
        } else {
            file.mkdir();
        }
    }
    try {
        fileOutputStream = new FileOutputStream(file);
        outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
        // new OutputStreamWriter(new FileOutputStream(file), "UTF-8")
        writer = new BufferedWriter(outputStreamWriter);
        result = process(templateName, model, writer);
        writer.flush();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {

        IOUtils.closeQuietly(writer);
        IOUtils.closeQuietly(outputStreamWriter);
        IOUtils.closeQuietly(fileOutputStream);

        // try {
        //     writer.close();
        // } catch (IOException e) {
        //     e.printStackTrace();
        // }
    }
    return result;
}

From source file:net.sf.jasperreports.engine.fonts.SimpleFontExtensionHelper.java

public static void writeFontExtensionsXml(OutputStream outputStream, FontExtensionsContainer extensions)
        throws JRException {
    Writer out = null;
    try {// ww w. j a v a2 s  .  c  om
        out = new OutputStreamWriter(outputStream, DEFAULT_ENCODING);
        writeFontExtensions(out, extensions);
        out.flush();
    } catch (Exception e) {
        throw new JRException(EXCEPTION_MESSAGE_KEY_OUTPUT_STREAM_WRITER_ERROR, null, e);
    }
}

From source file:com.nary.io.FileUtils.java

public static void writeFile(File outFile, String content) throws IOException {
    Writer outWriter = null;
    try {//from w  ww. ja v a2 s .  com
        outWriter = cfEngine.thisPlatform.getFileIO().getFileWriter(outFile);
        outWriter.write(content);
        outWriter.flush();
    } finally {
        try {
            if (outWriter != null)
                outWriter.close();
        } catch (Exception ignoreCloseException) {
        }
    }
}

From source file:Main.java

/**
 * Write the entire contents of the supplied string to the given writer. This method always flushes and closes the writer when
 * finished.//from  www  . j  av a  2 s .  co m
 * 
 * @param input the content to write to the writer; may be null
 * @param writer the writer to which the content is to be written
 * @throws IOException
 * @throws IllegalArgumentException if the writer is null
 */
public static void write(Reader input, Writer writer) throws IOException {
    boolean error = false;
    try {
        if (input != null) {
            char[] buffer = new char[1024];
            try {
                int numRead = 0;
                while ((numRead = input.read(buffer)) > -1) {
                    writer.write(buffer, 0, numRead);
                }
            } finally {
                input.close();
            }
        }
    } catch (IOException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing writer
        throw e;
    } catch (RuntimeException e) {
        error = true; // this error should be thrown, even if there is an error flushing/closing writer
        throw e;
    } finally {
        try {
            writer.flush();
        } catch (IOException e) {
            if (!error)
                throw e;
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                if (!error)
                    throw e;
            }
        }
    }
}

From source file:edu.stanford.epad.common.util.EPADFileUtils.java

/**
 * True if the write succeed otherwise it is false.
 * /*from   w  w w.  j  a  v  a 2s. c om*/
 * @param file - File including full directory path to write.
 * @param contents - String complete contents of file.
 * @return boolean
 */
public static boolean write(File file, String contents) {
    try {
        Writer out = new BufferedWriter(new FileWriter(file));
        out.write(contents);
        out.flush();
        out.close();
        return true;
    } catch (Exception e) {
        log.warning("Failed to write file: " + file.getAbsolutePath(), e);
        return false;
    }
}

From source file:com.mail163.email.mail.transport.Rfc822Output.java

/**
 * Write text (either as main body or inside a multipart), preceded by appropriate headers.
 *
 * Note this always uses base64, even when not required.  Slightly less efficient for
 * US-ASCII text, but handles all formats even when non-ascii chars are involved.  A small
 * optimization might be to prescan the string for safety and send raw if possible.
 *
 * @param writer the output writer//from   w ww  .  jav a2s  .  c o m
 * @param out the output stream inside the writer (used for byte[] access)
 * @param text The original text of the message
 */
private static void writeTextWithHeaders(Writer writer, OutputStream out, String text) throws IOException {
    Logs.v(Logs.LOG_MessageView, "isContentType >>>>>>>>> :" + isContentType);

    //=============2012.3.4===========
    if (isContentType) {//Content-Type
        writeHeader(writer, "Content-Type", "text/html; charset=utf-8");
    } else {
        writeHeader(writer, "Content-Type", "text/plain; charset=utf-8");
    }

    //=============2012.3.4===========
    writeHeader(writer, "Content-Transfer-Encoding", "base64");
    writer.write("\r\n");
    byte[] bytes = text.getBytes("UTF-8");
    writer.flush();
    out.write(Base64.encode(bytes, Base64.CRLF));
}

From source file:com.bristle.javalib.io.FileUtil.java

/**************************************************************************
* Copy the contents of the specified binary file to the specified Writer.
*@param  strFilename       Name of the file to copy.
*@param  writer            Writer to write to.
*@throws FileNotFoundException/*from   w  w  w  .j  a v  a 2 s.c  o m*/
*                           When binary file not found.
*@throws IOException        When an I/O error occurs reading the file or 
*                           writing it to the Writer.
**************************************************************************/
public static void copyBinaryFileToWriter(String strFilename, Writer writer)
        throws FileNotFoundException, IOException {
    RandomAccessFile file = new RandomAccessFile(strFilename, "r");
    try {
        int i;
        while (-1 != (i = file.read())) {
            writer.write((char) i);
        }
        writer.flush();
    } finally {
        file.close();
    }
}

From source file:Log.java

/**
 * Writes all currently logged messages to this stream if there was no
 * stream set previously, and sets the stream to write future log
 * messages to.//from   www .  j av a 2  s  .  co  m
 * @param stream The writer
 * @since jEdit 3.2pre4
 */
public static void setLogWriter(Writer stream) {
    if (Log.stream == null && stream != null) {
        try {
            if (wrap) {
                for (int i = logLineCount; i < log.length; i++) {
                    stream.write(log[i]);
                    stream.write(lineSep);
                }
            }
            for (int i = 0; i < logLineCount; i++) {
                stream.write(log[i]);
                stream.write(lineSep);
            }

            stream.flush();
        } catch (Exception e) {
            // do nothing, who cares
        }
    }

    Log.stream = stream;
}