A buffered writer that gobbles any \r characters and replaces every \n with a platform specific newline. In many places Groovy normalises streams to only have \n characters but when creating files that must be used by other platform-aware tools, you sometimes want the newlines to match what the plat
| 31 | * newlines to match what the platform expects. |
| 32 | */ |
| 33 | public class PlatformLineWriter extends Writer { |
| 34 | private final BufferedWriter writer; |
| 35 | |
| 36 | /** |
| 37 | * Creates a writer that normalizes line endings for the platform. |
| 38 | * |
| 39 | * @param out the underlying writer |
| 40 | */ |
| 41 | public PlatformLineWriter(Writer out) { |
| 42 | writer = new BufferedWriter(out); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Creates a writer that normalizes line endings for the platform. |
| 47 | * |
| 48 | * @param out the underlying writer |
| 49 | * @param sz the output buffer size |
| 50 | */ |
| 51 | public PlatformLineWriter(Writer out, int sz) { |
| 52 | writer = new BufferedWriter(out, sz); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Writes characters while converting line endings to the platform format. |
| 57 | * |
| 58 | * @param cbuf the source buffer |
| 59 | * @param off the buffer offset |
| 60 | * @param len the number of characters to write |
| 61 | * @throws IOException if an I/O error occurs |
| 62 | */ |
| 63 | @Override |
| 64 | public void write(char[] cbuf, int off, int len) throws IOException { |
| 65 | for (; len > 0; len--) { |
| 66 | char c = cbuf[off++]; |
| 67 | if (c == '\n') { |
| 68 | writer.newLine(); |
| 69 | } else if (c != '\r') { |
| 70 | writer.write(c); |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | /** {@inheritDoc} */ |
| 76 | @Override |
| 77 | public void flush() throws IOException { |
| 78 | writer.flush(); |
| 79 | } |
| 80 | |
| 81 | /** {@inheritDoc} */ |
| 82 | @Override |
| 83 | public void close() throws IOException { |
| 84 | writer.close(); |
| 85 | } |
| 86 | } |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…