View Javadoc
1   /*
2    * Copyright (c) 2001-2017, Zoltan Farkas All Rights Reserved.
3    *
4    * This library is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Lesser General Public
6    * License as published by the Free Software Foundation; either
7    * version 2.1 of the License, or (at your option) any later version.
8    *
9    * This library is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this program; if not, write to the Free Software
16   * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17   *
18   * Additionally licensed with:
19   *
20   * Licensed under the Apache License, Version 2.0 (the "License");
21   * you may not use this file except in compliance with the License.
22   * You may obtain a copy of the License at
23   *
24   *      http://www.apache.org/licenses/LICENSE-2.0
25   *
26   * Unless required by applicable law or agreed to in writing, software
27   * distributed under the License is distributed on an "AS IS" BASIS,
28   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
29   * See the License for the specific language governing permissions and
30   * limitations under the License.
31   */
32  package org.spf4j.io;
33  
34  import java.io.Closeable;
35  import java.io.Flushable;
36  import java.io.IOException;
37  import java.io.Writer;
38  import java.nio.CharBuffer;
39  
40  /**
41   * Utility class to adapt a Appendable to a Writer.
42   * this is a faster version of guava: CharStreams.asWriter
43   * @author zoly
44   */
45  public final class AppendableWriter extends Writer {
46  
47    private final Appendable appendable;
48  
49    private final boolean flushable;
50  
51    private boolean closed;
52  
53    public AppendableWriter(final Appendable appendable) {
54      this.appendable = appendable;
55      this.flushable = appendable instanceof Flushable;
56      this.closed = false;
57    }
58  
59    @Override
60    public void write(final char[] cbuf, final int off, final int len) throws IOException {
61      checkNotClosed();
62      /* Guava claims:
63      It turns out that creating a new String is usually as fast,
64      or faster than wrapping cbuf in a light-weight CharSequence.
65  
66      Since I am suspicious of claims that do not make much sense and most developers write rubbish benchmarks
67      (Linus I start to be like you more and more),
68      I wrote a JMH benchmark, and the results are as expected the opposite:
69  
70      Benchmark                                          Mode  Cnt      Score     Error  Units
71      AppendableWriterBenchmark.guavaAppendable         thrpt   10  10731.940 ± 427.258  ops/s
72      AppendableWriterBenchmark.spf4jAppendable         thrpt   10  17613.093 ± 344.769  ops/s
73  
74       Using a light weight wrapper is more than 50% faster!
75  
76       See AppendableWriterBenchmark for more detail...
77  
78      */
79      appendable.append(CharBuffer.wrap(cbuf), off, off + len);
80    }
81  
82    @Override
83    public void write(final int c) throws IOException {
84      checkNotClosed();
85      appendable.append((char) c);
86    }
87  
88    @Override
89    public Writer append(final char c) throws IOException {
90      checkNotClosed();
91      appendable.append(c);
92      return this;
93    }
94  
95    @Override
96    public Writer append(final CharSequence csq, final int start, final int end) throws IOException {
97      checkNotClosed();
98      appendable.append(csq, start, end);
99      return this;
100   }
101 
102   @Override
103   public Writer append(final CharSequence csq) throws IOException {
104     checkNotClosed();
105     appendable.append(csq);
106     return this;
107   }
108 
109   @Override
110   public void write(final String str, final int off, final int len) throws IOException {
111     checkNotClosed();
112     appendable.append(str, off, off + len);
113   }
114 
115   @Override
116   public void write(final String str) throws IOException {
117     appendable.append(str);
118   }
119 
120   @Override
121   public void write(final char[] cbuf) throws IOException {
122     appendable.append(CharBuffer.wrap(cbuf));
123   }
124 
125   @Override
126   public void flush() throws IOException {
127     checkNotClosed();
128     if (flushable) {
129       ((Flushable) appendable).flush();
130     }
131   }
132 
133   private void checkNotClosed() throws IOException {
134     if (closed) {
135       throw new IOException("Cannot write to closed writer " + this);
136     }
137   }
138 
139   @Override
140   public void close() throws IOException {
141     if (!closed) {
142       flush();
143       if (appendable instanceof Closeable) {
144         ((Closeable) appendable).close();
145       }
146       closed = true;
147     }
148   }
149 
150   @Override
151   public String toString() {
152     return "AppendableWriter{" + "appendable=" + appendable + ", flushable=" + flushable + ", closed=" + closed + '}';
153   }
154 
155 }