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.jhkt.playgroundArena.examples.generic.controller.GeneralController.java

@RequestMapping(value = "/sayHello", method = RequestMethod.GET)
public void sample(WebRequest request, Writer writer) {
    try {/*from  ww  w.  j  a v  a2s .co m*/
        writer.write("I say Hello");
        writer.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.alfresco.web.scripts.MessagesWebScript.java

/**
 * Generate the message for a given locale.
 * //ww w  .  j a  v  a  2 s .c  o m
 * @param locale    Java locale format
 * 
 * @return messages as JSON string
 * 
 * @throws IOException
 */
@Override
protected String generateMessages(WebScriptRequest req, WebScriptResponse res, String locale)
        throws IOException {
    Writer writer = new StringBuilderWriter(8192);
    writer.write("if (typeof Alfresco == \"undefined\" || !Alfresco) {var Alfresco = {};}\r\n");
    writer.write("Alfresco.messages = Alfresco.messages || {global: null, scope: {}}\r\n");
    writer.write("Alfresco.messages.global = ");
    JSONWriter out = new JSONWriter(writer);
    try {
        out.startObject();
        Map<String, String> messages = I18NUtil.getAllMessages(I18NUtil.parseLocale(locale));
        for (Map.Entry<String, String> entry : messages.entrySet()) {
            out.writeValue(entry.getKey(), entry.getValue());
        }
        out.endObject();
    } catch (IOException jsonErr) {
        throw new WebScriptException("Error building messages response.", jsonErr);
    }
    writer.write(";\r\n//Make global for sandbox mode\r\nwindow.Alfresco=Alfresco;\r\n");

    return writer.toString();
}

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

private void exportWorkloadLog(Writer writer) throws IOException {
    writer.write("=========================");
    writer.write("=========================");
    writer.write(" workload level ");
    writer.write("=========================");
    writer.write("=========================");
    writer.write('\n');
    String wsId = workload.getId();
    doExportLog(writer, wsId);//  w w  w  . j  av  a 2s  . c  o m
    writer.flush();
}

From source file:edu.cornell.mannlib.vitro.webapp.web.directives.UrlDirective.java

@Override
public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
        throws TemplateException, IOException {

    if (loopVars.length != 0) {
        throw new TemplateModelException("The url directive doesn't allow loop variables.");
    }//from  w  ww  .  jav a2 s. co m

    if (body != null) {
        throw new TemplateModelException("The url directive doesn't allow nested content.");
    }

    Object o = params.get("path");
    if (o == null) {
        throw new TemplateModelException("The url directive requires a value for parameter 'path'.");
    }

    if (!(o instanceof SimpleScalar)) {
        throw new TemplateModelException("The url directive requires a string value for parameter 'path'.");
    }

    String path = o.toString();

    if (!path.startsWith("/")) {
        throw new TemplateModelException(
                "The url directive requires that the value of parameter 'path' is an absolute path starting with '/'.");
    }

    String url = UrlBuilder.getUrl(path);
    Writer out = env.getOut();
    out.write(url);
}

From source file:org.ofbiz.common.CommonServices.java

public static Map<String, Object> streamTest(DispatchContext dctx, Map<String, ?> context) {
    InputStream in = (InputStream) context.get("inputStream");
    OutputStream out = (OutputStream) context.get("outputStream");

    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
    Writer writer = new OutputStreamWriter(out);
    String line;/*from w  w  w  .  jav a 2 s . c  o  m*/

    try {
        while ((line = reader.readLine()) != null) {
            Debug.log("Read line: " + line, module);
            writer.write(line);
        }
    } catch (IOException e) {
        Debug.logError(e, module);
        return ServiceUtil.returnError(e.getMessage());
    } finally {
        try {
            writer.close();
        } catch (Exception e) {
            Debug.logError(e, module);
        }
    }

    Map<String, Object> result = ServiceUtil.returnSuccess();
    result.put("contentType", "text/plain");
    return result;
}

From source file:com.antsdb.saltedfish.sql.vdm.ExprUtil.java

/**
 * <p>Unescapes any Java literals found in the <code>String</code> to a
 * <code>Writer</code>.</p>
 *
 * <p>For example, it will turn a sequence of <code>'\'</code> and
 * <code>'n'</code> into a newline character, unless the <code>'\'</code>
 * is preceded by another <code>'\'</code>.</p>
 * /* w ww.j ava 2 s  . c o  m*/
 * <p>A <code>null</code> string input has no effect.</p>
 * 
 * @see http://dev.mysql.com/doc/refman/5.7/en/string-literals.html
 * 
 * @param out  the <code>Writer</code> used to output unescaped characters
 * @param str  the <code>String</code> to unescape, may be null
 * @throws IllegalArgumentException if the Writer is <code>null</code>
 * @throws IOException if error occurs on underlying Writer
 */
public static void unescapeJava(Writer out, String str) throws IOException {
    if (out == null) {
        throw new IllegalArgumentException("The Writer must not be null");
    }
    if (str == null) {
        return;
    }
    int sz = str.length();
    StrBuilder unicode = new StrBuilder(4);
    boolean hadSlash = false;
    boolean inUnicode = false;
    for (int i = 0; i < sz; i++) {
        char ch = str.charAt(i);
        if (inUnicode) {
            // if in unicode, then we're reading unicode
            // values in somehow
            unicode.append(ch);
            if (unicode.length() == 4) {
                // unicode now contains the four hex digits
                // which represents our unicode character
                try {
                    int value = Integer.parseInt(unicode.toString(), 16);
                    out.write((char) value);
                    unicode.setLength(0);
                    inUnicode = false;
                    hadSlash = false;
                } catch (NumberFormatException nfe) {
                    throw new NestableRuntimeException("Unable to parse unicode value: " + unicode, nfe);
                }
            }
            continue;
        }
        if (hadSlash) {
            // handle an escaped value
            hadSlash = false;
            switch (ch) {
            case '0':
                out.write(0);
                break;
            case '\'':
                out.write('\'');
                break;
            case '\"':
                out.write('"');
                break;
            case 'b':
                out.write('\b');
                break;
            case 'n':
                out.write('\n');
                break;
            case 'r':
                out.write('\r');
                break;
            case 't':
                out.write('\t');
                break;
            case 'Z':
                out.write(26);
                break;
            case '\\':
                out.write('\\');
                break;
            case '%':
                out.write('%');
                break;
            case '_':
                out.write('_');
                break;
            case 'u': {
                // uh-oh, we're in unicode country....
                inUnicode = true;
                break;
            }
            default:
                out.write(ch);
            }
            continue;
        } else if (ch == '\\') {
            hadSlash = true;
            continue;
        }
        out.write(ch);
    }
    if (hadSlash) {
        // then we're in the weird case of a \ at the end of the
        // string, let's output it anyway.
        out.write('\\');
    }
}

From source file:com.linuxbox.enkive.message.MultiPartHeaderImpl.java

@Override
public void pushReconstitutedEmail(Writer output) throws IOException {
    output.write(printMultiPartHeader());
    output.flush();/*w  ww  .  j ava  2 s .  co m*/
}

From source file:com.amalto.core.storage.ItemPKCriteriaResultsWriter.java

private void doWrite(DataRecord record, Writer writer) throws IOException {
    writer.write("<r>"); //$NON-NLS-1$
    {// w w w. j a  v  a 2  s.c o  m
        Object timestamp;
        if (record.getType().hasField("timestamp")) { //$NON-NLS-1$
            timestamp = record.get("timestamp"); //$NON-NLS-1$
        } else {
            timestamp = record.getRecordMetadata().getLastModificationTime();
        }
        Object taskId;
        if (record.getType().hasField("taskid")) { //$NON-NLS-1$
            taskId = record.get("taskid"); //$NON-NLS-1$
        } else {
            taskId = record.getRecordMetadata().getLastModificationTime();
        }
        writer.write("<t>" + timestamp + "</t>"); //$NON-NLS-1$ //$NON-NLS-2$
        writer.write("<taskId>" + taskId + "</taskId>"); //$NON-NLS-1$ //$NON-NLS-2$
        writer.write("<n>" + typeName + "</n>"); //$NON-NLS-1$ //$NON-NLS-2$
        writer.write("<ids>"); //$NON-NLS-1$
        Collection<FieldMetadata> keyFields = itemType.getKeyFields();
        for (FieldMetadata keyField : keyFields) {
            writer.write("<i>" //$NON-NLS-1$
                    + StringEscapeUtils.escapeXml(StorageMetadataUtils.toString(record.get(keyField), keyField))
                    + "</i>"); //$NON-NLS-1$
        }
        writer.write("</ids>"); //$NON-NLS-1$
    }
    writer.write("</r>"); //$NON-NLS-1$
    writer.flush();
}

From source file:com.googlecode.jsfFlex.phaseListener.RawServiceRequestDataRetrieverFlusher.java

@Override
void retrieveFlushData(FacesContext context, String componentId, String methodToInvoke)
        throws ServletException, IOException {

    Collection<? extends Object> objectCollection = null;

    try {/*from  w  w  w .  j a v  a2 s  .c o m*/
        objectCollection = (Collection<? extends Object>) invokeResourceMethod(context, componentId,
                methodToInvoke, null, null);
    } catch (Exception methodInvocationException) {
        throw new ServletException(methodInvocationException);
    }

    HttpServletResponse response = HttpServletResponse.class.cast(context.getExternalContext().getResponse());
    response.setContentType(PLAIN_CONTENT_TYPE);

    if (objectCollection != null) {
        StringBuilder responseContent = new StringBuilder();

        for (Object currObj : objectCollection) {
            responseContent.append(currObj.toString());
        }

        _log.info("Flushing content : " + responseContent.toString());

        Writer writer = response.getWriter();
        writer.write(responseContent.toString());
        writer.flush();
    }

}

From source file:org.orderofthebee.addons.support.tools.repo.LogFileDelete.java

/**
 *
 * {@inheritDoc}/*from  www.  j  a v  a2s .c  om*/
 */
@Override
public void execute(final WebScriptRequest req, final WebScriptResponse res) throws IOException {
    final String servicePath = req.getServicePath();
    final String matchPath = req.getServiceMatch().getPath();
    final String filePath = servicePath.substring(servicePath.indexOf(matchPath) + matchPath.length());

    final File file = this.validateFilePath(filePath);
    if (file.delete()) {
        res.setStatus(Status.STATUS_OK);
        // we "have" to send a dummy JSON as repository admin console client JS always tries to parse
        final Writer writer = res.getWriter();
        try {
            writer.write("{}");
        } finally {
            writer.close();
        }
    } else {
        throw new WebScriptException(Status.STATUS_INTERNAL_SERVER_ERROR,
                "Log file could not be deleted immediately");
    }
}