Example usage for java.lang Appendable append

List of usage examples for java.lang Appendable append

Introduction

In this page you can find the example usage for java.lang Appendable append.

Prototype

Appendable append(char c) throws IOException;

Source Link

Document

Appends the specified character to this Appendable .

Usage

From source file:org.omnaest.utils.structure.container.ByteArrayContainer.java

/**
 * Writes the content of the {@link ByteArrayContainer} to an {@link Appendable} e.g. a {@link StringBuilder} or
 * {@link StringBuffer}//from  w ww . j a  va 2 s .  co  m
 * 
 * @param appendable
 * @param encoding
 * @return true : transfer successful
 */
public boolean writeTo(Appendable appendable, String encoding) {
    //
    boolean retval = true;

    //
    try {
        StringBuffer sb = new StringBuffer();
        StreamConnector.connect(this.getInputStream(), sb, encoding);

        //
        appendable.append(sb);
    } catch (IOException e) {
        retval = false;
        this.handleException(e);
    }

    //
    return retval;
}

From source file:cn.sinobest.jzpt.framework.utils.string.StringUtils.java

/**
 * StringBuilderAppendable//from  ww w .j a  va2s . c  o  m
 *
 * @param sb
 * @param str
 * @param n 0
 */
public static void repeat(Appendable sb, char str, int n) {
    if (n <= 0)
        return;
    try {
        for (int i = 0; i < n; i++) {
            sb.append(str);
        }
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:cn.sinobest.jzpt.framework.utils.string.StringUtils.java

/**
 * StringBuilderAppendable/*from w w  w.  jav  a  2s. com*/
 *
 * @param sb
 * @param str
 * @param n
 */
public static void repeat(Appendable sb, CharSequence str, int n) {
    if (n <= 0)
        return;
    try {
        for (int i = 0; i < n; i++) {
            sb.append(str);
        }
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:org.apache.ofbiz.content.data.DataResourceWorker.java

public static void writeText(GenericValue dataResource, String textData, Map<String, Object> context,
        String targetMimeTypeId, Locale locale, Appendable out) throws GeneralException, IOException {
    String dataResourceMimeTypeId = dataResource.getString("mimeTypeId");
    Delegator delegator = dataResource.getDelegator();

    // assume HTML as data resource data
    if (UtilValidate.isEmpty(dataResourceMimeTypeId)) {
        dataResourceMimeTypeId = "text/html";
    }/*from  w ww . ja v  a 2  s.co m*/

    // assume HTML for target
    if (UtilValidate.isEmpty(targetMimeTypeId)) {
        targetMimeTypeId = "text/html";
    }

    // we can only render text
    if (!targetMimeTypeId.startsWith("text")) {
        throw new GeneralException("Method writeText() only supports rendering text content : "
                + targetMimeTypeId + " is not supported");
    }

    if ("text/html".equals(targetMimeTypeId)) {
        // get the default mime type template
        GenericValue mimeTypeTemplate = EntityQuery.use(delegator).from("MimeTypeHtmlTemplate")
                .where("mimeTypeId", dataResourceMimeTypeId).cache().queryOne();

        if (mimeTypeTemplate != null && mimeTypeTemplate.get("templateLocation") != null) {
            // prepare the context
            Map<String, Object> mimeContext = new HashMap<String, Object>();
            mimeContext.putAll(context);
            mimeContext.put("dataResource", dataResource);
            mimeContext.put("textData", textData);

            String mimeString = DataResourceWorker.renderMimeTypeTemplate(mimeTypeTemplate, mimeContext);
            if (mimeString != null) {
                out.append(mimeString);
            }
        } else {
            if (textData != null) {
                out.append(textData);
            }
        }
    } else {
        out.append(textData);
    }
}

From source file:com.github.crab2died.ExcelUtils.java

private void exportCSVByMapHandler(List<?> data, Class clazz, boolean isWriteHeader, Appendable appendable)
        throws Excel4JException, IOException {

    List<ExcelHeader> headers = Utils.getHeaderList(clazz);
    appendable.append(new String(UTF_8_DOM, StandardCharsets.UTF_8));

    try (CSVPrinter printer = new CSVPrinter(appendable, CSVFormat.EXCEL)) {

        if (isWriteHeader) {
            for (ExcelHeader header : headers) {
                printer.print(header.getTitle());
            }/*  w ww. j a  va2 s . c  o m*/
            printer.println();
        }
        // ?
        for (Object _data : data) {
            for (ExcelHeader header : headers) {
                printer.print(Utils.getProperty(_data, header.getFiled(), header.getWriteConverter()));
            }
            printer.println();
        }
        printer.flush();
    }
}

From source file:com.cloudant.tests.UnicodeTest.java

/**
 * Copies the content of an entity to an Appendable, as a sequence of chars.
 *
 * @param destination An Appendable (such as a StringBuilder, a Writer, or a PrintStream).
 * @param reader      A Reader that wraps the InputStream of the entity returned by the given
 *                    URI.//from   w  ww . jav a 2s .c o  m
 *                    Should be buffered.
 * @throws RuntimeException if there is an exception reading the entity
 * @throws IOException      if there is an exception writing to the destination
 */
private static void pipeEntityContentAsChars(Appendable destination, Reader reader, URI uri)
        throws IOException {
    char[] buffer = new char[1024];
    for (;;) {
        int n;
        try {
            n = reader.read(buffer);
        } catch (SocketException e) {
            // At EOF, we may get this exception:
            // java.net.SocketException: Socket is closed
            //   at com.sun.net.ssl.internal.ssl.SSLSocketImpl.checkEOF(SSLSocketImpl.java:1284)
            //   at com.sun.net.ssl.internal.ssl.AppInputStream.read(AppInputStream.java:65)
            //   at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer
            // (AbstractSessionInputBuffer.java:149)
            //   at org.apache.http.impl.io.SocketInputBuffer.fillBuffer(SocketInputBuffer
            // .java:110)
            //   at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine
            // (AbstractSessionInputBuffer.java:264)
            //   at org.apache.http.impl.io.ChunkedInputStream.getChunkSize
            // (ChunkedInputStream.java:246)
            //   at org.apache.http.impl.io.ChunkedInputStream.nextChunk(ChunkedInputStream
            // .java:204)
            //   at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:167)
            //   at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream
            // .java:138)
            //   at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
            //   at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
            //   at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
            //   at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
            //   at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
            //   at java.io.InputStreamReader.read(InputStreamReader.java:167)
            //   at java.io.Reader.read(Reader.java:123)
            // See also http://stackoverflow
            // .com/questions/17040698/httpcomponentss-ssl-connection-results-in-socket-is
            // -closed
            break;
        } catch (IOException e) {
            throw new RuntimeException("Error reading from " + uri, e);
        }
        if (n < 0) {
            break;
        }
        destination.append(CharBuffer.wrap(buffer, 0, n));
    }
}

From source file:org.ramadda.util.Utils.java

/**
 * _more_/*  ww  w.j a v  a2 s. co m*/
 *
 * @param sb _more_
 * @param s _more_
 *
 * @return _more_
 */
public static Appendable append(Appendable sb, String s) {
    try {
        sb.append(s);

        return sb;
    } catch (java.io.IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:org.auraframework.impl.clientlibrary.ClientLibraryServiceImpl.java

/**
 * Writes resources css or js. Gets client libraries that should be combined and is written by their format adapter
 *
 * @param type CSS or JS//from   w  w w.  j a v a  2 s .  c o  m
 * @param output output
 * @throws IOException
 * @throws QuickFixException
 */
private void write(AuraContext context, ClientLibraryDef.Type type, Appendable output)
        throws IOException, QuickFixException {
    if (output == null) {
        throw new AuraRuntimeException("Output cannot be null");
    }

    if (context == null) {
        throw new NoContextException();
    }

    AuraContext.Mode mode = context.getMode();
    String uid = context.getUid(context.getApplicationDescriptor());

    String key = makeCacheKey(uid, mode, type);
    String code = getOutputCache().getIfPresent(key);

    if (code == null) {
        // no cache yet
        List<ClientLibraryDef> clientLibs = getClientLibraries(context, type);
        Set<Combinable> combinables = Sets.newLinkedHashSet();
        StringBuilder sb = new StringBuilder();

        for (ClientLibraryDef clientLib : clientLibs) {
            if (canCombine(clientLib)) {
                Combinable combinable = getCombinable(clientLib);
                if (combinable != null) {
                    combinables.add(combinable);
                }
            }
        }

        if (!combinables.isEmpty()) {
            // ClientLibraryCSSFormatAdapter or ClientLibraryJSFormatAdapter
            Aura.getSerializationService().writeCollection(combinables, Combinable.class, sb, type.toString());
        }

        code = sb.toString();
        getOutputCache().put(key, code);
    }

    output.append(code);
}

From source file:org.ofbiz.content.data.DataResourceWorker.java

public static void writeText(GenericValue dataResource, String textData, Map<String, Object> context,
        String targetMimeTypeId, Locale locale, Appendable out) throws GeneralException, IOException {
    String dataResourceMimeTypeId = dataResource.getString("mimeTypeId");
    Delegator delegator = dataResource.getDelegator();

    // assume HTML as data resource data
    if (UtilValidate.isEmpty(dataResourceMimeTypeId)) {
        dataResourceMimeTypeId = "text/html";
    }/*from  w ww  .j a v a  2  s.co  m*/

    // assume HTML for target
    if (UtilValidate.isEmpty(targetMimeTypeId)) {
        targetMimeTypeId = "text/html";
    }

    // we can only render text
    if (!targetMimeTypeId.startsWith("text")) {
        throw new GeneralException("Method writeText() only supports rendering text content : "
                + targetMimeTypeId + " is not supported");
    }

    if ("text/html".equals(targetMimeTypeId)) {
        // get the default mime type template
        GenericValue mimeTypeTemplate = EntityQuery.use(delegator).from("MimeTypeHtmlTemplate")
                .where("mimeTypeId", dataResourceMimeTypeId).cache().queryOne();

        if (mimeTypeTemplate != null && mimeTypeTemplate.get("templateLocation") != null) {
            // prepare the context
            Map<String, Object> mimeContext = FastMap.newInstance();
            mimeContext.putAll(context);
            mimeContext.put("dataResource", dataResource);
            mimeContext.put("textData", textData);

            String mimeString = DataResourceWorker.renderMimeTypeTemplate(mimeTypeTemplate, mimeContext);
            if (mimeString != null) {
                out.append(mimeString);
            }
        } else {
            if (textData != null) {
                out.append(textData);
            }
        }
    } else {
        out.append(textData);
    }
}

From source file:net.arnx.jsonic.JSON.java

/**
 * Format a object into a json string./*from  w ww. jav  a  2  s .  c o m*/
 * 
 * @param source a object to encode.
 * @param ap a destination. example: StringBuilder, Writer, ...
 * @return a json string
 */
public Appendable format(Object source, Appendable ap) throws IOException {
    Context context = new Context();

    OutputSource fs;
    if (ap instanceof Writer) {
        fs = new WriterOutputSource((Writer) ap);
    } else if (ap instanceof StringBuilder) {
        fs = new StringBuilderOutputSource((StringBuilder) ap);
    } else {
        fs = new AppendableOutputSource(ap);
    }

    context.enter('$', null);
    source = context.preformatInternal(source);

    if (context.isPrettyPrint() && context.getInitialIndent() > 0) {
        int indent = context.getInitialIndent();
        for (int j = 0; j < indent; j++) {
            ap.append(context.getIndentText());
        }
    }

    context.formatInternal(source, fs);
    context.exit();
    fs.flush();
    return ap;
}