Example usage for java.io CharArrayWriter CharArrayWriter

List of usage examples for java.io CharArrayWriter CharArrayWriter

Introduction

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

Prototype

public CharArrayWriter() 

Source Link

Document

Creates a new CharArrayWriter.

Usage

From source file:importer.filters.CceFilter.java

/**
 * Convert all the files in a directory 
 * @param input the raw text input string
 * @param name the name of the new version
 * @param cortext a cortext mvd archive//from  ww w .ja  va 2 s. c o  m
 * @param corcode a corcode mvd archive
 * @return the log
 */
public String convert(String input, String name, Archive cortex, Archive corcode) throws ImporterException {
    try {
        init();
        CharArrayWriter txt = new CharArrayWriter();
        String lastWord = "";
        String firstWord = "";
        String lineEnd = findLineEnding(input);
        String[] lines = input.split(lineEnd);
        paraSeen = true;
        for (int i = 0; i < lines.length; i++) {
            String str = lines[i].trim();
            firstWord = getFirstWord(str);
            if (str.startsWith(".") && str.length() > 1 && Character.isLetter(str.charAt(1))) {
                convertDotCommand(str, txt);
                if (!lastEndsInHyphen && written > 0)
                    writeCurrent(txt, SPACE);
                // don't reset lastWord
                continue;
            } else if (lines[i].startsWith("   ")) {
                Range r;
                if (!paraSeen)
                    paraSeen = true;
                else if (written > paraStart) {
                    // write previous para range
                    r = new Range("p", paraStart, written - paraStart);
                    markup.add(r);
                }
                if (written > 0)
                    writeCurrent(txt, CR);
                paraStart = written;
                // markup new paragraphs with 4 spaces for readability
                r = new Range(CSSStyles.PARA_START, written, 4);
                markup.add(r);
                writeLineContents("    " + str, txt);
            } else {
                if (lastEndsInHyphen) {
                    Range r;
                    if (isHardHyphen(lastWord, firstWord)) {
                        r = new Range(CSSStyles.STRONG, written - 1, 1);
                        markup.add(r);
                    } else {
                        r = new Range(CSSStyles.WEAK, written - 1, 1);
                        markup.add(r);
                    }
                    writeCurrent(txt, CR);
                    r = new Range(CSSStyles.HYPHEN_CR, written - 1, 1);
                    markup.add(r);
                } else if (written > 0) {
                    writeCurrent(txt, CR);
                    if (written == paraStart + 1)
                        paraStart = written;
                }
                writeLineContents(str, txt);
            }
            if (!lineCommands.isEmpty()) {
                for (int j = lineCommands.size() - 1; j >= 0; j--) {
                    Range r = new Range(lineCommands.get(j), paraStart, written - paraStart);
                    markup.add(r);
                }
                lineCommands.clear();
                paraStart = written;
            }
            lastWord = getLastWord(str);
        }
        // write closing para range
        if (written > paraStart) {
            Range r = new Range("p", paraStart, written - paraStart);
            markup.add(r);
        }
        markup.sort();
        char[] chars = txt.toCharArray();
        cortex.put(name, chars);
        String json = markup.toSTILDocument().toString();
        corcode.put(name, json.toCharArray());
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
    return "";
}

From source file:IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a character array
 * using the specified character encoding.
 * <p>//  w w w  .  ja v  a2s .c  o  m
 * Character encoding names can be found at
 * <a href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * 
 * @param is  the <code>InputStream</code> to read from
 * @param encoding  the encoding to use, null means platform default
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(InputStream is, String encoding) throws IOException {
    CharArrayWriter output = new CharArrayWriter();
    copy(is, output, encoding);
    return output.toCharArray();
}

From source file:com.aoyetech.fee.commons.utils.IOUtils.java

/**
 * Get the contents of an <code>InputStream</code> as a character array
 * using the specified character encoding.
 * <p>//from  w  w  w .  j  a  v a  2 s  .  c  om
 * Character encoding names can be found at <a
 * href="http://www.iana.org/assignments/character-sets">IANA</a>.
 * <p>
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedInputStream</code>.
 * 
 * @param is the <code>InputStream</code> to read from
 * @param encoding the encoding to use, null means platform default
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(final InputStream is, final String encoding) throws IOException {
    final CharArrayWriter output = new CharArrayWriter();
    copy(is, output, encoding);
    return output.toCharArray();
}

From source file:org.hippoecm.repository.PdfExtractionAndIndexingTest.java

private void createDocumentWithPdf(String name, boolean includeHippoText) throws Exception {

    Node handle = testPath.addNode(name, HippoNodeType.NT_HANDLE);
    Node document = handle.addNode(name, NT_SEARCHDOCUMENT);

    Node compound = document.addNode("substructure", NT_COMPOUNDSTRUCTURE);
    Node resource = compound.addNode("hippo:testresource", "hippo:resource");

    {/*  w w w.  j  a  v a2 s. c  om*/
        resource.setProperty("jcr:encoding", "UTF-8");
        resource.setProperty("jcr:mimeType", "text/plain");
        ByteArrayOutputStream data = new ByteArrayOutputStream();
        OutputStreamWriter writer = new OutputStreamWriter(data, "UTF-8");
        writer.write("The quick brown fox jumps over the lazy " + UNIQUE_WORD_IN_PLAIN_TEXT);
        writer.close();
        resource.setProperty("jcr:data", new BinaryImpl(new ByteArrayInputStream(data.toByteArray())));
        resource.setProperty("jcr:lastModified", Calendar.getInstance());
    }

    if (includeHippoText) {
        InputStream pdf = this.getClass().getResourceAsStream(UNITTEST_PDF_FILE_NAME);
        try {
            PDFParser parser = new PDFParser(new BufferedInputStream(pdf));
            PDDocument pdDocument = null;
            try {
                parser.parse();
                pdDocument = parser.getPDDocument();
                CharArrayWriter writer = new CharArrayWriter();

                PDFTextStripper stripper = new PDFTextStripper();
                stripper.setLineSeparator("\n");
                stripper.writeText(pdDocument, writer);

                StringBuilder extracted = new StringBuilder();
                extracted.append(writer.toCharArray());
                // make sure to store it as UTF-8
                InputStream extractedStream = IOUtils.toInputStream(extracted.toString(), "UTF-8");
                resource.setProperty("hippo:text",
                        resource.getSession().getValueFactory().createBinary(extractedStream));
            } finally {
                try {
                    if (pdDocument != null) {
                        pdDocument.close();
                    }
                } catch (IOException e) {
                    // ignore
                }
            }
        } catch (Exception e) {
            // it may happen that PDFParser throws a runtime
            // exception when parsing certain pdf documents

            // we set empty text:
            final ByteArrayInputStream emptyByteArrayInputStream = new ByteArrayInputStream(new byte[0]);
            resource.setProperty("hippo:text",
                    resource.getSession().getValueFactory().createBinary(emptyByteArrayInputStream));

        } finally {
            pdf.close();
        }
    }
    testPath.save();
}

From source file:org.roda.core.common.RodaUtils.java

public static Reader applyMetadataStylesheet(Binary binary, String basePath, String metadataType,
        String metadataVersion, Map<String, String> parameters) throws GenericException {
    try (Reader descMetadataReader = new InputStreamReader(
            new BOMInputStream(binary.getContent().createInputStream()))) {

        XMLReader xmlReader = XMLReaderFactory.createXMLReader();
        xmlReader.setEntityResolver(new RodaEntityResolver());
        InputSource source = new InputSource(descMetadataReader);
        Source text = new SAXSource(xmlReader, source);

        XsltExecutable xsltExecutable = CACHE.get(Triple.of(basePath, metadataType, metadataVersion));

        XsltTransformer transformer = xsltExecutable.load();
        CharArrayWriter transformerResult = new CharArrayWriter();

        transformer.setSource(text);/*  ww  w  .ja v  a  2s . co m*/
        transformer.setDestination(PROCESSOR.newSerializer(transformerResult));

        for (Entry<String, String> parameter : parameters.entrySet()) {
            QName qName = new QName(parameter.getKey());
            XdmValue xdmValue = new XdmAtomicValue(parameter.getValue());
            transformer.setParameter(qName, xdmValue);
        }

        transformer.transform();

        return new CharArrayReader(transformerResult.toCharArray());

    } catch (IOException | SAXException | ExecutionException | SaxonApiException e) {
        throw new GenericException("Could not process descriptive metadata binary " + binary.getStoragePath()
                + " metadata type " + metadataType + " and version " + metadataVersion, e);
    }
}

From source file:Base64.java

private static char[] readChars(File file) {
    CharArrayWriter caw = new CharArrayWriter();
    try {/*from w  w w. java2s  .co  m*/
        Reader fr = new FileReader(file);
        Reader in = new BufferedReader(fr);
        int count = 0;
        char[] buf = new char[16384];
        while ((count = in.read(buf)) != -1) {
            if (count > 0)
                caw.write(buf, 0, count);
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return caw.toCharArray();
}

From source file:Main.java

/**
 *
 * @param document//from  www.  ja v a  2  s  .  c o m
 * @return
 * @throws TransformerException
 * @throws UnsupportedEncodingException
 */
private static String write(Document document) throws TransformerException, UnsupportedEncodingException {

    TransformerFactory factory = TransformerFactory.newInstance();
    Transformer transformer = factory.newTransformer();
    Source source = new DOMSource(document);
    CharArrayWriter os = new CharArrayWriter();
    Result result = new StreamResult(os);
    transformer.transform(source, result);

    return os.toString();
}

From source file:IOUtils.java

/**
 * Get the contents of a <code>Reader</code> as a character array.
 * <p>//from  w  ww  .j  a va2 s .c o m
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * 
 * @param input  the <code>Reader</code> to read from
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(Reader input) throws IOException {
    CharArrayWriter sw = new CharArrayWriter();
    copy(input, sw);
    return sw.toCharArray();
}

From source file:Base64.java

private static char[] readChars(final File file) {
    final CharArrayWriter caw = new CharArrayWriter();
    try {//from w ww  . ja  v  a2s. com
        final Reader fr = new FileReader(file);
        final Reader in = new BufferedReader(fr);
        int count;
        final char[] buf = new char[16384];
        while ((count = in.read(buf)) != -1) {
            if (count > 0) {
                caw.write(buf, 0, count);
            }
        }
        in.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return caw.toCharArray();
}

From source file:com.aoyetech.fee.commons.utils.IOUtils.java

/**
 * Get the contents of a <code>Reader</code> as a character array.
 * <p>/* w  ww.  j av  a 2  s.  c o m*/
 * This method buffers the input internally, so there is no need to use a
 * <code>BufferedReader</code>.
 * 
 * @param input the <code>Reader</code> to read from
 * @return the requested character array
 * @throws NullPointerException if the input is null
 * @throws IOException if an I/O error occurs
 * @since Commons IO 1.1
 */
public static char[] toCharArray(final Reader input) throws IOException {
    final CharArrayWriter sw = new CharArrayWriter();
    copy(input, sw);
    return sw.toCharArray();
}