Search in sources :

Example 91 with Writer

use of java.io.Writer in project camel by apache.

the class ApprovalRequestsTest method shouldSerializeAsXml.

@Test
public void shouldSerializeAsXml() {
    final String xml = //
    "<ProcessApprovalRequest>" + //
    "<requests>" + //
    "<actionType>Submit</actionType>" + //
    "<contextActorId>005D00000015rZy</contextActorId>" + //
    "<contextId>001D000000I8mIm</contextId>" + //
    "<comments>this is a test 1</comments>" + //
    "<nextApproverIds>005D00000015rY9</nextApproverIds>" + //
    "<processDefinitionNameOrId>PTO_Request_Process</processDefinitionNameOrId>" + //
    "<skipEntryCriteria>true</skipEntryCriteria>" + //
    "</requests>" + //
    "<requests>" + //
    "<actionType>Submit</actionType>" + //
    "<contextActorId>005D00000015rZy</contextActorId>" + //
    "<contextId>001D000000I8dIm</contextId>" + //
    "<comments>this is a test 2</comments>" + //
    "<nextApproverIds>005D00000015xY9</nextApproverIds>" + //
    "<processDefinitionNameOrId>PTO_Request_Process</processDefinitionNameOrId>" + //
    "<skipEntryCriteria>true</skipEntryCriteria>" + //
    "</requests>" + "</ProcessApprovalRequest>";
    final XStream xStream = new XStream(new XppDriver(new NoNameCoder()) {

        @Override
        public HierarchicalStreamWriter createWriter(final Writer out) {
            return new CompactWriter(out, getNameCoder());
        }
    });
    xStream.ignoreUnknownElements();
    XStreamUtils.addDefaultPermissions(xStream);
    xStream.registerConverter(new DateTimeConverter());
    xStream.processAnnotations(ApprovalRequests.class);
    final String serialized = xStream.toXML(requests);
    assertEquals("Approval requests should serialize as XML", xml, serialized);
}
Also used : CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) XppDriver(com.thoughtworks.xstream.io.xml.XppDriver) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) XStream(com.thoughtworks.xstream.XStream) DateTimeConverter(org.apache.camel.component.salesforce.api.utils.DateTimeConverter) NoNameCoder(com.thoughtworks.xstream.io.naming.NoNameCoder) HierarchicalStreamWriter(com.thoughtworks.xstream.io.HierarchicalStreamWriter) Writer(java.io.Writer) CompactWriter(com.thoughtworks.xstream.io.xml.CompactWriter) Test(org.junit.Test)

Example 92 with Writer

use of java.io.Writer in project storm-json by rapportive-oss.

the class SimpleJSONSerializer method serialize.

/**
     * Serialise a JSON object or array to the stream.
     *
     * @throws IllegalArgumentException  if <tt>object</tt> is not a JSON
     *           object or array
     * @throws IOException  if there is an error writing to the stream.
     */
@Override
public void serialize(Object object, DataOutputStream stream) throws IOException {
    final Writer writer = new OutputStreamWriter(stream, ENCODING);
    if (object instanceof JSONObject) {
        ((JSONObject) object).writeJSONString(writer);
    } else if (object instanceof JSONArray) {
        ((JSONArray) object).writeJSONString(writer);
    } else {
        throw new IllegalArgumentException("Unexpected class " + object.getClass().getCanonicalName());
    }
    writer.flush();
}
Also used : JSONObject(org.json.simple.JSONObject) JSONArray(org.json.simple.JSONArray) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Example 93 with Writer

use of java.io.Writer in project retrofit by square.

the class GsonRequestBodyConverter method convert.

@Override
public RequestBody convert(T value) throws IOException {
    Buffer buffer = new Buffer();
    Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
    JsonWriter jsonWriter = gson.newJsonWriter(writer);
    adapter.write(jsonWriter, value);
    jsonWriter.close();
    return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
}
Also used : Buffer(okio.Buffer) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(com.google.gson.stream.JsonWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) JsonWriter(com.google.gson.stream.JsonWriter)

Example 94 with Writer

use of java.io.Writer in project libgdx by libgdx.

the class FileDescriptor method writeString.

/** Writes the specified string to the file as UTF-8. Parent directories will be created if necessary.
	 * @param append If false, this file will be overwritten if it exists, otherwise it will be appended.
	 * @param charset May be null to use the default charset.
	 * @throw RuntimeException if this file handle represents a directory, if it is a {@link FileType#Classpath} or
	 *        FileType#Internal file, or if it could not be written. */
public void writeString(String string, boolean append, String charset) {
    Writer writer = null;
    try {
        writer = writer(append, charset);
        writer.write(string);
    } catch (Exception ex) {
        throw new RuntimeException("Error writing file: " + file + " (" + type + ")", ex);
    } finally {
        try {
            if (writer != null)
                writer.close();
        } catch (Exception ignored) {
        }
    }
}
Also used : Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 95 with Writer

use of java.io.Writer in project libgdx by libgdx.

the class FlameMain method saveEffect.

public void saveEffect(File file) {
    Writer fileWriter = null;
    try {
        ParticleEffectLoader loader = (ParticleEffectLoader) assetManager.getLoader(ParticleEffect.class);
        loader.save(effect, new ParticleEffectSaveParameter(new FileHandle(file.getAbsolutePath()), assetManager, particleSystem.getBatches()));
    } catch (Exception ex) {
        System.out.println("Error saving effect: " + file.getAbsolutePath());
        ex.printStackTrace();
        JOptionPane.showMessageDialog(this, "Error saving effect.");
    } finally {
        StreamUtils.closeQuietly(fileWriter);
    }
}
Also used : ParticleEffect(com.badlogic.gdx.graphics.g3d.particles.ParticleEffect) ParticleEffectSaveParameter(com.badlogic.gdx.graphics.g3d.particles.ParticleEffectLoader.ParticleEffectSaveParameter) FileHandle(com.badlogic.gdx.files.FileHandle) ParticleEffectLoader(com.badlogic.gdx.graphics.g3d.particles.ParticleEffectLoader) Writer(java.io.Writer)

Aggregations

Writer (java.io.Writer)1410 OutputStreamWriter (java.io.OutputStreamWriter)554 IOException (java.io.IOException)474 StringWriter (java.io.StringWriter)333 File (java.io.File)306 FileOutputStream (java.io.FileOutputStream)213 BufferedWriter (java.io.BufferedWriter)202 FileWriter (java.io.FileWriter)196 PrintWriter (java.io.PrintWriter)175 OutputStream (java.io.OutputStream)139 Test (org.junit.Test)120 InputStreamReader (java.io.InputStreamReader)81 Reader (java.io.Reader)74 BufferedReader (java.io.BufferedReader)72 ByteArrayOutputStream (java.io.ByteArrayOutputStream)70 ArrayList (java.util.ArrayList)66 HashMap (java.util.HashMap)66 Map (java.util.Map)65 InputStream (java.io.InputStream)61 Properties (java.util.Properties)40