{"id":1174,"date":"2011-12-22T20:43:17","date_gmt":"2011-12-22T18:43:17","guid":{"rendered":"http:\/\/www.tutego.de\/blog\/javainsel\/?p=1174"},"modified":"2011-12-22T20:43:17","modified_gmt":"2011-12-22T18:43:17","slug":"guava-appendablewriter-internal-class-and-charstream-aswriter","status":"publish","type":"post","link":"https:\/\/www.tutego.de\/blog\/javainsel\/2011\/12\/guava-appendablewriter-internal-class-and-charstream-aswriter\/","title":{"rendered":"Guava AppendableWriter (internal class) and CharStream.asWriter()"},"content":{"rendered":"<p>With the current version the class is now package visible! If you want to use the class, copy it in your project and make it public. Or make use of CharStream.asWriter().<\/p>\n<p>&#160;<\/p>\n<p>The classes StringWriter, CharArrayWriter and ByteArrayOutputStream have two things in common: a) They are sinks and when you write into them you ask these classes for the collected result; b) the internal buffer always starts empty. So for getting the result the classes offer different methods:<\/p>\n<ul>\n<li>StringWriter: toString() return the buffer&#8217;s current value as a string.<\/li>\n<li>CharArrayWriter: toCharArray() returns a copy of the input data as a char array; toString() returns the input data as a String.<\/li>\n<li>ByteArrayOutputStream: toByteArray() returns a newly allocated byte array with a copy of the stream data. toString(String enc) converts the buffer&#8217;s contents into a String object, translating bytes into characters according to the given character encoding. The parameter less methode toString() uses the default encoding.<\/li>\n<\/ul>\n<p>One can see the second point, that every of these sink classes starts with an empty buffer, as a disadvantage. If one wants to append to an existing String or char or byte array this has to be done in a second step. It would be nice to have a class, lets say StringBuilderWriter, which writes into a mutable StringBuilder. But Java SE doesn\u2019t offer such a class.<\/p>\n<p>The Google Collection library provides a class AppendableWriter which writes into an Appendable. The Appendable interface was introduced in Java 5 and is implemented by classes to whom you can append chars or Strings to. It dictates three methods:<\/p>\n<ul>\n<li>Appendable append(char c) <\/li>\n<li>Appendable append(CharSequence csq) <\/li>\n<li>Appendable append(CharSequence csq, int start, int end) <\/li>\n<\/ul>\n<p>Implementing classes are among others:<\/p>\n<ul>\n<li>StringBuilder, StringBuffer<\/li>\n<li>Every Writer-class; the base class implements Appendable (this was retrofitted in Java 5)<\/li>\n<\/ul>\n<p>So to write into a StringBuilder all you have to do is:<\/p>\n<pre><pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">StringBuilder sb = <span style=\"color: #0000ff\">new<\/span> StringBuilder( &quot;<span style=\"color: #8b0000\">start-<\/span>&quot; );\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">Writer w = <span style=\"color: #0000ff\">new<\/span> AppendableWriter( sb );\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">w.write( &quot;<span style=\"color: #8b0000\">middle<\/span>&quot; );\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">w.close();\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">sb.append( &quot;<span style=\"color: #8b0000\">-end<\/span>&quot; );\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">System.out.println( sb ); <span style=\"color: #008000\">\/\/ start-middle-end<\/span><\/pre>\n<p>So with AppendableWriter its easy to represent every Appendable as a Writer. This is exactly what the static method asWriter() in the utility class CharStreams does:<\/p>\n<pre><pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"><span style=\"color: #0000ff\">public<\/span> <span style=\"color: #0000ff\">static<\/span> Writer asWriter(Appendable target) {\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"> <span style=\"color: #0000ff\">if<\/span> (target <span style=\"color: #0000ff\">instanceof<\/span> Writer) {\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">  <span style=\"color: #0000ff\">return<\/span> (Writer) target;\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"> }\n<\/pre>\n<pre style=\"background-color: #fbfbfb; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\"> <span style=\"color: #0000ff\">return<\/span> <span style=\"color: #0000ff\">new<\/span> AppendableWriter(target);\n<\/pre>\n<pre style=\"background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 13px\">}<\/pre>\n<p>If the Appendable is already of subtype Writer it does not make sense to wrap it in an AppendableWriter again, so the target is directly returned. But what happens if you call close() or flush() on this special Writer\u2014an Appendable doesn\u2019t have close() neither flush()? The answer is simple: If you call close()\/flush() on an AppendableWriter the implementation checks if the constructor argument implements Closeable\/Flushable and calls close()\/flush() accordingly. That means if you close\/flush this Writer the close()\/flush() operation will be delegated otherwise\u2014for example in the case of StringBuilder with does neither implement Closeable nor Flushable\u2014nothing happens.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With the current version the class is now package visible! If you want to use the class, copy it in your project and make it public. Or make use of CharStream.asWriter(). &#160; The classes StringWriter, CharArrayWriter and ByteArrayOutputStream have two things in common: a) They are sinks and when you write into them you ask [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","_links_to":"","_links_to_target":""},"categories":[67],"tags":[],"class_list":["post-1174","post","type-post","status-publish","format-standard","hentry","category-guavagoogle-core-libraries"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1174","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/comments?post=1174"}],"version-history":[{"count":1,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1174\/revisions"}],"predecessor-version":[{"id":1175,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/posts\/1174\/revisions\/1175"}],"wp:attachment":[{"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/media?parent=1174"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/categories?post=1174"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tutego.de\/blog\/javainsel\/wp-json\/wp\/v2\/tags?post=1174"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}