2 * @(#)FilterOutputStream.java 1.0 00/10/10
4 * Copyright (c) 2001 International Business Machines Corp.
7 * This software has been released under the terms of the IBM Public
8 * License. For details, see the LICENSE file in the top-level source
9 * directory or online at http://www.openafs.org/dl/license10.html
11 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
12 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
13 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
14 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
15 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
16 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
17 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
18 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
19 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
20 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 package org
.openafs
.jafs
;
26 import java
.io
.OutputStream
;
29 * This class is a file output stream for files within AFS.
30 * It is an output stream for writing data to a
31 * <code>{@link org.openafs.jafs.File}</code>.
33 * @version 2.1, 08/03/2001
34 * @see org.openafs.jafs.File
35 * @see org.openafs.jafs.FileInputStream
36 * @see java.io.FileOutputStream
38 public class FileOutputStream
extends OutputStream
40 /** Status indicator for the current state of this file output stream */
41 private int fileDescriptor
;
44 * Creates an output file stream to write to the AFS file with the
47 * If the file exists but is a directory rather than a regular file, does
48 * not exist but cannot be created, or cannot be opened for any other
49 * reason then a <code>AFSFileException</code> is thrown.
51 * @param name the name of the file to write to
52 * @exception AFSFileException If an AFS specific error occurs,
53 * if the file exists but is a directory
54 * rather than a regular file, does not exist but cannot
55 * be created, or cannot be opened for any other reason, including
58 public FileOutputStream(String name
) throws AFSFileException
63 * Creates an output file stream to write to the AFS file with the specified
64 * <code>name</code>. If the second argument is <code>true</code>, then
65 * bytes will be written to the end of the file rather than the beginning.
67 * If the file exists but is a directory rather than a regular file, does
68 * not exist but cannot be created, or cannot be opened for any other
69 * reason then a <code>AFSFileException</code> is thrown.
71 * @param name the name of the file to write to
72 * @param append if <code>true</code>, then bytes will be written
73 * to the end of the file rather than the beginning
74 * @exception AFSFileException If an AFS specific error occurs,
75 * if the file exists but is a directory
76 * rather than a regular file, does not exist but cannot
77 * be created, or cannot be opened for any other reason, including
80 public FileOutputStream(String name
, boolean append
) throws AFSFileException
83 fileDescriptor
= this.openAppend(name
);
85 fileDescriptor
= this.openWrite(name
);
89 * Creates a file output stream to write to the AFS file represented by
90 * the specified <code>File</code> object.
92 * If the file exists but is a directory rather than a regular file, does
93 * not exist but cannot be created, or cannot be opened for any other
94 * reason then a <code>AFSFileException</code> is thrown.
96 * @param file the AFS file to be opened for writing.
97 * @exception AFSFileException If an AFS specific error occurs,
98 * if the file exists but is a directory
99 * rather than a regular file, does not exist but cannot
100 * be created, or cannot be opened for any other reason, including
102 * @see org.openafs.jafs.File#getPath()
104 public FileOutputStream(File file
) throws AFSFileException
106 this(file
.getPath(), false);
109 * Creates a file output stream to write to the AFS file represented by
110 * the specified <code>File</code> object.
112 * If the file exists but is a directory rather than a regular file, does
113 * not exist but cannot be created, or cannot be opened for any other
114 * reason then a <code>AFSFileException</code> is thrown.
116 * @param file the AFS file to be opened for writing.
117 * @param append if <code>true</code>, then bytes will be written
118 * to the end of the file rather than the beginning
119 * @exception AFSFileException If an AFS specific error occurs,
120 * if the file exists but is a directory
121 * rather than a regular file, does not exist but cannot
122 * be created, or cannot be opened for any other reason, including
124 * @see org.openafs.jafs.File#getPath()
126 public FileOutputStream(File file
, boolean append
) throws AFSFileException
128 this(file
.getPath(), append
);
131 /*-------------------------------------------------------------------------*/
134 * Writes the specified <code>byte</code> to this file output stream.
136 * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
138 * @param b the byte to be written.
139 * @exception AFSFileException if an error occurs.
141 public void write(int b
) throws AFSFileException
143 byte[] bytes
= new byte[1];
145 this.write(bytes
, 0, 1);
148 * Writes <code>b.length</code> bytes from the specified byte array
149 * to this file output stream.
151 * Implements the <code>write</code> method of three arguments with the
152 * arguments <code>b</code>, <code>0</code>, and
153 * <code>b.length</code>.
155 * Note that this method does not call the one-argument
156 * <code>write</code> method of its underlying stream with the single
157 * argument <code>b</code>.
159 * @param b the data to be written.
160 * @exception AFSFileException if an error occurs.
161 * @see #write(byte[], int, int)
162 * @see java.io.FilterOutputStream#write(byte[], int, int)
164 public void write(byte[] b
) throws AFSFileException
166 this.write(b
, 0, b
.length
);
169 /////////////// public native methods ////////////////////
172 * Writes <code>len</code> bytes from the specified
173 * <code>byte</code> array starting at offset <code>off</code> to
174 * this file output stream.
176 * @param b the data to be written
177 * @param off the start offset in the data
178 * @param len the number of bytes that are written
179 * @exception AFSFileException if an I/O or other file related error occurs.
180 * @see java.io.FilterOutputStream#write(int)
182 public native void write(byte[] b
, int off
, int len
) throws AFSFileException
;
184 * Closes this file output stream and releases any system resources
185 * associated with this stream. This file output stream may no longer
186 * be used for writing bytes.
188 * @exception AFSFileException if an I/O or other file related error occurs.
190 public native void close() throws AFSFileException
;
192 /////////////// private native methods ////////////////////
195 * Opens an AFS file, with the specified name, for writing.
197 * @param filename name of file to be opened
198 * @return file descriptor
199 * @exception AFSFileException if an I/O or other file related error occurs.
201 private native int openWrite(String filename
) throws AFSFileException
;
203 * Opens an AFS file, with the specified name, for appending.
205 * @param filename name of file to be opened
206 * @return file descriptor
207 * @exception AFSFileException if an I/O or other file related error occurs.
209 private native int openAppend(String filename
) throws AFSFileException
;
211 /*-------------------------------------------------------------------------*/