001 /* FilterOutputStream.java -- Parent class for output streams that filter 002 Copyright (C) 1998, 1999, 2001, 2003, 2005 Free Software Foundation, Inc. 003 004 This file is part of GNU Classpath. 005 006 GNU Classpath is free software; you can redistribute it and/or modify 007 it under the terms of the GNU General Public License as published by 008 the Free Software Foundation; either version 2, or (at your option) 009 any later version. 010 011 GNU Classpath is distributed in the hope that it will be useful, but 012 WITHOUT ANY WARRANTY; without even the implied warranty of 013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 General Public License for more details. 015 016 You should have received a copy of the GNU General Public License 017 along with GNU Classpath; see the file COPYING. If not, write to the 018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 019 02110-1301 USA. 020 021 Linking this library statically or dynamically with other modules is 022 making a combined work based on this library. Thus, the terms and 023 conditions of the GNU General Public License cover the whole 024 combination. 025 026 As a special exception, the copyright holders of this library give you 027 permission to link this library with independent modules to produce an 028 executable, regardless of the license terms of these independent 029 modules, and to copy and distribute the resulting executable under 030 terms of your choice, provided that you also meet, for each linked 031 independent module, the terms and conditions of the license of that 032 module. An independent module is a module which is not derived from 033 or based on this library. If you modify this library, you may extend 034 this exception to your version of the library, but you are not 035 obligated to do so. If you do not wish to do so, delete this 036 exception statement from your version. */ 037 038 039 package java.io; 040 041 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 042 * "The Java Language Specification", ISBN 0-201-63451-1 043 * Status: Complete to version 1.1. 044 */ 045 046 /** 047 * This class is the common superclass of output stream classes that 048 * filter the output they write. These classes typically transform the 049 * data in some way prior to writing it out to another underlying 050 * <code>OutputStream</code>. This class simply overrides all the 051 * methods in <code>OutputStream</code> to redirect them to the 052 * underlying stream. Subclasses provide actual filtering. 053 * 054 * @author Aaron M. Renn (arenn@urbanophile.com) 055 * @author Tom Tromey (tromey@cygnus.com) 056 */ 057 public class FilterOutputStream extends OutputStream 058 { 059 /** 060 * This is the subordinate <code>OutputStream</code> that this class 061 * redirects its method calls to. 062 */ 063 protected OutputStream out; 064 065 /** 066 * This method initializes an instance of <code>FilterOutputStream</code> 067 * to write to the specified subordinate <code>OutputStream</code>. 068 * 069 * @param out The <code>OutputStream</code> to write to 070 */ 071 public FilterOutputStream(OutputStream out) 072 { 073 this.out = out; 074 } 075 076 /** 077 * This method closes the underlying <code>OutputStream</code>. Any 078 * further attempts to write to this stream may throw an exception. 079 * 080 * @exception IOException If an error occurs 081 */ 082 public void close() throws IOException 083 { 084 flush(); 085 out.close(); 086 } 087 088 /** 089 * This method attempt to flush all buffered output to be written to the 090 * underlying output sink. 091 * 092 * @exception IOException If an error occurs 093 */ 094 public void flush() throws IOException 095 { 096 out.flush(); 097 } 098 099 /** 100 * This method writes a single byte of output to the underlying 101 * <code>OutputStream</code>. 102 * 103 * @param b The byte to write, passed as an int. 104 * 105 * @exception IOException If an error occurs 106 */ 107 public void write(int b) throws IOException 108 { 109 out.write(b); 110 } 111 112 /** 113 * This method writes all the bytes in the specified array to the underlying 114 * <code>OutputStream</code>. It does this by calling the three parameter 115 * version of this method - <code>write(byte[], int, int)</code> in this 116 * class instead of writing to the underlying <code>OutputStream</code> 117 * directly. This allows most subclasses to avoid overriding this method. 118 * 119 * @param buf The byte array to write bytes from 120 * 121 * @exception IOException If an error occurs 122 */ 123 public void write(byte[] buf) throws IOException 124 { 125 // Don't do checking here, per Java Lang Spec. 126 write(buf, 0, buf.length); 127 } 128 129 /** 130 * This method calls the <code>write(int)</code> method <code>len</code> 131 * times for all bytes from the array <code>buf</code> starting at index 132 * <code>offset</code>. Subclasses should overwrite this method to get a 133 * more efficient implementation. 134 * 135 * @param buf The byte array to write bytes from 136 * @param offset The index into the array to start writing bytes from 137 * @param len The number of bytes to write 138 * 139 * @exception IOException If an error occurs 140 */ 141 public void write(byte[] buf, int offset, int len) throws IOException 142 { 143 // Don't do checking here, per Java Lang Spec. 144 for (int i=0; i < len; i++) 145 write(buf[offset + i]); 146 147 } 148 149 } // class FilterOutputStream 150