Example usage for java.io ByteArrayOutputStream toString

List of usage examples for java.io ByteArrayOutputStream toString

Introduction

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

Prototype

@Deprecated
public synchronized String toString(int hibyte) 

Source Link

Document

Creates a newly allocated string.

Usage

From source file:org.webpda.server.core.servermessage.ErrorMessage.java

@Override
public String createJson() throws JsonProcessingException {
    try {//from   www. j a v a  2 s  . co m
        JsonGenerator jg = createJsonGenerator();
        jg.writeStringField("title", title);
        jg.writeStringField("details", details);
        jg.writeEndObject();
        jg.close();
        ByteArrayOutputStream outputStream = (ByteArrayOutputStream) jg.getOutputTarget();
        String s = outputStream.toString(Constants.CHARSET);
        outputStream.close();
        return s;
    } catch (Exception e) {
        LoggerUtil.getLogger().log(Level.SEVERE, "Failed to create json.", e);
    }

    return null;
}

From source file:ar.com.zauber.commons.message.message.templates.AbstractMessageTemplate.java

/** Creates the AbstractMessageTemplate. */
public AbstractMessageTemplate(final Resource content, final String subject, final NotificationAddress address,
        final String charset) {
    Validate.notNull(content);//from www. j a v a  2 s .c  om
    Validate.notNull(subject);
    Validate.notNull(address);

    final ByteArrayOutputStream os = copyResource(content);
    try {
        this.content = os.toString(charset);
    } catch (UnsupportedEncodingException e) {
        throw new UnhandledException(e);
    }
    this.subject = subject;
    this.address = address;
}

From source file:gobblin.util.io.MeteredInputStreamTest.java

@Test
public void test() throws Exception {
    InputStream is = new ByteArrayInputStream("aabbccddee".getBytes(Charsets.UTF_8));

    Meter meter = new Meter();
    MeteredInputStream mis = MeteredInputStream.builder().in(is).meter(meter).updateFrequency(1).build();

    InputStream skipped = new MyInputStream(mis);
    DataInputStream dis = new DataInputStream(skipped);

    ByteArrayOutputStream os = new ByteArrayOutputStream();

    IOUtils.copy(dis, os);/*from  w w  w  .  j  av  a2 s . c  om*/
    String output = os.toString(Charsets.UTF_8.name());

    Assert.assertEquals(output, "abcde");

    Optional<MeteredInputStream> meteredOpt = MeteredInputStream.findWrappedMeteredInputStream(dis);
    Assert.assertEquals(meteredOpt.get(), mis);
    Assert.assertEquals(meteredOpt.get().getBytesProcessedMeter().getCount(), 10);
}

From source file:cz.zcu.kiv.eegdatabase.wui.core.experiments.metadata.ODMLSectionSerializer.java

@Override
public void serialize(Section value, JsonGenerator jgen, SerializerProvider provider)
        throws IOException, JsonProcessingException {
    long start = System.currentTimeMillis();
    try {//from   w  w  w . ja v a  2 s.  c om

        Writer wr = new Writer(value, true, true);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        wr.write(stream, false, false);

        String xmlString = stream.toString("UTF-8"); // encoding is necessary
        JSONObject jsonObject = XML.toJSONObject(xmlString);
        String jsonString = new String(jsonObject.toString()); // encoding is necessary
        jgen.writeRawValue(jsonString);

    } catch (JSONException e) {
        log.error(e.getMessage(), e);
    } finally {
        long end = System.currentTimeMillis();
        log.trace("Serialize time - " + (end - start) + " ms.");
    }

}

From source file:com.meltmedia.cadmium.servlets.guice.CadmiumListener.java

public final static Injector graphGood(File file, Injector inj) {
    try {/*  ww  w  .  j av a 2  s  .  co m*/
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(baos);

        Injector injector = Guice.createInjector(new GrapherModule(), new GraphvizModule());
        GraphvizRenderer renderer = injector.getInstance(GraphvizRenderer.class);
        renderer.setOut(out).setRankdir("TB");

        injector.getInstance(InjectorGrapher.class).of(inj).graph();

        out = new PrintWriter(file, "UTF-8");
        String s = baos.toString("UTF-8");
        s = fixGrapherBug(s);
        s = hideClassPaths(s);
        out.write(s);
        out.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return inj;
}

From source file:com.zaubersoftware.mule.module.jenkins.template.velocity.VelocityTemplate.java

/**
 * Creates the VelocityTemplate.// w  w w . j a  va  2 s. com
 *
 */
public VelocityTemplate(final Resource content, final String charset) {
    Validate.notNull(content);
    Validate.notNull(charset);

    InputStream is = null;
    try {
        is = content.getInputStream();
        final ByteArrayOutputStream os = new ByteArrayOutputStream();
        IOUtils.copy(is, os);
        os.close();
        message = os.toString(charset);
    } catch (final IOException e) {
        throw new UnhandledException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.mycontacts.resource.ContactResource.java

@Override
public void replaceContent(InputStream in, Long length)
        throws BadRequestException, ConflictException, NotAuthorizedException {
    try {//  ww w.  ja  v  a 2s. c om
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IOUtils.copy(in, bout);
        String icalData = bout.toString("UTF-8");
        contactManager.update(contact, icalData);
    } catch (IOException iOException) {
        throw new RuntimeException(iOException);
    }
}

From source file:com.ettrema.http.caldav.SchedulingCustomPostHandler.java

@Override
public void process(Resource resource, Request request, Response response) {
    log.trace("process");
    try {//from w ww.  ja va  2  s .c  o m
        SchedulingOutboxResource outbox = (SchedulingOutboxResource) resource;
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        IOUtils.copy(request.getInputStream(), bout);
        String iCalText = bout.toString("UTF-8");
        log.trace(iCalText);
        List<SchedulingResponseItem> respItems = outbox.queryFreeBusy(iCalText);

        String xml = schedulingHelper.generateXml(respItems);

        response.setStatus(Response.Status.SC_OK);
        response.setDateHeader(new Date());
        response.setContentTypeHeader("application/xml; charset=\"utf-8\"");
        response.setContentLengthHeader((long) xml.length());
        response.setEntity(new StringEntity(xml));
        // TODO: THIS IS NOT CALLED WITHIN THE STANDARDFILTER? DO WE NEED TO FLUSH HERE AGAIN?
        //response.close();

    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.lable.oss.dynamicconfig.serialization.yaml.JsonSerializerTest.java

@Test
public void testSave() throws ConfigurationException, IOException {
    HierarchicalConfigurationSerializer serializer = new JsonSerializer();
    HierarchicalConfiguration configuration = new HierarchicalConfiguration();
    configuration.setProperty("type.unicodeString", "");
    configuration.setProperty("type.booleanFalse", false);
    configuration.setProperty("type.booleanTrue", true);
    configuration.setProperty("type.list", Arrays.asList("1", "2", "3"));

    ByteArrayOutputStream output = new ByteArrayOutputStream();
    serializer.serialize(configuration, output);
    String json = IOUtils.toString(new StringReader(output.toString("UTF-8")));
    JsonNode tree = mapper.readTree(json);

    JsonNode nodeType = tree.get("type");
    assertThat(nodeType.get("unicodeString").textValue(), is(""));
    assertThat(nodeType.get("booleanFalse").booleanValue(), is(false));
    assertThat(nodeType.get("booleanTrue").booleanValue(), is(true));
    ArrayNode listString = (ArrayNode) nodeType.get("list");
    assertThat(listString, instanceOf(ArrayNode.class));
}

From source file:io.milton.http.annotated.ContactDataAnnotationHandler.java

public String execute(AnnoContactResource contactRes) {
    Object source = contactRes.getSource();
    try {/*from  ww  w . ja  v a 2s  .c  o m*/
        Object value = null;
        ControllerMethod cm = getBestMethod(source.getClass());
        if (cm == null) {
            // look for an annotation on the source itself
            java.lang.reflect.Method m = annoResourceFactory.findMethodForAnno(source.getClass(), annoClass);
            if (m != null) {
                value = m.invoke(source, (Object) null);
            } else {
                for (String nameProp : PROP_NAMES) {
                    if (PropertyUtils.isReadable(source, nameProp)) {
                        Object oPropVal = PropertyUtils.getProperty(source, nameProp);
                        value = oPropVal;
                        break;
                    }
                }
            }
        } else {
            value = invoke(cm, contactRes);
        }
        if (value != null) {
            if (value instanceof String) {
                return (String) value;
            } else if (value instanceof byte[]) {
                byte[] bytes = (byte[]) value;
                return new String(bytes, "UTF-8");
            } else if (value instanceof InputStream) {
                InputStream in = (InputStream) value;
                ByteArrayOutputStream bout = new ByteArrayOutputStream();
                IOUtils.copy(in, bout);
                return bout.toString("UTF-8");
            } else {
                return value.toString();
            }
        } else {
            return null;
        }
    } catch (Exception e) {
        throw new RuntimeException("Exception executing " + getClass() + " - " + source.getClass(), e);
    }
}