@Override public String toString() { String result = null; if (m_file != null) { flushToFile(); try { result = Files.readFile(m_file); } catch (IOException e) { e.printStackTrace(); } } else { result = m_sb.toString(); } return result; }
@Override public FileStringBuffer append(CharSequence s) { if (s == null) { throw new IllegalArgumentException("CharSequence (Argument 0 of FileStringBuffer#append) should not be null"); } // m_sb.append(s); if (m_sb.length() > m_maxCharacters) { flushToFile(); } if (s.length() < MAX) { // Small string, add it to our internal buffer m_sb.append(s); } else { // Big string, add it to the temporary file directly flushToFile(); try (FileWriter writer = new FileWriter(m_file, true /* append */)) { copy(new StringReader(s.toString()), writer); } catch (IOException e) { e.printStackTrace(); } } return this; }
@Override public void toWriter(Writer fw) { if (fw == null) { throw new IllegalArgumentException("Writer (Argument 0 of FileStringBuffer#toWriter) should not be null"); } try { BufferedWriter bw = new BufferedWriter(fw); if (m_file == null) { bw.write(m_sb.toString()); bw.close(); } else { flushToFile(); try (FileReader reader = new FileReader(m_file)) { copy(reader, bw); } bw.flush(); } } catch(IOException ex) { ex.printStackTrace(); } }
@Override public FileStringBuffer append(CharSequence s) { if (s == null) { throw new IllegalArgumentException( "CharSequence (Argument 0 of FileStringBuffer#append) should not be null"); } // m_sb.append(s); if (m_sb.length() > m_maxCharacters) { flushToFile(); } if (s.length() < MAX) { // Small string, add it to our internal buffer m_sb.append(s); } else { // Big string, add it to the temporary file directly flushToFile(); try (FileWriter writer = new FileWriter(m_file, true /* append */)) { copy(new StringReader(s.toString()), writer); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } return this; }
@Override public String toString() { String result = null; if (m_file != null) { flushToFile(); try { result = Files.readFile(m_file); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } else { result = m_sb.toString(); } return result; } }
@Override public void toWriter(Writer fw) { if (fw == null) { throw new IllegalArgumentException( "Writer (Argument 0 of FileStringBuffer#toWriter) should not be null"); } try { BufferedWriter bw = new BufferedWriter(fw); if (m_file == null) { bw.write(m_sb.toString()); bw.close(); } else { flushToFile(); try (FileReader reader = new FileReader(m_file)) { copy(reader, bw); } bw.flush(); } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } }