Example usage for java.io OutputStreamWriter OutputStreamWriter

List of usage examples for java.io OutputStreamWriter OutputStreamWriter

Introduction

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

Prototype

public OutputStreamWriter(OutputStream out) 

Source Link

Document

Creates an OutputStreamWriter that uses the default character encoding.

Usage

From source file:com.wondershare.http.server.impl.CallogServlet.java

@Override
protected void doGet(HttpRequest request, HttpResponse response, HttpContext context)
        throws IOException, ServletException {
    Cursor cursor = mContext.getContentResolver().query(callogUri, SysConst.CALLOG_PROJECTION, null, null,
            null);/*from  w  w  w . jav  a  2  s.  c o m*/
    final String html = Utils.spellCallogToHTML(cursor);
    HttpEntity entity = new EntityTemplate(new ContentProducer() {
        @Override
        public void writeTo(OutputStream outstream) throws IOException {
            OutputStreamWriter out = new OutputStreamWriter(outstream);
            out.write(html);
            out.flush();
        }
    });
    ((EntityTemplate) entity).setContentType("text/html");
    response.setEntity(entity);
}

From source file:jatoo.exec.InputStreamExhausterWithDumpStream.java

public final void exhaust() {

    BufferedReader processInputStreamReader = null;
    BufferedWriter dumpOutputStreamWriter = null;

    try {/*from   ww w.ja va 2s .co m*/

        processInputStreamReader = new BufferedReader(new InputStreamReader(processInputStream));
        dumpOutputStreamWriter = new BufferedWriter(new OutputStreamWriter(dumpOutputStream));

        String line = null;
        while ((line = processInputStreamReader.readLine()) != null) {

            dumpOutputStreamWriter.write(line);
            dumpOutputStreamWriter.newLine();

            dumpOutputStreamWriter.flush();
        }
    }

    catch (IOException e) {
        LOGGER.error("error exhausting the stream", e);
    }

    finally {
        if (closeDumpOutputStream && dumpOutputStreamWriter != null) {
            try {
                dumpOutputStreamWriter.close();
            } catch (IOException e) {
                LOGGER.error("error closing the dump stream", e);
            }
        }
    }
}

From source file:de.anycook.social.facebook.FacebookHandler.java

@SuppressWarnings("unchecked")
public static String publishtoWall(long facebookID, String accessToken, String message, String header)
        throws IOException {
    StringBuilder out = new StringBuilder();
    StringBuilder data = new StringBuilder();
    data.append("access_token=").append(URLEncoder.encode(accessToken, "UTF-8"));
    data.append("&message=").append(URLEncoder.encode(message, "UTF-8"));
    data.append("&name=").append(URLEncoder.encode(header, "UTF-8"));

    URL url = new URL("https://api.facebook.com/" + facebookID + "/feed");
    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);//from   w w  w.  ja  va2  s .c  o  m
    try (OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream())) {
        wr.write(data.toString());
        wr.flush();

        try (BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()))) {
            String line;
            while ((line = rd.readLine()) != null) {
                out.append(line);
            }
        }
    }

    return out.toString();
}

From source file:com.gu.management.logging.Log4JManagerServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    response.setContentType("text/html");
    OutputStreamWriter fileWriter = new OutputStreamWriter(response.getOutputStream());

    fileWriter.write(MANAGEMENT_PAGE_HEAD);

    @SuppressWarnings({ "unchecked" })
    List<Logger> loggers = sortLoggers(LogManager.getCurrentLoggers());

    for (Logger logger : loggers) {
        fileWriter.write(String.format(LOGGER_TABLE_ROW, logger.getName(), generateOptionsFor(logger),
                logger.getEffectiveLevel()));
    }/*w ww .j  ava2s. co  m*/

    fileWriter.write(MANAGEMENT_PAGE_FOOT);

    fileWriter.flush();
}

From source file:com.bstek.dorado.idesupport.RuleSetBuilderTest.java

public void testOutputAndParse() throws Exception {
    RuleTemplateManager ruleTemplateManager = getRuleTemplateBuilder().getRuleTemplateManager();

    byte[] byteArray = null;
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(out);
    try {/*from  w  w w  .j  av  a 2  s .  c o m*/
        getRuleSetOutputter().output(writer, ruleTemplateManager);
        byteArray = out.toByteArray();
    } finally {
        writer.close();
        out.close();
    }

    assertNotNull(byteArray);
    InputStream in = new ByteArrayInputStream(byteArray);
    try {
        RuleSet ruleSet = getRuleSetBuilder().buildRuleSet(in);
        assertNotNull(ruleSet);
        assertFalse(ruleSet.getRuleMap().isEmpty());

        List<PackageInfo> packageInfos = ruleSet.getPackageInfos();
        assertNotNull(packageInfos);
        assertTrue(!packageInfos.isEmpty());

        Rule modelRule = ruleSet.getRule("Model");
        assertNotNull(modelRule);

        Map<String, Child> children = modelRule.getChildren();
        assertTrue(children.size() >= 3);

        Child dataTypeChild = children.get("DataType");
        Rule dataTypeRule = dataTypeChild.getRule();
        assertNotNull(dataTypeRule);
        assertEquals("DataType", dataTypeRule.getNodeName());

        Child dataProviderChild = children.get("DataProvider");
        Rule dataProviderRule = dataProviderChild.getRule();
        assertNotNull(dataProviderRule);

        Rule propertyDefRule = ruleSet.getRule("PropertyDef");
        assertNotNull(propertyDefRule);

        Child validatorChild = propertyDefRule.getChild("Validator");
        assertNotNull(validatorChild);
        System.out.println("Children of ValidotorRule: ");
        Set<Rule> validatorRules = validatorChild.getConcreteRules();
        for (Rule validatorRule : validatorRules) {
            System.out.println(validatorRule.getName() + " : " + validatorRule.getNodeName());
        }

        Property mappingProperty = propertyDefRule.getProperty("mapping");
        assertNotNull(mappingProperty);
        assertTrue(mappingProperty.getCompositeType() == CompositeType.Fixed);
        assertEquals(3, mappingProperty.getProperties().size());

        Rule buttonRule = ruleSet.getRule("Button");
        assertNotNull(buttonRule);
        Property property = buttonRule.getProperty("action");
        assertNotNull(property);
        assertEquals("Action", property.getReference().getRule().getName());

        Rule abstractButtonRule = ruleSet.getRule("AbstractButton");
        assertNotNull(abstractButtonRule);
        assertTrue(ArrayUtils.indexOf(buttonRule.getParents(), abstractButtonRule) >= 0);

        boolean hasButtonRule = false;
        Rule[] concretButtonRules = abstractButtonRule.getSubRules();
        for (Rule concretButtonRule : concretButtonRules) {
            String name = concretButtonRule.getName();
            if ("Button".equals(name)) {
                hasButtonRule = true;
            }
        }
        assertTrue(hasButtonRule);

        Rule controlRule = ruleSet.getRule("Control_1");
        assertNotNull(controlRule);
        assertTrue(ArrayUtils.indexOf(abstractButtonRule.getParents(), controlRule) >= 0);

        boolean hasAbstractButtonRule = false;
        Rule[] concretControlRules = controlRule.getSubRules();
        for (Rule concretControlRule : concretControlRules) {
            String name = concretControlRule.getName();
            if ("AbstractButton".equals(name)) {
                hasAbstractButtonRule = true;
            }
        }
        assertTrue(hasAbstractButtonRule);

        Rule floatPanelRule = ruleSet.getRule("FloatPanel");
        assertNotNull(floatPanelRule);
        assertNotNull(floatPanelRule.getChild("Children"));
        assertNotNull(floatPanelRule.getChild("Tools"));
        assertNotNull(floatPanelRule.getChild("Buttons"));

        Property animateTargetProperty = floatPanelRule.getProperty("animateTarget");
        assertNotNull(animateTargetProperty);
        assertFalse(animateTargetProperty.isVisible());

        Rule viewRule = ruleSet.getRule("View");
        for (Child child : viewRule.getChildren().values()) {
            Rule childRule = child.getRule();
            System.out.println(childRule.getName() + " : " + childRule.getNodeName());
        }

        Rule dataTreeGridRule = ruleSet.getRule("DataTreeGrid");
        assertNotNull(dataTreeGridRule);
        property = dataTreeGridRule.getProperty("dataSet");
        assertNotNull(property);
        assertEquals("DataSet", property.getReference().getRule().getName());

        Rule treeRule = ruleSet.getRule("Tree");
        assertNotNull(treeRule);

        Rule customDropDownRule = ruleSet.getRule("CustomDropDown");
        assertNotNull(customDropDownRule);

        Child child = customDropDownRule.getChild("Control");
        assertNotNull(child);

        System.out.println("Children of CustomDropDown: ");
        Set<Rule> concreteRules = child.getConcreteRules();
        for (Rule concreteRule : concreteRules) {
            System.out.println(concreteRule.getName() + " : " + concreteRule.getNodeName());
        }
    } finally {
        in.close();
    }
}

From source file:com.roche.iceboar.demo.JnlpServlet.java

/**
 * This method handle all HTTP requests for *.jnlp files (defined in web.xml). Method check, is name correct
 * (allowed), read file from disk, replace #{codebase} (it's necessary to be generated based on where application
 * is deployed), #{host} () and write to the response.
 * <p>//from   ww w  .j  a v  a  2 s . c  o  m
 * You can use this class in your code for downloading JNLP files.
 * Return a content of requested jnlp file in response.
 *
 * @throws IOException when can't close some stream
 */
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String contextPath = request.getContextPath();
    String requestURI = request.getRequestURI();
    String host = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    String codebase = host + contextPath;
    String filename = StringUtils.removeStart(requestURI, contextPath);
    response.setContentType("application/x-java-jnlp-file");
    response.addHeader("Pragma", "no-cache");
    response.addHeader("Expires", "-1");

    OutputStreamWriter out = new OutputStreamWriter(response.getOutputStream());

    InputStream in = JnlpServlet.class.getResourceAsStream(filename);
    if (in == null) {
        error(response, "Can't open: " + filename);
        return;
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(in));

    String line = reader.readLine();
    while (line != null) {
        line = line.replace("#{codebase}", codebase);
        line = line.replace("#{host}", host);
        out.write(line);
        out.write("\n");
        line = reader.readLine();
    }

    out.flush();
    out.close();
    reader.close();
}