Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-6314

ResourceGroovyMethods uses BufferedWriter/BufferedOutputStream

    XMLWordPrintableJSON

Details

    • Patch

    Description

      ResourceGroovyMethods uses BufferedWriter and BufferedOutputStream to write in-memory data to files. This is bad because it causes 8 K of chars or bytes to be created per-call.

      For example, this

      new File('keks') << 'ah'
      

      generates 16 KB of unnecessary waste.

      Affected methods:

      • write(File file, String text)
      • write(File file, String text, String charset)
      • append(File file, Object text)
      • append(File file, byte[] bytes)
      • append(File file, Object text, String charset)

      Solution:

          public static void append(File file, byte[] bytes) throws IOException {
              BufferedOutputStream stream = null;
              try {
                  stream = new BufferedOutputStream(new FileOutputStream(file, true));
                  stream.write(bytes, 0, bytes.length);
                  stream.flush();
      
                  OutputStream temp = stream;
                  stream = null;
                  temp.close();
              } finally {
                  closeWithWarning(stream);
              }
          }
      

      (Before)

          public static void append(File file, byte[] bytes) throws IOException {
              OutputStream stream = null;
              try {
                  stream = new FileOutputStream(file, true);
                  stream.write(bytes, 0, bytes.length);
                  stream.flush();
      
                  OutputStream temp = stream;
                  stream = null;
                  temp.close();
              } finally {
                  closeWithWarning(stream);
              }
          }
      

      (After)

      Attachments

        Activity

          People

            blackdrag Jochen Theodorou
            johann Johann
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: