List of usage examples for java.io CharArrayReader CharArrayReader
public CharArrayReader(char buf[])
From source file:org.theospi.portfolio.portal.web.XsltPortal.java
private Element createSitesTabArea(Session session, String siteId, Document doc, HttpServletRequest req) throws IOException, SAXException { Element siteTabs = doc.createElement("siteTabs"); CharArrayWriter writer = new CharArrayWriter(); PrintWriter printWriter = new PrintWriter(writer); printWriter.write("<div id=\"blank\">"); includeTabs(printWriter, req, session, siteId, "site", false); Document tabs = getDocumentBuilder().parse(new InputSource(new CharArrayReader(writer.toCharArray()))); siteTabs.appendChild(doc.importNode(tabs.getDocumentElement(), true)); return siteTabs; }
From source file:org.mule.util.ClassUtils.java
/** * Provide a simple-to-understand class name (with access to only Java 1.4 API). * * @param clazz The class whose name we will generate * @return A readable name for the class *///w w w. java 2 s . c o m public static String getSimpleName(Class clazz) { if (null == clazz) { return "null"; } else { return classNameHelper(new BufferedReader(new CharArrayReader(clazz.getName().toCharArray()))); } }
From source file:org.openmrs.api.ObsServiceTest.java
/** * @see ObsService#saveObs(Obs,String)//from w w w. j a va2 s. co m */ @Test public void saveObs_shouldCreateNewFileFromComplexDataForNewObs() { executeDataSet(COMPLEX_OBS_XML); ObsService os = Context.getObsService(); ConceptService cs = Context.getConceptService(); AdministrationService as = Context.getAdministrationService(); // make sure the file isn't there to begin with File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory( as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR)); File createdFile = new File(complexObsDir, "nameOfFile.txt"); if (createdFile.exists()) createdFile.delete(); // the complex data to put onto an obs that will be saved Reader input = new CharArrayReader("This is a string to save to a file".toCharArray()); ComplexData complexData = new ComplexData("nameOfFile.txt", input); // must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked // this is a concept mapped to the text handler Concept questionConcept = cs.getConcept(8474); Obs obsToSave = new Obs(new Person(1), questionConcept, new Date(), new Location(1)); obsToSave.setComplexData(complexData); try { os.saveObs(obsToSave, null); // make sure the file appears now after the save Assert.assertTrue(createdFile.exists()); } finally { // we always have to delete this inside the same unit test because it is outside the // database and hence can't be "rolled back" like everything else createdFile.delete(); } }
From source file:org.openmrs.api.ObsServiceTest.java
/** * @throws IOException/* w ww. ja va 2s .c o m*/ * @see ObsService#saveObs(Obs,String) */ @Test public void saveObs_shouldNotOverwriteFileWhenUpdatingAComplexObs() throws IOException { executeDataSet(COMPLEX_OBS_XML); ObsService os = Context.getObsService(); ConceptService cs = Context.getConceptService(); AdministrationService as = Context.getAdministrationService(); // Create the file that was supposedly put there by another obs File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory( as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR)); File previouslyCreatedFile = new File(complexObsDir, "nameOfFile.txt"); FileUtils.writeByteArrayToFile(previouslyCreatedFile, "a string to save to a file".getBytes()); // the file we'll be creating...defining it here so we can delete it in a finally block File newComplexFile = null; try { long oldFileSize = previouslyCreatedFile.length(); // now add a new file to this obs and update it // ...then make sure the original file is still there // the complex data to put onto an obs that will be saved Reader input2 = new CharArrayReader("diff string to save to a file with the same name".toCharArray()); ComplexData complexData = new ComplexData("nameOfFile.txt", input2); // must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked // this is a concept mapped to the text handler Concept questionConcept = cs.getConcept(8474); Obs obsToSave = new Obs(new Person(1), questionConcept, new Date(), new Location(1)); obsToSave.setComplexData(complexData); os.saveObs(obsToSave, null); // make sure the old file still appears now after the save Assert.assertEquals(oldFileSize, previouslyCreatedFile.length()); String valueComplex = obsToSave.getValueComplex(); String filename = valueComplex.substring(valueComplex.indexOf("|") + 1).trim(); newComplexFile = new File(complexObsDir, filename); // make sure the file appears now after the save Assert.assertTrue(newComplexFile.length() > oldFileSize); } finally { // clean up the files we created newComplexFile.delete(); try { previouslyCreatedFile.delete(); } catch (Exception e) { // pass } } }
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.// w w w .j a v a 2 s .c o m */ 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.openmrs.api.ObsServiceTest.java
/** * @see ObsService#saveObs(Obs,String)/*w w w . j a v a2s . c o m*/ */ @Test public void saveObs_shouldDeleteThePreviousFileWhenAComplexObservationIsUpdatedWithANewComplexValue() { String changeMessage = "Testing TRUNK-4538"; executeDataSet(COMPLEX_OBS_XML); ObsService os = Context.getObsService(); ConceptService cs = Context.getConceptService(); AdministrationService as = Context.getAdministrationService(); // make sure the file isn't there to begin with File complexObsDir = OpenmrsUtil.getDirectoryInApplicationDataDirectory( as.getGlobalProperty(OpenmrsConstants.GLOBAL_PROPERTY_COMPLEX_OBS_DIR)); final File createdFile = new File(complexObsDir, "nameOfFile.txt"); if (createdFile.exists()) createdFile.delete(); // the complex data to put onto an obs that will be saved Reader input = new CharArrayReader("This is a string to save to a file".toCharArray()); ComplexData complexData = new ComplexData("nameOfFile.txt", input); // must fetch the concept instead of just new Concept(8473) because the attributes on concept are checked // this is a concept mapped to the text handler Concept questionConcept = cs.getConcept(8474); Obs obsToSave = new Obs(new Person(1), questionConcept, new Date(), new Location(1)); obsToSave.setComplexData(complexData); os.saveObs(obsToSave, null); File updatedFile = new File(complexObsDir, "nameOfUpdatedFile.txt"); if (updatedFile.exists()) updatedFile.delete(); // the complex data to put onto an obs that will be updated Reader updatedInput = new CharArrayReader( "This is a string to save to a file which uploaded to update an obs".toCharArray()); ComplexData updatedComplexData = new ComplexData("nameOfUpdatedFile.txt", updatedInput); obsToSave.setComplexData(updatedComplexData); try { os.saveObs(obsToSave, changeMessage); Assert.assertFalse(createdFile.exists()); } finally { // we always have to delete this inside the same unit test because it is outside the // database and hence can't be "rolled back" like everything else updatedFile.delete(); } }
From source file:org.apache.openjpa.jdbc.sql.DBDictionary.java
public void insertClobForStreamingLoad(Row row, Column col, Object ob) throws SQLException { if (ob != null) { row.setCharacterStream(col, new CharArrayReader(new char[0]), 0); } else {/*from w ww. jav a 2s .co m*/ row.setNull(col); } }