Example usage for java.io Writer write

List of usage examples for java.io Writer write

Introduction

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

Prototype

public void write(String str) throws IOException 

Source Link

Document

Writes a string.

Usage

From source file:com.netxforge.oss2.core.xml.CastorUtils.java

/**
 * Marshal a Castor XML configuration file.
 *
 * @param obj the object representing the objected to be marshalled to XML
 * @throws org.springframework.dao.DataAccessException if the underlying Castor
 *      Marshaller.marshal() call throws a MarshalException or
 *      ValidationException.  The underlying exception will be translated
 *      using MarshallingExceptionTranslator.
 * @param resource a {@link org.springframework.core.io.Resource} object.
 *//*w  w w. j  av  a 2s.  c  o m*/
public static void marshalWithTranslatedExceptionsViaString(Object obj, Resource resource)
        throws DataAccessException {
    Writer fileWriter = null;
    try {
        StringWriter writer = new StringWriter();
        marshal(obj, writer);

        fileWriter = new OutputStreamWriter(new FileOutputStream(resource.getFile()), "UTF-8");
        fileWriter.write(writer.toString());
        fileWriter.flush();

    } catch (IOException e) {
        throw CASTOR_EXCEPTION_TRANSLATOR.translate("marshalling XML file", e);
    } catch (MarshalException e) {
        throw CASTOR_EXCEPTION_TRANSLATOR.translate("marshalling XML file", e);
    } catch (ValidationException e) {
        throw CASTOR_EXCEPTION_TRANSLATOR.translate("marshalling XML file", e);
    } finally {
        IOUtils.closeQuietly(fileWriter);
    }
}

From source file:com.netspective.sparx.util.HttpUtils.java

public static void renderDevelopmentEnvironmentHeader(Writer writer, NavigationContext nc) throws IOException {
    if (!nc.getRuntimeEnvironmentFlags()
            .flagIsSet(RuntimeEnvironmentFlags.DEVELOPMENT | RuntimeEnvironmentFlags.FRAMEWORK_DEVELOPMENT))
        return;/*from   ww w. j a va  2  s .c  om*/

    final ProjectComponent projectComponent = nc.getProjectComponent();
    if (projectComponent.hasErrors()) {
        int errorsCount = projectComponent.getErrors().size() + projectComponent.getWarnings().size();

        writer.write(
                "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"3\" bgcolor=darkred><tr>\n");
        writer.write(
                "  <td nowrap><font size=2 color=white style='font-family: tahoma,arial; font-size: 8pt'><b>You have <font color=yellow>"
                        + errorsCount + "</font> Netspective Frameworks Project Errors/Warnings.");
        writer.write("             Visit the <a href='" + nc.getRootUrl()
                + "/console/project/input-source#errors' style='color: yellow'>Console</a> to see the messages</b></font></td>\n");
        writer.write("</tr></table>");
    }

    final NavigationPage.Flags flags = (NavigationPage.Flags) nc.getActiveState().getFlags();
    if (flags.isDebuggingRequest())
        develEnvironmentHeader.process(writer, nc, null);
}

From source file:com.twitter.elephanttwin.util.HdfsUtils.java

/**
 * Write \n separated lines of text to HDFS as UTF-8.
 *//*from   w  w  w.  j a  v  a2 s. c o  m*/
public static void writeLines(FileSystem fs, Path path, Iterable<String> lines) throws IOException {
    Preconditions.checkNotNull(fs);
    Preconditions.checkNotNull(path);
    Preconditions.checkNotNull(lines);
    Writer stream = new BufferedWriter(new OutputStreamWriter(fs.create(path), "UTF-8"));
    try {
        for (String line : lines) {
            stream.write(line + "\n");
        }
    } finally {
        stream.close();
    }
}

From source file:Main.java

/**
 * <p>Worker method for the {@link #escapeJavaScript(String)} method.</p>
 *
 * @param out                write to receieve the escaped string
 * @param str                String to escape values in, may be null
 * @param escapeSingleQuote  escapes single quotes if <code>true</code>
 * @param escapeForwardSlash escapes forward slashes if <code>true</code>
 * @throws IOException if an IOException occurs
 *//*from   w w w  .  ja  v  a  2 s  .  co m*/
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
        boolean escapeForwardSlash) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (str == null) {
        return;
    }
    int sz;
    sz = str.length();
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);

        // handle unicode
        if (ch > 0xfff) {
            out.write("\\u" + hex(ch));
        } else if (ch > 0xff) {
            out.write("\\u0" + hex(ch));
        } else if (ch > 0x7f) {
            out.write("\\u00" + hex(ch));
        } else if (ch < 32) {
            switch (ch) {
            case '\b':
                out.write('\\');
                out.write('b');
                break;
            case '\n':
                out.write('\\');
                out.write('n');
                break;
            case '\t':
                out.write('\\');
                out.write('t');
                break;
            case '\f':
                out.write('\\');
                out.write('f');
                break;
            case '\r':
                out.write('\\');
                out.write('r');
                break;
            default:
                if (ch > 0xf) {
                    out.write("\\u00" + hex(ch));
                } else {
                    out.write("\\u000" + hex(ch));
                }
                break;
            }
        } else {
            switch (ch) {
            case '\'':
                if (escapeSingleQuote) {
                    out.write('\\');
                }
                out.write('\'');
                break;
            case '"':
                out.write('\\');
                out.write('"');
                break;
            case '\\':
                out.write('\\');
                out.write('\\');
                break;
            case '/':
                if (escapeForwardSlash) {
                    out.write('\\');
                }
                out.write('/');
                break;
            default:
                out.write(ch);
                break;
            }
        }
    }
}

From source file:com.google.gwtjsonrpc.server.JsonServlet.java

private static void textError(final ActiveCall call, final int status, final String message)
        throws IOException {
    final HttpServletResponse r = call.httpResponse;
    r.setStatus(status);// ww w  . j a v  a  2 s  .c om
    r.setContentType("text/plain; charset=" + ENC);

    final Writer w = new OutputStreamWriter(r.getOutputStream(), ENC);
    try {
        w.write(message);
    } finally {
        w.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.//  ww  w.  j  a v  a2 s . c om
 * @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;
}

From source file:brut.androlib.meta.YamlStringEscapeUtils.java

/**
 * @param out write to receieve the escaped string
 * @param str String to escape values in, may be null
 * @param escapeSingleQuote escapes single quotes if <code>true</code>
 * @param escapeForwardSlash TODO/*from   ww w  . java  2 s.  c  o  m*/
 * @throws IOException if an IOException occurs
 */
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote,
        boolean escapeForwardSlash) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (str == null) {
        return;
    }
    int sz;
    sz = str.length();
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        // "[^\t\n\r\u0020-\u007E\u0085\u00A0-\uD7FF\uE000-\uFFFD]"
        // handle unicode
        if (ch > 0xFFFD) {
            out.write("\\u" + CharSequenceTranslator.hex(ch));
        } else if (ch > 0xD7FF && ch < 0xE000) {
            out.write("\\u" + CharSequenceTranslator.hex(ch));
        } else if (ch > 0x7E && ch != 0x85 && ch < 0xA0) {
            out.write("\\u00" + CharSequenceTranslator.hex(ch));
        } else if (ch < 32) {
            switch (ch) {
            case '\t':
                out.write('\\');
                out.write('t');
                break;
            case '\n':
                out.write('\\');
                out.write('n');
                break;
            case '\r':
                out.write('\\');
                out.write('r');
                break;
            default:
                if (ch > 0xf) {
                    out.write("\\u00" + CharSequenceTranslator.hex(ch));
                } else {
                    out.write("\\u000" + CharSequenceTranslator.hex(ch));
                }
                break;
            }
        } else {
            switch (ch) {
            case '\'':
                if (escapeSingleQuote) {
                    out.write('\\');
                }
                out.write('\'');
                break;
            case '"':
                out.write('\\');
                out.write('"');
                break;
            case '\\':
                out.write('\\');
                out.write('\\');
                break;
            case '/':
                if (escapeForwardSlash) {
                    out.write('\\');
                }
                out.write('/');
                break;
            default:
                out.write(ch);
                break;
            }
        }
    }
}

From source file:com.redhat.rhn.frontend.taglibs.list.ListTagUtil.java

/**
 * Writes arbitrary text to the client (browser)
 * @param ctx caller's page context// ww  w  .  j a  va2 s .  c om
 * @param text text to write
 * @throws JspException if an error occurs
 */
public static void write(PageContext ctx, String text) throws JspException {
    if (text == null) {
        text = "null";
    }
    Writer writer = ctx.getOut();
    try {
        writer.write(text);
    } catch (IOException e) {
        throw new JspException(e);
    }
}

From source file:io.hops.hopsworks.common.jobs.yarn.LogReader.java

/**
 * Writes all logs for a single container to the provided writer.
 *
 * @param valueStream/*from www. j a  v a2s . c om*/
 * @param writer
 * @throws IOException
 */
public static void readAcontainerLogs(DataInputStream valueStream, Writer writer) throws IOException {
    int bufferSize = 65536;
    char[] cbuf = new char[bufferSize];
    String fileType;
    String fileLengthStr;
    long fileLength;

    while (true) {
        try {
            fileType = valueStream.readUTF();
        } catch (EOFException e) {
            // EndOfFile
            return;
        }
        fileLengthStr = valueStream.readUTF();
        fileLength = Long.parseLong(fileLengthStr);
        writer.write("\n\nLogType:");
        writer.write(fileType);
        writer.write("\nLogLength:");
        writer.write(fileLengthStr);
        writer.write("\nLog Contents:\n");
        // ByteLevel
        BoundedInputStream bis = new BoundedInputStream(valueStream, fileLength);
        InputStreamReader reader = new InputStreamReader(bis);
        int currentRead = 0;
        int totalRead = 0;
        while ((currentRead = reader.read(cbuf, 0, bufferSize)) != -1) {
            writer.write(cbuf, 0, currentRead);
            totalRead += currentRead;
        }
    }
}

From source file:ca.rmen.android.networkmonitor.app.email.Emailer.java

/**
 * Append the given attachments to the message which is being written by the given writer.
 *
 * @param boundary separates each file attachment
 *//*from   ww w  .j a v  a2  s .c om*/
private static void appendAttachments(Writer writer, String boundary, Collection<File> attachments)
        throws IOException {
    for (File attachment : attachments) {
        ByteArrayOutputStream fileOs = new ByteArrayOutputStream((int) attachment.length());
        FileInputStream fileIs = new FileInputStream(attachment);
        try {
            IoUtil.copy(fileIs, fileOs);
        } finally {
            IoUtil.closeSilently(fileIs, fileOs);
        }
        final String mimeType = attachment.getName().substring(attachment.getName().indexOf(".") + 1);
        writer.write("--" + boundary + "\n");
        writer.write("Content-Type: application/" + mimeType + "; name=\"" + attachment.getName() + "\"\n");
        writer.write("Content-Disposition: attachment; filename=\"" + attachment.getName() + "\"\n");
        writer.write("Content-Transfer-Encoding: base64\n\n");
        String encodedFile = Base64.encodeToString(fileOs.toByteArray(), Base64.DEFAULT);
        writer.write(encodedFile);
        writer.write("\n");
    }
}