public static IBuffer create() { return new FileStringBuffer(); } }
@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; }
private void flushToFile() { if (m_sb.length() == 0) return; if (m_file == null) { try { m_file = File.createTempFile("testng", "fileStringBuffer"); m_file.deleteOnExit(); p("Created temp file " + m_file); } catch (IOException e) { e.printStackTrace(); } } p("Size " + m_sb.length() + ", flushing to " + m_file); try (FileWriter fw = new FileWriter(m_file, true /* append */)) { fw.append(m_sb); } catch (IOException e) { e.printStackTrace(); } m_sb = new StringBuilder(); }
public static void main(String[] args) throws IOException { String s = "abcdefghijklmnopqrstuvwxyz"; FileStringBuffer fsb = new FileStringBuffer(10); StringBuilder control = new StringBuilder(); Random r = new Random(); for (int i = 0; i < 1000; i++) { int start = Math.abs(r.nextInt() % 26); int length = Math.abs(r.nextInt() % (26 - start)); String fragment = s.substring(start, start + length); p("... Appending " + fragment); fsb.append(fragment); control.append(fragment); } File expected = new File("/tmp/expected"); expected.delete(); try (FileWriter expectedWriter = new FileWriter(expected)) { expectedWriter.append(control); } File actual = new File("/tmp/actual"); actual.delete(); try (FileWriter actualWriter = new FileWriter(actual)) { fsb.toWriter(actualWriter); } // Assert.assertEquals(fsb.toString(), control.toString()); }
@Test public void basic() { { FileStringBuffer fsb = new FileStringBuffer(5); String s = "0123456789"; String s3 = s + s + s; fsb.append(s3); // Assert.assertEquals(s3, fsb.toString()); } { FileStringBuffer fsb = new FileStringBuffer(5); String s = "0123456789"; String s3 = s + s + s; fsb.append(s); fsb.append(s); fsb.append(s); // Assert.assertEquals(s3, fsb.toString()); } } }
@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 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 ex) { ex.printStackTrace(); } }
public static IBuffer create() { return new FileStringBuffer(); } }
private void flushToFile() { if (m_sb.length() == 0) return; if (m_file == null) { try { m_file = File.createTempFile("testng", "fileStringBuffer"); m_file.deleteOnExit(); p("Created temp file " + m_file); } catch (IOException e) { LOGGER.error(e.getMessage(), e); } } p("Size " + m_sb.length() + ", flushing to " + m_file); try (FileWriter fw = new FileWriter(m_file, true /* append */)) { fw.append(m_sb); } catch (IOException e) { e.printStackTrace(); } m_sb = new StringBuilder(); }
@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 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); } }