Example usage for java.io Writer append

List of usage examples for java.io Writer append

Introduction

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

Prototype

public Writer append(char c) throws IOException 

Source Link

Document

Appends the specified character to this writer.

Usage

From source file:ded.model.Diagram.java

/** Write this diagram to the specified file. */
public void saveToFile(String fname) throws Exception {
    JSONObject serialized = this.toJSON();
    FileOutputStream fos = new FileOutputStream(fname);
    try {//from  www.jav a  2  s  .  com
        Writer w = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"));
        try {
            serialized.write(w, 2, 0);
            w.append('\n');
        } finally {
            w.close();
            fos = null;
        }
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
}

From source file:org.codehaus.groovy.grails.web.util.StreamCharBufferTests.java

public void testWritingStringBuffer() throws IOException {
    StreamCharBuffer charBuffer = new StreamCharBuffer(10, 0, 10);
    Writer writer = charBuffer.getWriter();
    StringBuffer sb = new StringBuffer("ABCDE12345ABCDEABCDE67890");
    writer.append(sb);
    writer.close();//  w w  w . j av a2 s . co m
    assertEquals(25, charBuffer.size());
    assertEquals("ABCDE12345ABCDEABCDE67890", charBuffer.toString());

    assertEquals(25, charBuffer.size());
}

From source file:org.codehaus.groovy.grails.web.util.StreamCharBufferTests.java

public void testWritingStringBuffer2() throws IOException {
    StreamCharBuffer charBuffer = new StreamCharBuffer(100, 0, 100);
    Writer writer = charBuffer.getWriter();
    StringBuffer sb = new StringBuffer("ABCDE12345ABCDEABCDE67890");
    writer.append(sb);
    writer.close();//  www .  j av a 2s .  c  om
    assertEquals(25, charBuffer.size());
    assertEquals("ABCDE12345ABCDEABCDE67890", charBuffer.toString());

    assertEquals(25, charBuffer.size());
}

From source file:org.codehaus.groovy.grails.web.util.StreamCharBufferTests.java

public void testWritingStringBuilder() throws IOException {
    StreamCharBuffer charBuffer = new StreamCharBuffer(10, 0, 10);
    Writer writer = charBuffer.getWriter();
    StringBuilder sb = new StringBuilder("ABCDE12345ABCDEABCDE67890");
    writer.append(sb);
    writer.close();//from  w  w  w . j a  va 2 s . co m
    assertEquals(25, charBuffer.size());
    assertEquals("ABCDE12345ABCDEABCDE67890", charBuffer.toString());

    assertEquals(25, charBuffer.size());
}

From source file:org.codehaus.groovy.grails.web.util.StreamCharBufferTests.java

public void testWritingStringBuilder2() throws IOException {
    StreamCharBuffer charBuffer = new StreamCharBuffer(100, 0, 100);
    Writer writer = charBuffer.getWriter();
    StringBuilder sb = new StringBuilder("ABCDE12345ABCDEABCDE67890");
    writer.append(sb);
    writer.close();/*from  w ww  . j  a  v a  2s  . c om*/
    assertEquals(25, charBuffer.size());
    assertEquals("ABCDE12345ABCDEABCDE67890", charBuffer.toString());

    assertEquals(25, charBuffer.size());
}

From source file:fi.okm.mpass.shibboleth.profile.impl.BuildServiceRestResponse.java

/** {@inheritDoc} */
@Override/*from   w  ww  .  j  a  v  a 2  s.  c om*/
@Nonnull
public Event execute(@Nonnull final RequestContext springRequestContext) {
    ComponentSupport.ifNotInitializedThrowUninitializedComponentException(this);
    final HttpServletRequest httpRequest = getHttpServletRequest();
    pushHttpResponseProperties();
    final HttpServletResponse httpResponse = getHttpServletResponse();

    try {
        final Writer out = new OutputStreamWriter(httpResponse.getOutputStream(), "UTF-8");

        if (!HttpMethod.GET.toString().equals(httpRequest.getMethod())) {
            log.warn("{}: Unsupported method attempted {}", getLogPrefix(), httpRequest.getMethod());
            out.append(makeErrorResponse(HttpStatus.SC_METHOD_NOT_ALLOWED,
                    httpRequest.getMethod() + " not allowed", "Only GET is allowed"));
        } else {
            final List<ServiceDTO> response = new ArrayList<ServiceDTO>();
            ServiceableComponent<MetadataResolver> component = null;
            component = service.getServiceableComponent();
            if (null == component) {
                log.error("{} Error accessing underlying metadata source: Invalid configuration.",
                        getLogPrefix());
            } else {
                final MetadataResolver resolver = component.getComponent();
                if (resolver instanceof RelyingPartyMetadataProvider
                        && ((RelyingPartyMetadataProvider) resolver)
                                .getEmbeddedResolver() instanceof ChainingMetadataResolver) {
                    final ChainingMetadataResolver embedded = (ChainingMetadataResolver) ((RelyingPartyMetadataProvider) resolver)
                            .getEmbeddedResolver();
                    for (final MetadataResolver chained : embedded.getResolvers()) {
                        final Iterator<EntityDescriptor> iterator = ((IterableMetadataSource) chained)
                                .iterator();
                        while (iterator.hasNext()) {
                            final EntityDescriptor entity = iterator.next();
                            final List<RoleDescriptor> roleDescriptors = entity.getRoleDescriptors();
                            if (roleDescriptors != null && !roleDescriptors.isEmpty()
                                    && roleDescriptors.get(0) instanceof SPSSODescriptor) {
                                final SPSSODescriptor spDescriptor = (SPSSODescriptor) roleDescriptors.get(0);
                                final ServiceDTO service = new ServiceDTO();
                                service.setId(entity.getID());
                                final UIInfo uiInfo = getUIInfo(spDescriptor);
                                if (uiInfo != null) {
                                    //TODO support languages
                                    service.setIconUrl(getLogoUrl(uiInfo, null));
                                    service.setTitle(getTitle(uiInfo, null));
                                    service.setDescription(getDescription(uiInfo, null));
                                    service.setServiceUrl(getServiceUrl(uiInfo, null));
                                }
                                if (StringSupport.trimOrNull(unsolicitedUrl) != null) {
                                    service.setSsoUrl(unsolicitedUrl + "?providerId=" + entity.getEntityID());
                                }
                                if (StringSupport.trimOrNull(service.getId()) != null
                                        && StringSupport.trimOrNull(service.getTitle()) != null
                                        && StringSupport.trimOrNull(service.getServiceUrl()) != null) {
                                    response.add(service);
                                }
                            } else {
                                log.debug("{} No SPSSODescriptor found", getLogPrefix());
                            }
                        }
                    }
                    final Gson gson = new Gson();
                    final LocalizedResponse localizedResponse = new LocalizedResponse();
                    localizedResponse.setLang("FI");
                    localizedResponse.setResponse(response);
                    out.append(gson.toJson(localizedResponse));
                    httpResponse.setStatus(HttpStatus.SC_OK);
                } else {
                    log.error("{} Unsupported metadata resolver, not IterableMetadataSource: {}",
                            getLogPrefix(), resolver.getClass());
                    out.append(makeErrorResponse(HttpStatus.SC_NOT_IMPLEMENTED,
                            "Not implemented on the server side", ""));
                }
            }
            if (null != component) {
                component.unpinComponent();
            }
        }
        out.flush();
    } catch (IOException e) {
        log.error("{}: Could not encode the JSON response", getLogPrefix(), e);
        httpResponse.setStatus(HttpStatus.SC_SERVICE_UNAVAILABLE);
        return ActionSupport.buildEvent(this, EventIds.IO_ERROR);
    }
    return ActionSupport.buildProceedEvent(this);
}

From source file:com.joliciel.csvLearner.maxent.MaxentBestFeatureObserver.java

public void writeFileTotalsToFile(Writer writer) {
    try {//w  w w.  j ava2 s  . c  o m
        boolean firstOutcome = true;
        Map<String, Double> totalPerFile = new HashMap<String, Double>();
        for (String fileName : fileNames)
            totalPerFile.put(fileName, 0.0);

        for (String outcome : filePercentagePerOutcome.keySet()) {
            if (firstOutcome) {
                writer.append("outcome,");
                for (String fileName : fileNames)
                    writer.append(CSVFormatter.format(fileName) + ",");
                writer.append("\n");
            }
            writer.append(CSVFormatter.format(outcome) + ",");
            for (String fileName : fileNames) {
                Double filePercentageObj = filePercentagePerOutcome.get(outcome).get(fileName);
                double filePercentage = filePercentageObj == null ? 0 : filePercentageObj.doubleValue();
                double currentTotal = totalPerFile.get(fileName);
                totalPerFile.put(fileName, currentTotal + filePercentage);
                writer.append(CSVFormatter.format(filePercentage) + ",");
            }
            writer.append("\n");
            firstOutcome = false;
        }

        writer.append(CSVFormatter.format("AVERAGE") + ",");
        for (String fileName : fileNames) {
            double total = totalPerFile.get(fileName);
            writer.append(CSVFormatter.format(total / filePercentagePerOutcome.size()) + ",");
        }
        writer.append("\n");
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:org.qi4j.entitystore.map.JSONMapEntityStoreMixin.java

protected void writeEntityState(JSONEntityState state, Writer writer, String identity, long lastModified)
        throws EntityStoreException {
    try {/*from ww  w . j ava  2s  .co m*/
        JSONObject jsonState = state.state();
        jsonState.put("version", identity);
        jsonState.put("modified", lastModified);
        writer.append(jsonState.toString());
    } catch (Exception e) {
        throw new EntityStoreException("Could not store EntityState", e);
    }
}

From source file:com.amalto.core.storage.record.ViewSearchResultsWriter.java

@Override
public void write(DataRecord record, Writer writer) throws IOException {
    writer.write("<result xmlns:metadata=\"" + DataRecordReader.METADATA_NAMESPACE + "\"" //$NON-NLS-1$ //$NON-NLS-2$
            + " xmlns:xsi=\"" + XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI + "\">\n"); //$NON-NLS-1$ //$NON-NLS-2$
    for (FieldMetadata fieldMetadata : record.getSetFields()) {
        Object value = record.get(fieldMetadata);
        String name = fieldMetadata.getName();
        if (value != null) {
            writer.append("\t<").append(name).append(">"); //$NON-NLS-1$ //$NON-NLS-2$
            processValue(writer, fieldMetadata, value);
            writer.append("</").append(name).append(">\n"); //$NON-NLS-1$ //$NON-NLS-2$
        } else {/*from   www.java2  s .c  om*/
            writer.append("\t<").append(name).append("/>\n"); //$NON-NLS-1$//$NON-NLS-2$
        }
    }
    writer.append("</result>"); //$NON-NLS-1$
    writer.flush();
}

From source file:net.paoding.rose.web.portal.impl.DefaultPipeRender.java

@Override
public void render(Writer out, Window window) throws IOException {

    JSONObject json = new JSONObject();
    json.put("content", window.getContent());
    json.put("id", window.getName());

    // javascript
    JSONArray js = getAttributeAsArray(window, PipeImpl.WINDIW_JS);
    if (js != null && js.length() > 0) {
        json.put("js", js);
    }/* www.  jav  a  2s . co m*/
    // css
    JSONArray css = getAttributeAsArray(window, PipeImpl.WINDOW_CSS);
    if (css != null && css.length() > 0) {
        json.put("css", css);
    }
    out.append("<script type=\"text/javascript\">");
    out.append("rosepipe.addWindow(");
    out.append(json.toString());
    out.append(");");
    out.append("</script>");
    out.append('\n');
}