} // getTempDir() public static StreamWriter getWriter(NOutputStream file, string encoding, string append) // throws IllegalStateException { bool append_to_bool = append.Equals("true", StringComparison.CurrentCultureIgnoreCase); bool insertBom = !append_to_bool; return(getWriter(file, encoding, append_to_bool, insertBom)); } // getWriter()
} // getWriter() public static StreamWriter getWriter(NOutputStream stream_arg, string encoding, bool append, bool insertBom) // throws IllegalStateException { if (append && insertBom) { throw new ArgumentException("Can not insert BOM into appending writer"); } NOutputStream output_stream = getOutputStream(stream_arg, append); return(getWriter(output_stream, encoding, insertBom.ToString())); } // getWriter()
} // getOutputStream() public static NOutputStream getOutputStream(NOutputStream stream_arg, bool append) { try { return(new NOutputStream(stream_arg.SafeFileHandle, FileAccess.Write)); } catch (FileNotFoundException e) { throw new InvalidOperationException(e.Message); } } // getOutputStream()
} // getWriter() public static StreamWriter getWriter(NOutputStream output_stream, string encoding_arg, bool insertBom) // throws IllegalStateException { //if (!((Stream)output_stream is BufferedStream)) //{ // output_stream = new BufferedStream(output_stream); //} try { if (insertBom) { StreamWriter writer = null; // new UnicodeWriter(output_stream, encoding_arg); return(writer); } else { Encoding encoding = null; switch (encoding_arg.ToLower()) { case "ascii": encoding = Encoding.ASCII; break; case "utf-8": encoding = Encoding.UTF8; break; case "utf-32": encoding = Encoding.UTF32; break; default: break; } StreamWriter writer = null; if (encoding != null) { writer = new StreamWriter(output_stream, encoding); } else { throw new IOException("Encoding " + encoding_arg + " not available"); } return(writer); } } catch (Exception e) { throw new InvalidOperationException(e.Message); } } // getWriter()
} // getBufferedReader public static void copy(NInputStream from_stream, NOutputStream to_stream) // throws IllegalStateException { try { byte[] buffer = new byte[1024 * 32]; int offset = 0; for (int value = from_stream.Read(buffer, offset++, 1); value != -1; value = from_stream.Read(buffer, offset++, 1)) { to_stream.Write(buffer, 0, value); } } catch (IOException e) { throw new InvalidOperationException(e.Message); } }
} // write() public void append(NAction <NOutputStream> appendCallback) // throws ResourceException { NOutputStream out_stream = append(); try { appendCallback.run(out_stream); } catch (Exception e) { throw new NResourceException("Error occurred in append callback", e); } finally { FileHelper.safeClose(out_stream); } } // append()
} // read() public void write(NAction <NOutputStream> writeCallback) // throws ResourceException { NOutputStream out_stream = write(); try { writeCallback.run(out_stream); } catch (Exception e) { throw new NResourceException("Error occurred in write callback", e); } finally { FileHelper.safeClose(out_stream); } } // write()
public static void copy(Resource from_res, Resource to_res) // throws IllegalStateException { Debug.Assert(from_res.isExists()); NInputStream in_stream = from_res.read(); try { NOutputStream out_stream = to_res.write(); try { copy(in_stream, out_stream); } finally { safeClose(out_stream); } } finally { safeClose(in_stream); } } // copy()
} // copy() public static NOutputStream getOutputStream(NOutputStream stream_arg) // throws IllegalStateException { return(getOutputStream(stream_arg, false)); } // getOutputStream()
} // writeStringAsFile() public static void writeStringAsFile(NOutputStream stream_arg, String string_arg, String encoding) // throws IllegalStateException { StreamWriter bw = getBufferedWriter(stream_arg, encoding); writeString(bw, string_arg, encoding); } // writeStringAsFile()
} // writeString() public static void writeStringAsFile(NOutputStream out_stream, String string_arg) // throws IllegalStateException { writeStringAsFile(out_stream, string_arg, DEFAULT_ENCODING); } // writeStringAsFile()
} // writeString() public static void writeString(NOutputStream out_stream, String string_arg, String encoding) // throws IllegalStateException { StreamWriter writer = getWriter(out_stream, encoding); writeString(writer, string_arg, encoding); } // writeString()
public static StreamWriter getWriter(NOutputStream stream_arg) // throws IllegalStateException { return(getWriter(stream_arg, DEFAULT_ENCODING)); } // getWriter()
} // safeClose() public static StreamWriter getBufferedWriter(NOutputStream stream_arg, String encoding) // throws IllegalStateException { StreamWriter writer = getWriter(stream_arg, encoding); return(writer); } // getBufferedWriter()
} // getWriter() public static StreamWriter getWriter(NOutputStream outputStream, string encoding) // throws IllegalStateException { return(getWriter(outputStream, encoding, false)); } // getWriter()