Example usage for java.io Writer toString

List of usage examples for java.io Writer toString

Introduction

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

Prototype

public String toString() 

Source Link

Document

Returns a string representation of the object.

Usage

From source file:org.cryptsecure.HttpStringRequest.java

/**
 * Convert the content of a HTTP response into a string.
 * //www .ja va 2 s .  c o  m
 * @param response
 *            the response
 * @return the content as string
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
private static String getContentAsString(HttpResponse response) throws IOException {
    String returnString = "";
    HttpEntity httpEntity = response.getEntity();

    InputStream inputStream = httpEntity.getContent();
    InputStreamReader is = new InputStreamReader(inputStream, "ISO-8859-1"); // "UTF-8");

    if (is != null) {
        Writer writer = new StringWriter();

        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(is);
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }

        returnString = writer.toString().replace("\n", "");
    }

    return returnString;
}

From source file:no.digipost.android.utilities.JSONUtilities.java

public static String getJsonStringFromInputStream(final InputStream inputStream) {
    String content = "";

    if (inputStream != null) {
        Writer writer = new StringWriter();
        int buffer_size = 1024;
        char[] buffer = new char[buffer_size];

        try {//w  w  w. j a  v a2  s .  co  m
            Reader reader = new BufferedReader(new InputStreamReader(inputStream, ApiConstants.ENCODING),
                    buffer_size);
            int n;

            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }

            inputStream.close();
            reader.close();
            writer.close();
        } catch (Exception e) {
        }
        content = writer.toString();
    }
    return content;
}

From source file:Main.java

public static String getDefaultCurrencies(InputStream is) {

    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {/*from  w  w w.j  av a2s  . c  om*/
        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } catch (UnsupportedEncodingException e) {
        Log.e(TAG, "loadCurrencies " + "Encoding error");
    } catch (IOException e) {
        Log.e(TAG, "loadCurrencies " + "IOException");
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            Log.e(TAG, "loadCurrencies " + "IOException - close");
        }
    }
    return writer.toString();
}

From source file:org.godhuli.rhipe.FileUtils.java

public static String getStackTrace(final Throwable throwable) {
    final Writer result = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(result);
    throwable.printStackTrace(printWriter);
    return result.toString();
}

From source file:fr.mby.saml2.sp.impl.helper.SamlHelper.java

/**
 * Decode a SAML2 anthentication request for the HTTP-redirect binding.
 * //from   w w  w .  j  a  v  a2  s.c  om
 * @param authnRequest
 *            the authn request
 * @return the encoded request
 * @throws IOException
 */
public static String httpRedirectDecode(final String encodedRequest) throws IOException {
    String inflatedRequest = null;

    ByteArrayInputStream bytesIn = null;
    InflaterInputStream inflater = null;

    final byte[] decodedBytes = Base64.decode(encodedRequest);

    try {
        bytesIn = new ByteArrayInputStream(decodedBytes);
        inflater = new InflaterInputStream(bytesIn, new Inflater(true));
        final Writer writer = new StringWriter();
        final char[] buffer = new char[1024];

        final Reader reader = new BufferedReader(new InputStreamReader(inflater, "UTF-8"));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }

        inflatedRequest = writer.toString();
    } finally {
        if (bytesIn != null) {
            bytesIn.close();
        }
        if (inflater != null) {
            inflater.close();
        }
    }

    return inflatedRequest;
}

From source file:org.apache.ofbiz.webtools.print.FoPrintServerEvents.java

public static byte[] getXslFo(DispatchContext dctx, String screen, Map<String, Object> parameters)
        throws GeneralException {
    // run as the system user
    GenericValue system = null;//from   w  w w  .ja  va  2s  .com
    try {
        system = dctx.getDelegator().findOne("UserLogin", false, "userLoginId", "system");
    } catch (GenericEntityException e) {
        throw new GeneralException(e.getMessage(), e);
    }
    parameters.put("userLogin", system);
    if (!parameters.containsKey("locale")) {
        parameters.put("locale", Locale.getDefault());
    }

    // render and obtain the XSL-FO
    Writer writer = new StringWriter();
    try {
        ScreenStringRenderer screenStringRenderer = new MacroScreenRenderer(
                EntityUtilProperties.getPropertyValue("widget", "screen.name", dctx.getDelegator()),
                EntityUtilProperties.getPropertyValue("widget", "screen.screenrenderer", dctx.getDelegator()));
        ScreenRenderer screens = new ScreenRenderer(writer, null, screenStringRenderer);
        screens.populateContextForService(dctx, parameters);
        screens.render(screen);
    } catch (Throwable t) {
        throw new GeneralException("Problems rendering FOP XSL-FO", t);
    }
    return writer.toString().getBytes();
}

From source file:com.cloudbees.eclipse.core.util.Utils.java

public final static String readString(final InputStream is) throws CloudBeesException {
    if (is == null) {
        return null;
    }/*w  w  w . j  a va 2  s  .  co m*/
    try {
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    } catch (Exception e) {
        throw new CloudBeesException("Failed to read inputstream", e);
    }
}

From source file:eu.anynet.java.util.HttpClient.java

/**
 * Read from inputstream and get the result as string
 * @param stream The stream//w  w  w. j a  v a2  s.  c  o m
 * @param maxlength Max bytes to fetch, set &lt;1 to disable max length
 * @return The string
 * @throws IOException
 */
public static String toString(InputStream stream, int maxlength) throws IOException {
    BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
    Writer resultbuffer = new StringWriter();

    char buffer[] = new char[1024];
    int totalReadSize = 0, currentRead = 0, writelength = 0;

    while ((currentRead = reader.read(buffer, 0, buffer.length)) != -1) {
        totalReadSize += currentRead;
        writelength = currentRead;
        if (maxlength > 0 && totalReadSize > maxlength) {
            writelength = maxlength - totalReadSize;
        }

        if (writelength > 0) {
            resultbuffer.write(buffer, 0, writelength);
        } else {
            break;
        }
    }

    reader.close();
    return resultbuffer.toString();
}

From source file:com.adobe.cq.wcm.core.components.Utils.java

/**
 * Provided a {@code model} object and an {@code expectedJsonResource} identifying a JSON file in the class path, this method will
 * test the JSON export of the model and compare it to the JSON object provided by the {@code expectedJsonResource}.
 *
 * @param model                the Sling Model
 * @param expectedJsonResource the class path resource providing the expected JSON object
 *///from  w  w  w.j a  v  a2 s. c o  m
public static void testJSONExport(Object model, String expectedJsonResource) {
    Writer writer = new StringWriter();
    ObjectMapper mapper = new ObjectMapper();
    PageModuleProvider pageModuleProvider = new PageModuleProvider();
    mapper.registerModule(pageModuleProvider.getModule());
    DefaultMethodSkippingModuleProvider defaultMethodSkippingModuleProvider = new DefaultMethodSkippingModuleProvider();
    mapper.registerModule(defaultMethodSkippingModuleProvider.getModule());
    try {
        mapper.writer().writeValue(writer, model);
    } catch (IOException e) {
        fail(String.format("Unable to generate JSON export for model %s: %s", model.getClass().getName(),
                e.getMessage()));
    }
    JsonReader outputReader = Json.createReader(IOUtils.toInputStream(writer.toString()));
    InputStream is = Thread.currentThread().getContextClassLoader().getClass()
            .getResourceAsStream(expectedJsonResource);
    if (is != null) {
        JsonReader expectedReader = Json.createReader(is);
        assertEquals(expectedReader.read(), outputReader.read());
    } else {
        fail("Unable to find test file " + expectedJsonResource + ".");
    }
    IOUtils.closeQuietly(is);
}

From source file:org.atricore.idbus.capabilities.sso.support.core.util.XmlUtils.java

public static String marshal(Object msg, final String msgQName, final String msgLocalName,
        String[] userPackages) throws Exception {

    TreeSet<String> contextPackages = new TreeSet<String>();
    for (int i = 0; i < userPackages.length; i++) {
        String userPackage = userPackages[i];
        contextPackages.add(userPackage);
    }/*from  ww  w  . jav a 2s.  c  om*/

    JAXBContext jaxbContext = JAXBUtils.getJAXBContext(contextPackages, constructionType,
            contextPackages.toString(), XmlUtils.class.getClassLoader(), new HashMap<String, Object>());
    Marshaller marshaller = JAXBUtils.getJAXBMarshaller(jaxbContext);

    JAXBElement jaxbRequest = new JAXBElement(new QName(msgQName, msgLocalName), msg.getClass(), msg);

    Writer writer = new StringWriter();

    // Support XMLDsig
    XMLEventWriter xmlWriter = staxOF.createXMLEventWriter(writer);
    marshaller.marshal(jaxbRequest, xmlWriter);
    xmlWriter.flush();
    JAXBUtils.releaseJAXBMarshaller(jaxbContext, marshaller);

    return writer.toString();

}