List of usage examples for java.io CharArrayWriter CharArrayWriter
public CharArrayWriter(int initialSize)
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);/* w ww. j ava 2 s . co m*/ char[] result = charsWriter.toCharArray(); assertTrue(Arrays.equals(testbuffer, result)); assertEquals(0, charBuffer.charsAvailable()); assertEquals(0, charBuffer.size()); }
From source file:org.batoo.jpa.jdbc.AbstractColumn.java
private Object readLob(Object value) { try {//from w w w . ja v a 2 s .c o m if (value instanceof Clob) { final Clob clob = (Clob) value; if (this.javaType == String.class) { final StringWriter w = new StringWriter(); IOUtils.copy(clob.getAsciiStream(), w); value = w.toString(); } else { final CharArrayWriter w = new CharArrayWriter((int) clob.length()); IOUtils.copy(clob.getCharacterStream(), w); value = w.toCharArray(); } } else if (value instanceof byte[]) { if (this.javaType == String.class) { final StringWriter w = new StringWriter(); IOUtils.copy(new ByteArrayInputStream((byte[]) value), w); value = w.toString(); } else if (this.javaType == char[].class) { final byte[] byteArray = (byte[]) value; final char[] charArray = new char[byteArray.length]; for (int i = 0; i < charArray.length; i++) { charArray[i] = (char) byteArray[i]; } value = charArray; } else if (this.javaType != byte[].class) { final ObjectInputStream is = new ObjectInputStream(new ByteArrayInputStream((byte[]) value)); try { return is.readObject(); } finally { is.close(); } } } else if (value instanceof String) { return value; } else { final Blob blob = (Blob) value; if (this.javaType == byte[].class) { final ByteArrayOutputStream os = new ByteArrayOutputStream(); IOUtils.copy(blob.getBinaryStream(), os); value = os.toByteArray(); } else { final ObjectInputStream is = new ObjectInputStream(blob.getBinaryStream()); try { value = is.readObject(); } finally { is.close(); } } } return value; } catch (final Exception e) { throw new PersistenceException("Cannot read sql data", e); } }
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 w w w . j a va 2 s.c o m char[] result = charsOut.toCharArray(); assertTrue(Arrays.equals(testbuffer, result)); assertEquals(testbuffer.length, charBuffer.size()); }
From source file:org.codice.ddf.spatial.ogc.csw.catalog.transformer.CswQueryResponseTransformer.java
private String multiThreadedMarshal(List<Result> results, String recordSchema, final Map<String, Serializable> arguments) throws CatalogTransformerException { CompletionService<BinaryContent> completionService = new ExecutorCompletionService<>(queryExecutor); try {/*from w w w .j a va 2 s. com*/ for (Result result : results) { final Metacard mc = result.getMetacard(); final MetacardTransformer transformer = metacardTransformerManager .getTransformerBySchema(recordSchema); if (transformer == null) { throw new CatalogTransformerException("Cannot find transformer for schema: " + recordSchema); } // the "current" thread will run submitted task when queueSize exceeded; effectively // blocking enqueue of more tasks. completionService.submit(new Callable<BinaryContent>() { @Override public BinaryContent call() throws Exception { BinaryContent content = transformer.transform(mc, arguments); return content; } }); } int metacardCount = results.size(); CharArrayWriter accum = new CharArrayWriter(ACCUM_INITIAL_SIZE); for (int i = 0; i < metacardCount; i++) { Future<BinaryContent> binaryContentFuture = completionService.take(); // blocks BinaryContent binaryContent = binaryContentFuture.get(); IOUtils.copy(binaryContent.getInputStream(), accum); } return accum.toString(); } catch (IOException | InterruptedException | ExecutionException xe) { throw new CatalogTransformerException(xe); } }
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 w w w . j a va 2 s. c om*/ 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);//from w w w . j a v a2 s. com char[] result = charsWriter.toCharArray(); assertTrue(Arrays.equals(testbuffer, result)); assertEquals(testbuffer.length, charBuffer.size()); }
From source file:net.sf.jasperreports.engine.JRResultSetDataSource.java
protected CharArrayReader getArrayReader(Reader reader, long size) throws IOException { char[] buf = new char[8192]; CharArrayWriter bufWriter = new CharArrayWriter((size > 0) ? (int) size : 8192); BufferedReader bufReader = new BufferedReader(reader, 8192); for (int read = bufReader.read(buf); read > 0; read = bufReader.read(buf)) { bufWriter.write(buf, 0, read);/*from ww w. ja va 2s .c o m*/ } bufWriter.flush(); return new CharArrayReader(bufWriter.toCharArray()); }
From source file:jp.aegif.alfresco.online_webdav.WebDAVMethod.java
/** * Create an XML writer for the response * // ww w . j ava2 s .c om * @return XMLWriter * @exception IOException */ protected final XMLWriter createXMLWriter() throws IOException { // Buffer the XML response, in case we have to reset mid-transaction m_xmlWriter = new CharArrayWriter(1024); return new XMLWriter(m_xmlWriter, getXMLOutputFormat()); }
From source file:com.netscape.cms.logging.LogFile.java
/** * This method actually does the logging, and is not overridden * by subclasses, so you can call it and know that it will do exactly * what you see below./* ww w. j av a 2 s . c om*/ */ private synchronized void doLog(ILogEvent event, boolean noFlush) throws ELogException { String entry = logEvt2String(event); if (mLogWriter == null) { String[] params = { mFileName, entry }; if (mLogSigning) { ConsoleError.send(new SystemEvent(CMS.getUserMessage("CMS_LOG_LOGFILE_CLOSED", params))); // Failed to write to audit log, shut down CMS shutdownCMS(); } throw new ELogException(CMS.getUserMessage("CMS_LOG_LOGFILE_CLOSED", params)); } else { try { mLogWriter.write(entry, 0/*offset*/, entry.length()); if (mLogSigning == true) { if (mSignature != null) { // include newline for calculating MAC mSignature.update(entry.getBytes("UTF-8")); } else { CMS.debug("LogFile: mSignature is not yet ready... null in log()"); } } if (mTrace) { CharArrayWriter cw = new CharArrayWriter(200); PrintWriter pw = new PrintWriter(cw); Exception e = new Exception(); e.printStackTrace(pw); char[] c = cw.toCharArray(); cw.close(); pw.close(); CharArrayReader cr = new CharArrayReader(c); LineNumberReader lr = new LineNumberReader(cr); String text = null; String method = null; String fileAndLine = null; if (lr.ready()) { text = lr.readLine(); do { text = lr.readLine(); } while (text.indexOf("logging") != -1); int p = text.indexOf("("); fileAndLine = text.substring(p); String classandmethod = text.substring(0, p); int q = classandmethod.lastIndexOf("."); method = classandmethod.substring(q + 1); mLogWriter.write(fileAndLine, 0/*offset*/, fileAndLine.length()); mLogWriter.write(" ", 0/*offset*/, " ".length()); mLogWriter.write(method, 0/*offset*/, method.length()); } } mLogWriter.newLine(); if (mLogSigning == true) { if (mSignature != null) { mSignature.update(LINE_SEP_BYTE); } else { CMS.debug("LogFile: mSignature is null in log() 2"); } } } catch (IOException e) { ConsoleError.send(new SystemEvent( CMS.getUserMessage("CMS_LOG_WRITE_FAILED", mFileName, entry, e.toString()))); if (mLogSigning) { // Failed to write to audit log, shut down CMS e.printStackTrace(); shutdownCMS(); } } catch (IllegalStateException e) { CMS.debug("LogFile: exception thrown in log(): " + e.toString()); ConsoleError .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, e.toString()))); } catch (GeneralSecurityException gse) { // DJN: handle error CMS.debug("LogFile: exception thrown in log(): " + gse.toString()); gse.printStackTrace(); ConsoleError .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, gse.toString()))); } catch (Exception ee) { // Make darn sure we got everything ConsoleError .send(new SignedAuditEvent(CMS.getLogMessage(LOG_SIGNED_AUDIT_EXCEPTION, ee.toString()))); if (mLogSigning) { // Failed to write to audit log, shut down CMS ee.printStackTrace(); shutdownCMS(); } } // XXX // Although length will be in Unicode dual-bytes, the PrintWriter // will only print out 1 byte per character. I suppose this could // be dependent on the encoding of your log file, but it ain't that // smart yet. Also, add one for the newline. (hmm, on NT, CR+LF) int nBytes = entry.length() + 1; mBytesWritten += nBytes; mBytesUnflushed += nBytes; if (mBufferSize > 0 && mBytesUnflushed > mBufferSize && !noFlush) { flush(); } } }
From source file:org.apache.jmeter.save.CSVSaveService.java
/** * Reads from file and splits input into strings according to the delimiter, * taking note of quoted strings./*from www. j a va 2s . c o m*/ * <p> * Handles DOS (CRLF), Unix (LF), and Mac (CR) line-endings equally. * <p> * A blank line - or a quoted blank line - both return an array containing * a single empty String. * @param infile * input file - must support mark(1) * @param delim * delimiter (e.g. comma) * @return array of strings, will be empty if there is no data, i.e. if the input is at EOF. * @throws IOException * also for unexpected quote characters */ public static String[] csvReadFile(BufferedReader infile, char delim) throws IOException { int ch; ParserState state = ParserState.INITIAL; List<String> list = new ArrayList<>(); CharArrayWriter baos = new CharArrayWriter(200); boolean push = false; while (-1 != (ch = infile.read())) { push = false; switch (state) { case INITIAL: if (ch == QUOTING_CHAR) { state = ParserState.QUOTED; } else if (isDelimOrEOL(delim, ch)) { push = true; } else { baos.write(ch); state = ParserState.PLAIN; } break; case PLAIN: if (ch == QUOTING_CHAR) { baos.write(ch); throw new IOException("Cannot have quote-char in plain field:[" + baos.toString() + "]"); } else if (isDelimOrEOL(delim, ch)) { push = true; state = ParserState.INITIAL; } else { baos.write(ch); } break; case QUOTED: if (ch == QUOTING_CHAR) { state = ParserState.EMBEDDEDQUOTE; } else { baos.write(ch); } break; case EMBEDDEDQUOTE: if (ch == QUOTING_CHAR) { baos.write(QUOTING_CHAR); // doubled quote => quote state = ParserState.QUOTED; } else if (isDelimOrEOL(delim, ch)) { push = true; state = ParserState.INITIAL; } else { baos.write(QUOTING_CHAR); throw new IOException( "Cannot have single quote-char in quoted field:[" + baos.toString() + "]"); } break; default: throw new IllegalStateException("Unexpected state " + state); } // switch(state) if (push) { if (ch == '\r') {// Remove following \n if present infile.mark(1); if (infile.read() != '\n') { infile.reset(); // did not find \n, put the character // back } } String s = baos.toString(); list.add(s); baos.reset(); } if ((ch == '\n' || ch == '\r') && state != ParserState.QUOTED) { break; } } // while not EOF if (ch == -1) {// EOF (or end of string) so collect any remaining data if (state == ParserState.QUOTED) { throw new IOException("Missing trailing quote-char in quoted field:[\"" + baos.toString() + "]"); } // Do we have some data, or a trailing empty field? if (baos.size() > 0 // we have some data || push // we've started a field || state == ParserState.EMBEDDEDQUOTE // Just seen "" ) { list.add(baos.toString()); } } return list.toArray(new String[list.size()]); }