001/* Writer.java -- Base class for character output streams
002   Copyright (C) 1998, 1999, 2001, 2003, 2004, 2005  Free Software Foundation, Inc.
003
004This file is part of GNU Classpath.
005
006GNU Classpath is free software; you can redistribute it and/or modify
007it under the terms of the GNU General Public License as published by
008the Free Software Foundation; either version 2, or (at your option)
009any later version.
010
011GNU Classpath is distributed in the hope that it will be useful, but
012WITHOUT ANY WARRANTY; without even the implied warranty of
013MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014General Public License for more details.
015
016You should have received a copy of the GNU General Public License
017along with GNU Classpath; see the file COPYING.  If not, write to the
018Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
01902110-1301 USA.
020
021Linking this library statically or dynamically with other modules is
022making a combined work based on this library.  Thus, the terms and
023conditions of the GNU General Public License cover the whole
024combination.
025
026As a special exception, the copyright holders of this library give you
027permission to link this library with independent modules to produce an
028executable, regardless of the license terms of these independent
029modules, and to copy and distribute the resulting executable under
030terms of your choice, provided that you also meet, for each linked
031independent module, the terms and conditions of the license of that
032module.  An independent module is a module which is not derived from
033or based on this library.  If you modify this library, you may extend
034this exception to your version of the library, but you are not
035obligated to do so.  If you do not wish to do so, delete this
036exception statement from your version. */
037
038
039package java.io;
040
041/* Written using "Java Class Libraries", 2nd edition, plus online
042 * API docs for JDK 1.2 beta from http://www.javasoft.com.
043 * Status:  Believed complete and correct.
044 */
045
046/**
047 * This abstract class forms the base of the hierarchy of classes that
048 * write output as a stream of chars.  It provides a common set of methods
049 * for writing chars to stream.  Subclasses implement and/or extend these
050 * methods to write chars in a particular manner or to a particular
051 * destination such as a file on disk or network connection.
052 *
053 * @author Aaron M. Renn (arenn@urbanophile.com)
054 * @author Per Bothner (bothner@cygnus.com)
055 */
056public abstract class Writer implements Appendable, Closeable, Flushable
057{
058  /**
059   * This is the object used to synchronize criticial code sections for
060   * thread safety.  Subclasses should use this field instead of using
061   * synchronized methods or explicity synchronizations on <code>this</code>
062   */
063  protected Object lock;
064
065  /**
066   * This is the default no-argument constructor for this class.  This method
067   * will set up the class to synchronize criticial sections on itself.
068   */
069  protected Writer()
070  {
071    lock = this;
072  }
073
074  /**
075   * This method initializes a <code>Writer</code> that will synchronize
076   * on the specified <code>Object</code>.
077   *
078   * @param lock The <code>Object</code> to use for synchronizing critical
079   *             sections. Must not be null.
080   */
081  protected Writer(Object lock)
082  {
083    if (lock == null)
084      throw new NullPointerException();
085
086    this.lock = lock;
087  }
088
089  /**
090   * This method forces any data that may have been buffered to be written
091   * to the underlying output device.  Please note that the host environment
092   * might perform its own buffering unbeknowst to Java.  In that case, a
093   * write made (for example, to a disk drive) might be cached in OS
094   * buffers instead of actually being written to disk.
095   *
096   * @exception IOException If an error occurs
097   */
098  public abstract void flush() throws IOException;
099
100  /**
101   * This method closes the stream.  Any internal or native resources
102   * associated
103   * with this stream are freed.  Any subsequent attempt to access the stream
104   * might throw an exception.
105   * <p>
106   * This method in this class does nothing.
107   *
108   * @exception IOException If an error occurs
109   */
110  public abstract void close() throws IOException;
111
112  /**
113   * This method writes a single char to the output stream.
114   *
115   * @param b The char to be written to the output stream, passed as an int
116   *
117   * @exception IOException If an error occurs
118   */
119  public void write(int b) throws IOException
120  {
121    char[] buf = new char[1];
122
123    buf[0] = (char)b;
124    write(buf, 0, buf.length);
125  }
126
127  /**
128   * This method all the writes char from the passed array to the output
129   * stream. This method is equivalent to
130   * <code>write(buf, 0, buf.length)</code> which
131   * is exactly how it is implemented in this class.
132   *
133   * @param buf The array of char to write
134   *
135   * @exception IOException If an error occurs
136   */
137  public void write(char[] buf) throws IOException
138  {
139    write(buf, 0, buf.length);
140  }
141
142  /**
143   * This method writes <code>len</code> char from the specified array
144   * <code>buf</code> starting at index <code>offset</code> into the array.
145   * <p>
146   * Subclasses must provide an implementation of this abstract method.
147   *
148   * @param buf The array of char to write from
149   * @param offset The index into the array to start writing from
150   * @param len The number of char to write
151   *
152   * @exception IOException If an error occurs
153   */
154  public abstract void write(char[] buf, int offset, int len)
155    throws IOException;
156
157  /**
158   * This method writes all the characters in a <code>String</code> to the
159   * output.
160   *
161   * @param str The <code>String</code> whose chars are to be written.
162   *
163   * @exception IOException If an error occurs
164   */
165  public void write(String str) throws IOException
166  {
167    write(str, 0, str.length());
168  }
169
170  /**
171   * This method writes <code>len</code> chars from the <code>String</code>
172   * starting at position <code>offset</code>.
173   *
174   * @param str The <code>String</code> that is to be written
175   * @param offset The character offset into the <code>String</code> to start
176   *               writing from
177   * @param len The number of chars to write
178   *
179   * @exception IOException If an error occurs
180   */
181  public void write(String str, int offset, int len) throws IOException
182  {
183    // FIXME - for libgcj re-write using native code to not require
184    // copied buffer.
185    char[] buf = new char[len];
186
187    str.getChars(offset, offset + len, buf, 0);
188    write(buf, 0, len);
189  }
190
191  /** @since 1.5 */
192  public Writer append(char c) throws IOException
193  {
194    write(c);
195    return this;
196  }
197
198  /** @since 1.5 */
199  public Writer append(CharSequence cs) throws IOException
200  {
201    write(cs == null ? "null" : cs.toString());
202    return this;
203  }
204
205  /** @since 1.5 */
206  public Writer append(CharSequence cs, int start, int end) throws IOException
207  {
208    write(cs == null ? "null" : cs.subSequence(start, end).toString());
209    return this;
210  }
211}