Details
-
Bug
-
Status: Closed
-
Minor
-
Resolution: Fixed
-
2.1.4
-
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)