Example usage for java.io CharArrayWriter toCharArray

List of usage examples for java.io CharArrayWriter toCharArray

Introduction

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

Prototype

public char[] toCharArray() 

Source Link

Document

Returns a copy of the input data.

Usage

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

public void testToReader() throws IOException {
    StreamCharBuffer charBuffer = createTestInstance();
    Reader input = charBuffer.getReader();
    CharArrayWriter charsOut = new CharArrayWriter(charBuffer.calculateTotalCharsUnread());
    copy(input, charsOut, 2048);/*from ww w .ja v a  2s.c  o m*/
    char[] result = charsOut.toCharArray();
    assertTrue(Arrays.equals(testbuffer, result));
    assertEquals(0, charBuffer.charsAvailable());
    assertEquals(0, charBuffer.size());
}

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

public void testToReaderOneByOne() throws IOException {
    StreamCharBuffer charBuffer = createTestInstance();
    Reader input = charBuffer.getReader();
    CharArrayWriter charsOut = new CharArrayWriter(charBuffer.calculateTotalCharsUnread());
    copyOneByOne(input, charsOut);//from  w w  w.jav  a2 s  .co  m
    char[] result = charsOut.toCharArray();
    assertTrue(Arrays.equals(testbuffer, result));
    assertEquals(0, charBuffer.charsAvailable());
    assertEquals(0, charBuffer.size());
}

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

public void testWriteTo() throws IOException {
    StreamCharBuffer charBuffer = createTestInstance();
    CharArrayWriter charsWriter = new CharArrayWriter(charBuffer.calculateTotalCharsUnread());
    charBuffer.writeTo(charsWriter);//from  ww  w  .j a  v  a 2 s  .  c  o  m
    char[] result = charsWriter.toCharArray();
    assertTrue(Arrays.equals(testbuffer, result));
    assertEquals(0, charBuffer.charsAvailable());
    assertEquals(0, charBuffer.size());
}

From source file:org.apache.jasper.compiler.JspUtil.java

public static char[] removeQuotes(char[] chars) {
    CharArrayWriter caw = new CharArrayWriter();
    for (int i = 0; i < chars.length; i++) {
        if (chars[i] == '%' && chars[i + 1] == '\\' && chars[i + 2] == '>') {
            caw.write('%');
            caw.write('>');
            i = i + 2;//from  w ww.  java2 s  .  c o m
        } else {
            caw.write(chars[i]);
        }
    }
    return caw.toCharArray();
}

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

public void testToReader() throws IOException {
    StreamCharBuffer charBuffer = createTestInstance();
    Reader input = charBuffer.getReader();
    CharArrayWriter charsOut = new CharArrayWriter(charBuffer.size());
    copy(input, charsOut, 2048);/*from  www . jav a  2 s  . co m*/
    char[] result = charsOut.toCharArray();
    assertTrue(Arrays.equals(testbuffer, result));

    assertEquals(testbuffer.length, charBuffer.size());
}

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

public void testToReaderOneByOne() throws IOException {
    StreamCharBuffer charBuffer = createTestInstance();
    Reader input = charBuffer.getReader();
    CharArrayWriter charsOut = new CharArrayWriter(charBuffer.size());
    copyOneByOne(input, charsOut);//from ww  w  . j  a  v a2 s . co  m
    char[] result = charsOut.toCharArray();
    assertTrue(Arrays.equals(testbuffer, result));

    assertEquals(testbuffer.length, charBuffer.size());
}

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

public void testWriteTo() throws IOException {
    StreamCharBuffer charBuffer = createTestInstance();
    CharArrayWriter charsWriter = new CharArrayWriter(charBuffer.size());
    charBuffer.writeTo(charsWriter);/* www  . j av  a2s .  c  om*/
    char[] result = charsWriter.toCharArray();
    assertTrue(Arrays.equals(testbuffer, result));

    assertEquals(testbuffer.length, charBuffer.size());
}

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

private void createDocumentWithPdf(String name, String lineSeparator) 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");

    {//from   w  w w . j  av a2  s  .co m
        resource.setProperty("jcr:encoding", "UTF-8");
        resource.setProperty("jcr:mimeType", "application/pdf");
        InputStream pdf = this.getClass().getResourceAsStream(WORDS_ON_NEW_LINE_WITHOUT_SPACES);
        resource.setProperty("jcr:data", new BinaryImpl(new BufferedInputStream(pdf)));
        resource.setProperty("jcr:lastModified", Calendar.getInstance());
    }
    {
        InputStream pdf = this.getClass().getResourceAsStream(WORDS_ON_NEW_LINE_WITHOUT_SPACES);
        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();
                if (lineSeparator != null) {
                    stripper.setLineSeparator(lineSeparator);
                }
                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.getSession().save();
}

From source file:com.silverpeas.jcrutil.model.impl.AbstractJcrTestCase.java

protected String readFile(String path) throws IOException {
    CharArrayWriter writer = null;
    InputStream in = null;/*w  w  w .  j a v  a  2 s.  c  o m*/
    Reader reader = null;
    try {
        in = new FileInputStream(path);
        writer = new CharArrayWriter();
        reader = new InputStreamReader(in);
        char[] buffer = new char[8];
        int c = 0;
        while ((c = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, c);
        }
        return new String(writer.toCharArray());
    } catch (IOException ioex) {
        return null;
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (in != null) {
            in.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}

From source file:com.silverpeas.jcrutil.model.impl.AbstractJcrTestCase.java

protected String readFileFromNode(Node fileNode)
        throws IOException, ValueFormatException, PathNotFoundException, RepositoryException {
    CharArrayWriter writer = null;
    InputStream in = null;//  w  w  w  . ja v  a2s . c om
    Reader reader = null;
    try {
        in = fileNode.getNode(JcrConstants.JCR_CONTENT).getProperty(JcrConstants.JCR_DATA).getStream();
        writer = new CharArrayWriter();
        reader = new InputStreamReader(in);
        char[] buffer = new char[8];
        int c = 0;
        while ((c = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, c);
        }
        return new String(writer.toCharArray());
    } catch (IOException ioex) {
        return null;
    } finally {
        if (reader != null) {
            reader.close();
        }
        if (in != null) {
            in.close();
        }
        if (writer != null) {
            writer.close();
        }
    }
}