Writer or OutputStream?

I'm designing a library where a class should have an ability to be able to convert itself internals into text. Which class shall I use: OutputStream or Writer? And what is the key difference between them (in my case)?

public interface Memento {
  void save(OutputStream stream);
  void save(Writer writer);
}

Which one?


Solution 1:

An OutputStream is a byte-oriented stream. Any text you write has to be encoded as bytes using some encoding (most commonly ISO-8859-1 or UTF-8). A Writer is a character-oriented stream that may or may not internally encode characters as bytes, depending on what it is writing to.

EDIT If you are designing a library, then if you provide an OutputStream-oriented interface to which text is to be written, you really should provide client classes the ability to control the encoding to be used.

Solution 2:

Text? Writer. It is intended for handling characters, honors encoding.

Stream/array of bytes? OutputStream. Works on raw bytes, has no notion of characters, encodings, strings, etc.