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:llc.rockford.webcast.StringUtils.java

public static String convertStreamToString(InputStream is) throws IOException {
    /*//from   w w w. jav a 2  s. co m
     * To convert the InputStream to String we use the Reader.read(char[]
     * buffer) method. We iterate until the Reader return -1 which means
     * there's no more data to read. We use the StringWriter class to
     * produce the string.
     */
    if (is != null) {
        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();
    } else {
        return "";
    }
}

From source file:com.evolveum.midpoint.gui.api.component.result.OpResult.java

public static OpResult getOpResult(PageBase page, OperationResult result) {
    OpResult opResult = new OpResult();
    Validate.notNull(result, "Operation result must not be null.");
    Validate.notNull(result.getStatus(), "Operation result status must not be null.");

    if (result.getCause() != null && result.getCause() instanceof CommonException) {
        LocalizableMessage localizableMessage = ((CommonException) result.getCause()).getUserFriendlyMessage();
        if (localizableMessage != null) {
            opResult.message = WebComponentUtil.resolveLocalizableMessage(localizableMessage, page);

            // Exclamation code:
            //                String key = localizableMessage.getKey() != null ? localizableMessage.getKey() : localizableMessage.getFallbackMessage();
            //              StringResourceModel stringResourceModel = new StringResourceModel(key, page).setModel(new Model<String>()).setDefaultValue(localizableMessage.getFallbackMessage())
            //            .setParameters(localizableMessage.getArgs());
            //              opResult.message = stringResourceModel.getString();
        }/*from  w  w w  .java 2 s  . c o m*/
    }

    if (opResult.message == null) {
        opResult.message = result.getMessage();
    }
    opResult.operation = result.getOperation();
    opResult.status = result.getStatus();
    opResult.count = result.getCount();
    opResult.userFriendlyMessage = result.getUserFriendlyMessage();

    if (result.getCause() != null) {
        Throwable cause = result.getCause();
        opResult.exceptionMessage = cause.getMessage();

        Writer writer = new StringWriter();
        cause.printStackTrace(new PrintWriter(writer));
        opResult.exceptionsStackTrace = writer.toString();
    }

    if (result.getParams() != null) {
        for (Map.Entry<String, Collection<String>> entry : result.getParams().entrySet()) {
            String paramValue = null;
            Collection<String> values = entry.getValue();
            if (values != null) {
                paramValue = values.toString();
            }

            opResult.getParams().add(new Param(entry.getKey(), paramValue));
        }
    }

    if (result.getContext() != null) {
        for (Map.Entry<String, Collection<String>> entry : result.getContext().entrySet()) {
            String contextValue = null;
            Collection<String> values = entry.getValue();
            if (values != null) {
                contextValue = values.toString();
            }

            opResult.getContexts().add(new Context(entry.getKey(), contextValue));
        }
    }

    if (result.getSubresults() != null) {
        for (OperationResult subresult : result.getSubresults()) {
            OpResult subOpResult = OpResult.getOpResult(page, subresult);
            opResult.getSubresults().add(subOpResult);
            subOpResult.parent = opResult;
            if (subOpResult.getBackgroundTaskOid() != null) {
                opResult.backgroundTaskOid = subOpResult.getBackgroundTaskOid();
            }
        }
    }

    if (result.getBackgroundTaskOid() != null) {
        opResult.backgroundTaskOid = result.getBackgroundTaskOid();
    }

    try {
        OperationResultType resultType = result.createOperationResultType();
        ObjectFactory of = new ObjectFactory();
        opResult.xml = page.getPrismContext().xmlSerializer().serialize(of.createOperationResult(resultType));
    } catch (SchemaException | RuntimeException ex) {
        String m = "Can't create xml: " + ex;
        //         error(m);
        opResult.xml = "<?xml version='1.0'?><message>" + StringEscapeUtils.escapeXml(m) + "</message>";
        //            throw ex;
    }
    return opResult;
}

From source file:org.benetech.secureapp.generator.SecureAppGeneratorApplication.java

private static String convertToBase64BySingleBytes(byte[] data) throws IOException {
    Writer writer = new StringWriter();
    XmlWriterFilter wf = new XmlWriterFilter(writer);
    Base64XmlOutputStream out = new Base64XmlOutputStream(wf);
    for (int i = 0; i < data.length; ++i)
        out.write(data[i]);/*w  w w .ja  v  a 2s. c o  m*/
    out.close();
    String result = "data:image/png;base64," + writer.toString();
    return result;
}

From source file:com.websqrd.catbot.scraping.HttpHandler.java

/**
 *  ? .//from  w  w  w.  j ava  2 s .  c o  m
 */
public static String executeGet2(HttpClient httpclient, String url, String encoding, String[] a)
        throws IOException {
    String type = "text";
    HttpGet httget = new HttpGet(url);
    HttpResponse response = httpclient.execute(httget);
    HttpEntity entity = response.getEntity();
    Header header = entity.getContentType();
    if (header != null) {
        type = header.getValue().toLowerCase();
    }

    //InputStream to String
    InputStream is = entity.getContent();
    Writer writer = new StringWriter();
    char[] buffer = new char[1024];
    try {
        Reader reader = new BufferedReader(new InputStreamReader(is, encoding));
        int n;
        while ((n = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, n);
        }
    } finally {
        is.close();
    }
    a[0] = writer.toString();
    return type;
}

From source file:com.mobicage.rogerthat.util.logging.L.java

public static String getStackTraceString(Throwable t) {
    final Writer w = new StringWriter();
    final PrintWriter pw = new PrintWriter(w);
    t.printStackTrace(pw);// w w w  .j a  v a 2s  .c o  m
    return w.toString();
}

From source file:main.java.refinement_class.Useful.java

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

From source file:com.twinsoft.convertigo.engine.util.CarUtils.java

private static void exportXMLProject(String fileName, Document document) throws EngineException {
    try {/* w ww  .j a  v  a  2 s.c  o m*/
        Transformer transformer = TransformerFactory.newInstance().newTransformer();

        boolean isCR = FileUtils.isCRLF();

        Writer writer = isCR ? new StringWriter() : new FileWriterWithEncoding(fileName, "UTF-8");
        transformer.transform(new DOMSource(document), new StreamResult(writer));

        if (isCR) {
            String content = FileUtils.CrlfToLf(writer.toString());
            writer = new FileWriterWithEncoding(fileName, "UTF-8");
            writer.write(content);
        }

        writer.close();
    } catch (Exception e) {
        throw new EngineException("(CarUtils) exportProject failed", e);
    }
}

From source file:com.vmware.identity.samlservice.Shared.java

/**
 * Return exception stack trace as a string
 *
 * @param throwable// www.  ja  v  a2s .co m
 * @return
 */
public static String getStackTrace(Throwable throwable) {
    Writer writer = new StringWriter();
    PrintWriter printWriter = new PrintWriter(writer);
    throwable.printStackTrace(printWriter);
    return writer.toString();
}

From source file:mobisocial.metrics.MusubiExceptionHandler.java

static JSONObject jsonForException(Context context, Throwable ex, boolean caught) {
    JSONObject json = new JSONObject();
    try {/*  w w  w  .j a  va2s .co  m*/
        Writer traceWriter = new StringWriter();
        PrintWriter printer = new PrintWriter(traceWriter);
        ex.printStackTrace(printer);
        json.put("type", "exception");
        json.put("caught", caught);
        json.put("app", context.getPackageName());
        json.put("message", ex.getMessage());
        json.put("trace", traceWriter.toString());
        json.put("timestamp", Long.toString(new Date().getTime()));

        boolean devmode = MusubiBaseActivity.isDeveloperModeEnabled(context);
        json.put("musubi_devmode", Boolean.toString(devmode));
        if (devmode) {
            IdentitiesManager im = new IdentitiesManager(App.getDatabaseSource(context));
            MIdentity id = im.getMyDefaultIdentity();
            String user = "Unknown";
            if (id != null) {
                user = UiUtil.safeNameForIdentity(id);
            }
            json.put("musubi_devmode_user_id", user);
            user = App.getMusubi(context).userForLocalDevice(null).getName();
            json.put("musubi_devmode_user_name", user);
        }
        try {
            PackageInfo info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
            json.put("musubi_version_name", info.versionName);
            json.put("musubi_version_code", Integer.toString(info.versionCode));
        } catch (NameNotFoundException e) {
            // shouldn't happen, but not fatal.
        }
        json.put("android_api", Integer.toString(Build.VERSION.SDK_INT));
        json.put("android_release", Build.VERSION.RELEASE);
        json.put("android_model", Build.MODEL);
        json.put("android_make", Build.MANUFACTURER);
    } catch (JSONException e) {
    }
    return json;
}

From source file:com.eucalyptus.imaging.manifest.DownloadManifestFactory.java

private static final String nodeToString(Node node, boolean addDeclaration) throws Exception {
    Transformer tf = TransformerFactory.newInstance().newTransformer();
    tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
    if (!addDeclaration)
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Writer out = new StringWriter();
    tf.transform(new DOMSource(node), new StreamResult(out));
    return out.toString();
}