You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@harmony.apache.org by nd...@apache.org on 2006/08/18 03:45:39 UTC
svn commit: r432462 [6/21] -
/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileDescriptor.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileDescriptor.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileDescriptor.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileDescriptor.java Thu Aug 17 18:45:35 2006
@@ -1,88 +1,88 @@
-/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-
-/**
- * FileDescriptor is the lowest level representation of a File, Device, or
- * Socket. You can create any of the IO classes which take a FileDescriptor as
- * an argument by querying an open Socket or File for the FileDescriptor.
- * <p>
- * The FileDescriptor class also contains static fields representing Standard
- * Input, Output and Error. You can use these directly if desired but it is
- * recommended you go through System.in, System.out, and System.err streams
- * respectively.
- * <p>
- * Applications should not create new FileDescriptors.
- *
- * @see FileInputStream#getFD()
- * @see FileOutputStream#getFD()
- * @see RandomAccessFile#getFD()
- */
-public final class FileDescriptor {
- /** FileDescriptor representing Standard In */
- public static final FileDescriptor in = new FileDescriptor();
-
- /** FileDescriptor representing Standard Out */
- public static final FileDescriptor out = new FileDescriptor();
-
- /** FileDescriptor representing Standard Error */
- public static final FileDescriptor err = new FileDescriptor();
-
- /**
- * Represents a link to any underlying OS resources for this FileDescriptor.
- * A value of -1 indicates that this FileDescriptor is invalid.
- */
- long descriptor = -1;
-
- private static native void oneTimeInitialization();
-
- static {
- in.descriptor = 0;
- out.descriptor = 1;
- err.descriptor = 2;
-
- oneTimeInitialization();
- }
-
- /**
- * Constructs a new FileDescriptor containing an invalid handle. This
- * constructor does nothing interesting. Provided for signature
- * compatibility.
- *
- */
- public FileDescriptor() {
- super();
- }
-
- /**
- * Ensures that data which is buffered within the underlying implementation
- * is written out to the appropriate device before returning.
- *
- * @throws SyncFailedException
- * when the operation fails
- */
- public native void sync() throws SyncFailedException;
-
- /**
- * Answers a boolean indicating whether or not this FileDescriptor is valid.
- *
- * @return <code>true</code> if this FileDescriptor is valid,
- * <code>false</code> otherwise
- */
- public native boolean valid();
-
-}
+/* Copyright 1998, 2005 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * FileDescriptor is the lowest level representation of a File, Device, or
+ * Socket. You can create any of the IO classes which take a FileDescriptor as
+ * an argument by querying an open Socket or File for the FileDescriptor.
+ * <p>
+ * The FileDescriptor class also contains static fields representing Standard
+ * Input, Output and Error. You can use these directly if desired but it is
+ * recommended you go through System.in, System.out, and System.err streams
+ * respectively.
+ * <p>
+ * Applications should not create new FileDescriptors.
+ *
+ * @see FileInputStream#getFD()
+ * @see FileOutputStream#getFD()
+ * @see RandomAccessFile#getFD()
+ */
+public final class FileDescriptor {
+ /** FileDescriptor representing Standard In */
+ public static final FileDescriptor in = new FileDescriptor();
+
+ /** FileDescriptor representing Standard Out */
+ public static final FileDescriptor out = new FileDescriptor();
+
+ /** FileDescriptor representing Standard Error */
+ public static final FileDescriptor err = new FileDescriptor();
+
+ /**
+ * Represents a link to any underlying OS resources for this FileDescriptor.
+ * A value of -1 indicates that this FileDescriptor is invalid.
+ */
+ long descriptor = -1;
+
+ private static native void oneTimeInitialization();
+
+ static {
+ in.descriptor = 0;
+ out.descriptor = 1;
+ err.descriptor = 2;
+
+ oneTimeInitialization();
+ }
+
+ /**
+ * Constructs a new FileDescriptor containing an invalid handle. This
+ * constructor does nothing interesting. Provided for signature
+ * compatibility.
+ *
+ */
+ public FileDescriptor() {
+ super();
+ }
+
+ /**
+ * Ensures that data which is buffered within the underlying implementation
+ * is written out to the appropriate device before returning.
+ *
+ * @throws SyncFailedException
+ * when the operation fails
+ */
+ public native void sync() throws SyncFailedException;
+
+ /**
+ * Answers a boolean indicating whether or not this FileDescriptor is valid.
+ *
+ * @return <code>true</code> if this FileDescriptor is valid,
+ * <code>false</code> otherwise
+ */
+ public native boolean valid();
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileDescriptor.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileFilter.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileFilter.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileFilter.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileFilter.java Thu Aug 17 18:45:35 2006
@@ -1,32 +1,32 @@
-/* Copyright 1998, 2002 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-
-/**
- * FileFilter is an interface for filtering abstract Files
- *
- */
-public abstract interface FileFilter {
-
-/**
- * Answers a boolean indicating whether or not a specific File should be included in a pathname list.
- *
- * @param pathname the abstract File to check.
- * @return <code>true</code> if the File should be includes, <code>false</code> otherwise.
- */
-public abstract boolean accept(File pathname);
-}
+/* Copyright 1998, 2002 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * FileFilter is an interface for filtering abstract Files
+ *
+ */
+public abstract interface FileFilter {
+
+/**
+ * Answers a boolean indicating whether or not a specific File should be included in a pathname list.
+ *
+ * @param pathname the abstract File to check.
+ * @return <code>true</code> if the File should be includes, <code>false</code> otherwise.
+ */
+public abstract boolean accept(File pathname);
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileFilter.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java Thu Aug 17 18:45:35 2006
@@ -1,356 +1,356 @@
-/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-import java.nio.channels.FileChannel;
-
-import org.apache.harmony.luni.platform.IFileSystem;
-import org.apache.harmony.luni.platform.Platform;
-import org.apache.harmony.luni.util.Msg;
-import org.apache.harmony.nio.FileChannelFactory;
-
-/**
- * FileInputStream is a class for reading bytes from a file. This class may also
- * be used with other InputStreams, ie: BufferedInputStream, to read data from a
- * file with buffering.
- *
- * @see FileOutputStream
- */
-public class FileInputStream extends InputStream implements Closeable {
- /**
- * The FileDescriptor representing this FileInputStream.
- */
- FileDescriptor fd;
-
- // The unique file channel associated with this FileInputStream (lazily
- // initialized).
- private FileChannel channel;
-
- private IFileSystem fileSystem = Platform.getFileSystem();
-
- private Object repositioningLock = new Object();
-
- /**
- * Constructs a new FileInputStream on the File <code>file</code>. If the
- * file does not exist, the <code>FileNotFoundException</code> is thrown.
- *
- * @param file
- * the File on which to stream reads.
- *
- * @throws FileNotFoundException
- * If the <code>file</code> is not found.
- *
- * @see java.lang.SecurityManager#checkRead(FileDescriptor)
- * @see java.lang.SecurityManager#checkRead(String)
- * @see java.lang.SecurityManager#checkRead(String, Object)
- */
- public FileInputStream(File file) throws FileNotFoundException {
- super();
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- String filePath = (null == file ? null : file.getPath());
- security.checkRead(filePath);
- }
- fd = new FileDescriptor();
- fd.descriptor = fileSystem.open(file.properPath(true),
- IFileSystem.O_RDONLY);
- channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
- IFileSystem.O_RDONLY);
- }
-
- /**
- * Constructs a new FileInputStream on the FileDescriptor <code>fd</code>.
- * The file must already be open, therefore no
- * <code>FileNotFoundException</code> will be thrown.
- *
- * @param fd
- * the FileDescriptor on which to stream reads.
- *
- * @see java.lang.SecurityManager#checkRead(FileDescriptor)
- * @see java.lang.SecurityManager#checkRead(String)
- * @see java.lang.SecurityManager#checkRead(String, Object)
- */
- public FileInputStream(FileDescriptor fd) {
- super();
- if (fd == null) {
- throw new NullPointerException();
- }
- SecurityManager security = System.getSecurityManager();
- if (security != null) {
- security.checkRead(fd);
- }
- this.fd = fd;
- channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
- IFileSystem.O_RDONLY);
- }
-
- /**
- * Constructs a new FileInputStream on the file named <code>fileName</code>.
- * If the file does not exist, the <code>FileNotFoundException</code> is
- * thrown. The <code>fileName</code> may be absolute or relative to the
- * System property <code>"user.dir"</code>.
- *
- * @param fileName
- * the file on which to stream reads.
- *
- * @throws FileNotFoundException
- * If the <code>fileName</code> is not found.
- */
- public FileInputStream(String fileName) throws FileNotFoundException {
- this(null == fileName ? (File) null : new File(fileName));
- }
-
- /**
- * Answers a int representing then number of bytes that are available before
- * this InputStream will block. This method always returns the size of the
- * file minus the current position.
- *
- * @return the number of bytes available before blocking.
- *
- * @throws IOException
- * If an error occurs in this stream.
- */
- @Override
- public int available() throws IOException {
- openCheck();
- synchronized (repositioningLock) {
- // stdin requires special handling
- if (fd == FileDescriptor.in) {
- return (int) fileSystem.ttyAvailable();
- }
-
- long currentPosition = fileSystem.seek(fd.descriptor, 0L,
- IFileSystem.SEEK_CUR);
- long endOfFilePosition = fileSystem.seek(fd.descriptor, 0L,
- IFileSystem.SEEK_END);
- fileSystem.seek(fd.descriptor, currentPosition,
- IFileSystem.SEEK_SET);
- return (int) (endOfFilePosition - currentPosition);
- }
- }
-
- /**
- * Close the FileInputStream.
- *
- * @throws IOException
- * If an error occurs attempting to close this FileInputStream.
- */
- @Override
- public void close() throws IOException {
- if (fd == null) {
- // if fd is null, then the underlying file is not opened, so nothing
- // to close
- return;
- }
- if (channel == null) {
- /*
- * if channel is null, then the channel doesn't need be taken care
- * of but the underlying file has been opened
- */
- synchronized (this) {
- if (fd.descriptor >= 0) {
- fileSystem.close(fd.descriptor);
- }
- fd.descriptor = -1;
- }
- } else {
- /*
- * if the FileInputStream is constructed sucessfully, then channel
- * must be closed, which will close the underlying file
- */
- synchronized (channel) {
- synchronized (this) {
- // FIXME: System.in, out, err may not want to be closed?
- if (channel.isOpen() && fd.descriptor >= 0) {
- channel.close();
- }
- fd.descriptor = -1;
- }
- }
- }
- }
-
- /**
- * This method ensures that all resources for this file are released when it
- * is about to be garbage collected.
- *
- * @throws IOException
- * If an error occurs attempting to finalize this
- * FileInputStream.
- */
- @Override
- protected void finalize() throws IOException {
- close();
- }
-
- /**
- * Answers the FileChannel equivalent to this input stream.
- * <p>
- * The file channel is read-only and has an initial position within the file
- * that is the same as the current position of the FileInputStream within
- * the file. All changes made to the underlying file descriptor state via
- * the channel are visible by the input stream and vice versa.
- * </p>
- *
- * @return the file channel representation for this FileInputStream.
- */
- public FileChannel getChannel() {
- return channel;
- }
-
- /**
- * Answers the FileDescriptor representing the operating system resource for
- * this FileInputStream.
- *
- * @return the FileDescriptor for this FileInputStream.
- *
- * @throws IOException
- * If an error occurs attempting to get the FileDescriptor of
- * this FileInputStream.
- */
- public final FileDescriptor getFD() throws IOException {
- return fd;
- }
-
- /**
- * Reads a single byte from this FileInputStream and returns the result as
- * an int. The low-order byte is returned or -1 of the end of stream was
- * encountered.
- *
- * @return the byte read or -1 if end of stream.
- *
- * @throws IOException
- * If the stream is already closed or another IOException
- * occurs.
- */
- @Override
- public int read() throws IOException {
- byte[] readed = new byte[1];
- int result = read(readed, 0, 1);
- return result == -1 ? -1 : readed[0] & 0xff;
- }
-
- /**
- * Reads bytes from the FileInputStream and stores them in byte array
- * <code>buffer</code>. Answer the number of bytes actually read or -1 if
- * no bytes were read and end of stream was encountered.
- *
- * @param buffer
- * the byte array in which to store the read bytes.
- * @return the number of bytes actually read or -1 if end of stream.
- *
- * @throws IOException
- * If the stream is already closed or another IOException
- * occurs.
- */
- @Override
- public int read(byte[] buffer) throws IOException {
- return read(buffer, 0, buffer.length);
- }
-
- /**
- * Reads at most <code>count</code> bytes from the FileInputStream and
- * stores them in byte array <code>buffer</code> starting at
- * <code>offset</code>. Answer the number of bytes actually read or -1 if
- * no bytes were read and end of stream was encountered.
- *
- * @param buffer
- * the byte array in which to store the read bytes.
- * @param offset
- * the offset in <code>buffer</code> to store the read bytes.
- * @param count
- * the maximum number of bytes to store in <code>buffer</code>.
- * @return the number of bytes actually read or -1 if end of stream.
- *
- * @throws IOException
- * If the stream is already closed or another IOException
- * occurs.
- */
- @Override
- public int read(byte[] buffer, int offset, int count) throws IOException {
- if (count > buffer.length - offset || count < 0 || offset < 0) {
- throw new IndexOutOfBoundsException();
- }
- if (0 == count) {
- return 0;
- }
- openCheck();
- synchronized (repositioningLock) {
- // stdin requires special handling
- if (fd == FileDescriptor.in) {
- return (int) fileSystem.ttyRead(buffer, offset, count);
- }
- return (int) fileSystem.read(fd.descriptor, buffer, offset, count);
- }
- }
-
- /**
- * Skips <code>count</code> number of bytes in this FileInputStream.
- * Subsequent <code>read()</code>'s will not return these bytes unless
- * <code>reset()</code> is used. This method may perform multiple reads to
- * read <code>count</code> bytes.
- *
- * @param count
- * the number of bytes to skip.
- * @return the number of bytes actually skipped.
- *
- * @throws IOException
- * If the stream is already closed or another IOException
- * occurs.
- */
- @Override
- public long skip(long count) throws IOException {
- openCheck();
-
- if (count == 0) {
- return 0;
- }
- if (count < 0) {
- // KA013=Number of bytes to skip cannot be negative
- throw new IOException(Msg.getString("KA013")); //$NON-NLS-1$
- }
-
- // stdin requires special handling
- if (fd == FileDescriptor.in) {
- // Read and discard count bytes in 8k chunks
- long skipped = 0, numRead;
- int chunk = count < 8192 ? (int) count : 8192;
- byte[] buffer = new byte[chunk];
- for (long i = count / chunk; i >= 0; i--) {
- numRead = fileSystem.ttyRead(buffer, 0, chunk);
- skipped += numRead;
- if (numRead < chunk) {
- return skipped;
- }
- }
- return skipped;
- }
-
- synchronized (repositioningLock) {
- final long currentPosition = fileSystem.seek(fd.descriptor, 0L,
- IFileSystem.SEEK_CUR);
- final long newPosition = fileSystem.seek(fd.descriptor,
- currentPosition + count, IFileSystem.SEEK_SET);
- return newPosition - currentPosition;
- }
- }
-
- private synchronized void openCheck() throws IOException {
- if (fd.descriptor < 0) {
- throw new IOException();
- }
- }
-}
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+import java.nio.channels.FileChannel;
+
+import org.apache.harmony.luni.platform.IFileSystem;
+import org.apache.harmony.luni.platform.Platform;
+import org.apache.harmony.luni.util.Msg;
+import org.apache.harmony.nio.FileChannelFactory;
+
+/**
+ * FileInputStream is a class for reading bytes from a file. This class may also
+ * be used with other InputStreams, ie: BufferedInputStream, to read data from a
+ * file with buffering.
+ *
+ * @see FileOutputStream
+ */
+public class FileInputStream extends InputStream implements Closeable {
+ /**
+ * The FileDescriptor representing this FileInputStream.
+ */
+ FileDescriptor fd;
+
+ // The unique file channel associated with this FileInputStream (lazily
+ // initialized).
+ private FileChannel channel;
+
+ private IFileSystem fileSystem = Platform.getFileSystem();
+
+ private Object repositioningLock = new Object();
+
+ /**
+ * Constructs a new FileInputStream on the File <code>file</code>. If the
+ * file does not exist, the <code>FileNotFoundException</code> is thrown.
+ *
+ * @param file
+ * the File on which to stream reads.
+ *
+ * @throws FileNotFoundException
+ * If the <code>file</code> is not found.
+ *
+ * @see java.lang.SecurityManager#checkRead(FileDescriptor)
+ * @see java.lang.SecurityManager#checkRead(String)
+ * @see java.lang.SecurityManager#checkRead(String, Object)
+ */
+ public FileInputStream(File file) throws FileNotFoundException {
+ super();
+ SecurityManager security = System.getSecurityManager();
+ if (security != null) {
+ String filePath = (null == file ? null : file.getPath());
+ security.checkRead(filePath);
+ }
+ fd = new FileDescriptor();
+ fd.descriptor = fileSystem.open(file.properPath(true),
+ IFileSystem.O_RDONLY);
+ channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+ IFileSystem.O_RDONLY);
+ }
+
+ /**
+ * Constructs a new FileInputStream on the FileDescriptor <code>fd</code>.
+ * The file must already be open, therefore no
+ * <code>FileNotFoundException</code> will be thrown.
+ *
+ * @param fd
+ * the FileDescriptor on which to stream reads.
+ *
+ * @see java.lang.SecurityManager#checkRead(FileDescriptor)
+ * @see java.lang.SecurityManager#checkRead(String)
+ * @see java.lang.SecurityManager#checkRead(String, Object)
+ */
+ public FileInputStream(FileDescriptor fd) {
+ super();
+ if (fd == null) {
+ throw new NullPointerException();
+ }
+ SecurityManager security = System.getSecurityManager();
+ if (security != null) {
+ security.checkRead(fd);
+ }
+ this.fd = fd;
+ channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+ IFileSystem.O_RDONLY);
+ }
+
+ /**
+ * Constructs a new FileInputStream on the file named <code>fileName</code>.
+ * If the file does not exist, the <code>FileNotFoundException</code> is
+ * thrown. The <code>fileName</code> may be absolute or relative to the
+ * System property <code>"user.dir"</code>.
+ *
+ * @param fileName
+ * the file on which to stream reads.
+ *
+ * @throws FileNotFoundException
+ * If the <code>fileName</code> is not found.
+ */
+ public FileInputStream(String fileName) throws FileNotFoundException {
+ this(null == fileName ? (File) null : new File(fileName));
+ }
+
+ /**
+ * Answers a int representing then number of bytes that are available before
+ * this InputStream will block. This method always returns the size of the
+ * file minus the current position.
+ *
+ * @return the number of bytes available before blocking.
+ *
+ * @throws IOException
+ * If an error occurs in this stream.
+ */
+ @Override
+ public int available() throws IOException {
+ openCheck();
+ synchronized (repositioningLock) {
+ // stdin requires special handling
+ if (fd == FileDescriptor.in) {
+ return (int) fileSystem.ttyAvailable();
+ }
+
+ long currentPosition = fileSystem.seek(fd.descriptor, 0L,
+ IFileSystem.SEEK_CUR);
+ long endOfFilePosition = fileSystem.seek(fd.descriptor, 0L,
+ IFileSystem.SEEK_END);
+ fileSystem.seek(fd.descriptor, currentPosition,
+ IFileSystem.SEEK_SET);
+ return (int) (endOfFilePosition - currentPosition);
+ }
+ }
+
+ /**
+ * Close the FileInputStream.
+ *
+ * @throws IOException
+ * If an error occurs attempting to close this FileInputStream.
+ */
+ @Override
+ public void close() throws IOException {
+ if (fd == null) {
+ // if fd is null, then the underlying file is not opened, so nothing
+ // to close
+ return;
+ }
+ if (channel == null) {
+ /*
+ * if channel is null, then the channel doesn't need be taken care
+ * of but the underlying file has been opened
+ */
+ synchronized (this) {
+ if (fd.descriptor >= 0) {
+ fileSystem.close(fd.descriptor);
+ }
+ fd.descriptor = -1;
+ }
+ } else {
+ /*
+ * if the FileInputStream is constructed sucessfully, then channel
+ * must be closed, which will close the underlying file
+ */
+ synchronized (channel) {
+ synchronized (this) {
+ // FIXME: System.in, out, err may not want to be closed?
+ if (channel.isOpen() && fd.descriptor >= 0) {
+ channel.close();
+ }
+ fd.descriptor = -1;
+ }
+ }
+ }
+ }
+
+ /**
+ * This method ensures that all resources for this file are released when it
+ * is about to be garbage collected.
+ *
+ * @throws IOException
+ * If an error occurs attempting to finalize this
+ * FileInputStream.
+ */
+ @Override
+ protected void finalize() throws IOException {
+ close();
+ }
+
+ /**
+ * Answers the FileChannel equivalent to this input stream.
+ * <p>
+ * The file channel is read-only and has an initial position within the file
+ * that is the same as the current position of the FileInputStream within
+ * the file. All changes made to the underlying file descriptor state via
+ * the channel are visible by the input stream and vice versa.
+ * </p>
+ *
+ * @return the file channel representation for this FileInputStream.
+ */
+ public FileChannel getChannel() {
+ return channel;
+ }
+
+ /**
+ * Answers the FileDescriptor representing the operating system resource for
+ * this FileInputStream.
+ *
+ * @return the FileDescriptor for this FileInputStream.
+ *
+ * @throws IOException
+ * If an error occurs attempting to get the FileDescriptor of
+ * this FileInputStream.
+ */
+ public final FileDescriptor getFD() throws IOException {
+ return fd;
+ }
+
+ /**
+ * Reads a single byte from this FileInputStream and returns the result as
+ * an int. The low-order byte is returned or -1 of the end of stream was
+ * encountered.
+ *
+ * @return the byte read or -1 if end of stream.
+ *
+ * @throws IOException
+ * If the stream is already closed or another IOException
+ * occurs.
+ */
+ @Override
+ public int read() throws IOException {
+ byte[] readed = new byte[1];
+ int result = read(readed, 0, 1);
+ return result == -1 ? -1 : readed[0] & 0xff;
+ }
+
+ /**
+ * Reads bytes from the FileInputStream and stores them in byte array
+ * <code>buffer</code>. Answer the number of bytes actually read or -1 if
+ * no bytes were read and end of stream was encountered.
+ *
+ * @param buffer
+ * the byte array in which to store the read bytes.
+ * @return the number of bytes actually read or -1 if end of stream.
+ *
+ * @throws IOException
+ * If the stream is already closed or another IOException
+ * occurs.
+ */
+ @Override
+ public int read(byte[] buffer) throws IOException {
+ return read(buffer, 0, buffer.length);
+ }
+
+ /**
+ * Reads at most <code>count</code> bytes from the FileInputStream and
+ * stores them in byte array <code>buffer</code> starting at
+ * <code>offset</code>. Answer the number of bytes actually read or -1 if
+ * no bytes were read and end of stream was encountered.
+ *
+ * @param buffer
+ * the byte array in which to store the read bytes.
+ * @param offset
+ * the offset in <code>buffer</code> to store the read bytes.
+ * @param count
+ * the maximum number of bytes to store in <code>buffer</code>.
+ * @return the number of bytes actually read or -1 if end of stream.
+ *
+ * @throws IOException
+ * If the stream is already closed or another IOException
+ * occurs.
+ */
+ @Override
+ public int read(byte[] buffer, int offset, int count) throws IOException {
+ if (count > buffer.length - offset || count < 0 || offset < 0) {
+ throw new IndexOutOfBoundsException();
+ }
+ if (0 == count) {
+ return 0;
+ }
+ openCheck();
+ synchronized (repositioningLock) {
+ // stdin requires special handling
+ if (fd == FileDescriptor.in) {
+ return (int) fileSystem.ttyRead(buffer, offset, count);
+ }
+ return (int) fileSystem.read(fd.descriptor, buffer, offset, count);
+ }
+ }
+
+ /**
+ * Skips <code>count</code> number of bytes in this FileInputStream.
+ * Subsequent <code>read()</code>'s will not return these bytes unless
+ * <code>reset()</code> is used. This method may perform multiple reads to
+ * read <code>count</code> bytes.
+ *
+ * @param count
+ * the number of bytes to skip.
+ * @return the number of bytes actually skipped.
+ *
+ * @throws IOException
+ * If the stream is already closed or another IOException
+ * occurs.
+ */
+ @Override
+ public long skip(long count) throws IOException {
+ openCheck();
+
+ if (count == 0) {
+ return 0;
+ }
+ if (count < 0) {
+ // KA013=Number of bytes to skip cannot be negative
+ throw new IOException(Msg.getString("KA013")); //$NON-NLS-1$
+ }
+
+ // stdin requires special handling
+ if (fd == FileDescriptor.in) {
+ // Read and discard count bytes in 8k chunks
+ long skipped = 0, numRead;
+ int chunk = count < 8192 ? (int) count : 8192;
+ byte[] buffer = new byte[chunk];
+ for (long i = count / chunk; i >= 0; i--) {
+ numRead = fileSystem.ttyRead(buffer, 0, chunk);
+ skipped += numRead;
+ if (numRead < chunk) {
+ return skipped;
+ }
+ }
+ return skipped;
+ }
+
+ synchronized (repositioningLock) {
+ final long currentPosition = fileSystem.seek(fd.descriptor, 0L,
+ IFileSystem.SEEK_CUR);
+ final long newPosition = fileSystem.seek(fd.descriptor,
+ currentPosition + count, IFileSystem.SEEK_SET);
+ return newPosition - currentPosition;
+ }
+ }
+
+ private synchronized void openCheck() throws IOException {
+ if (fd.descriptor < 0) {
+ throw new IOException();
+ }
+ }
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileInputStream.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileNotFoundException.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileNotFoundException.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileNotFoundException.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileNotFoundException.java Thu Aug 17 18:45:35 2006
@@ -1,46 +1,46 @@
-/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-
-/**
- * This IO exception is thrown when a file specified by a program cannot be
- * found.
- *
- */
-public class FileNotFoundException extends IOException {
-
- private static final long serialVersionUID = -897856973823710492L;
-
- /**
- * Constructs a new instance of this class with its walkback filled in.
- */
- public FileNotFoundException() {
- super();
- }
-
- /**
- * Constructs a new instance of this class with its walkback and message
- * filled in.
- *
- * @param detailMessage
- * The detail message for the exception.
- */
- public FileNotFoundException(String detailMessage) {
- super(detailMessage);
- }
-
-}
+/* Copyright 1998, 2004 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+/**
+ * This IO exception is thrown when a file specified by a program cannot be
+ * found.
+ *
+ */
+public class FileNotFoundException extends IOException {
+
+ private static final long serialVersionUID = -897856973823710492L;
+
+ /**
+ * Constructs a new instance of this class with its walkback filled in.
+ */
+ public FileNotFoundException() {
+ super();
+ }
+
+ /**
+ * Constructs a new instance of this class with its walkback and message
+ * filled in.
+ *
+ * @param detailMessage
+ * The detail message for the exception.
+ */
+ public FileNotFoundException(String detailMessage) {
+ super(detailMessage);
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileNotFoundException.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java
URL: http://svn.apache.org/viewvc/incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java?rev=432462&r1=432461&r2=432462&view=diff
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java (original)
+++ incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java Thu Aug 17 18:45:35 2006
@@ -1,316 +1,316 @@
-/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package java.io;
-
-
-import java.nio.channels.FileChannel;
-
-import org.apache.harmony.luni.platform.IFileSystem;
-import org.apache.harmony.luni.platform.Platform;
-import org.apache.harmony.nio.FileChannelFactory;
-
-/**
- * FileOutputStream is a class whose underlying stream is represented by a file
- * in the operating system. The bytes that are written to this stream are passed
- * directly to the underlying operating system equivalent function. Since
- * overhead may be high in writing to the OS, FileOutputStreams are usually
- * wrapped with a BufferedOutputStream to reduce the number of times the OS is
- * called.
- * <p>
- * <code>BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("aFile.txt"));</code>
- *
- * @see FileInputStream
- */
-public class FileOutputStream extends OutputStream implements Closeable{
-
- /**
- * The FileDescriptor representing this FileOutputStream.
- */
- FileDescriptor fd;
-
- // The unique file channel associated with this FileInputStream (lazily
- // initialized).
- private FileChannel channel;
-
- private IFileSystem fileSystem = Platform.getFileSystem();
-
- /**
- * Constructs a new FileOutputStream on the File <code>file</code>. If
- * the file exists, it is written over. See the constructor which can append
- * to the file if so desired.
- *
- * @param file
- * the File on which to stream reads.
- *
- * @throws FileNotFoundException
- * If the <code>file</code> cannot be opened for writing.
- *
- * @see java.lang.SecurityManager#checkWrite(FileDescriptor)
- */
- public FileOutputStream(File file) throws FileNotFoundException {
- this(file, false);
- }
-
- /**
- * Constructs a new FileOutputStream on the File <code>file</code>. If
- * the file exists, it is written over. The parameter <code>append</code>
- * determines whether or not the file is opened and appended to or just
- * opened empty.
- *
- * @param file
- * the File on which to stream reads.
- * @param append
- * a boolean indicating whether or not to append to an existing
- * file.
- *
- * @throws FileNotFoundException
- * If the <code>file</code> cannot be opened for writing.
- *
- * @see java.lang.SecurityManager#checkWrite(FileDescriptor)
- * @see java.lang.SecurityManager#checkWrite(String)
- */
- public FileOutputStream(File file, boolean append)
- throws FileNotFoundException {
- super();
- SecurityManager security = System.getSecurityManager();
- if (security != null)
- security.checkWrite(file.getPath());
- fd = new FileDescriptor();
- fd.descriptor = fileSystem.open(file.properPath(true), append?IFileSystem.O_APPEND:IFileSystem.O_WRONLY);
- channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
- append? IFileSystem.O_APPEND:IFileSystem.O_WRONLY);
- }
-
- /**
- * Constructs a new FileOutputStream on the FileDescriptor <code>fd</code>.
- * The file must already be open, therefore no <code>FileIOException</code>
- * will be thrown.
- *
- * @param fd
- * the FileDescriptor on which to stream writes.
- *
- * @see java.lang.SecurityManager#checkWrite(FileDescriptor)
- */
- public FileOutputStream(FileDescriptor fd) {
- super();
- if (fd == null) {
- throw new NullPointerException(org.apache.harmony.luni.util.Msg
- .getString("K006c")); //$NON-NLS-1$
- }
- SecurityManager security = System.getSecurityManager();
- if (security != null)
- security.checkWrite(fd);
- this.fd = fd;
- channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
- IFileSystem.O_WRONLY);
- }
-
- /**
- * Constructs a new FileOutputStream on the file named <code>fileName</code>.
- * If the file exists, it is written over. See the constructor which can
- * append to the file if so desired. The <code>fileName</code> may be
- * absolute or relative to the System property <code>"user.dir"</code>.
- *
- * @param filename
- * the file on which to stream writes.
- *
- * @throws FileNotFoundException
- * If the <code>filename</code> cannot be opened for writing.
- */
- public FileOutputStream(String filename) throws FileNotFoundException {
- this(filename, false);
- }
-
- /**
- * Constructs a new FileOutputStream on the file named <code>filename</code>.
- * If the file exists, it is written over. The parameter <code>append</code>
- * determines whether or not the file is opened and appended to or just
- * opened empty. The <code>filename</code> may be absolute or relative to
- * the System property <code>"user.dir"</code>.
- *
- * @param filename
- * the file on which to stream writes.
- * @param append
- * a boolean indicating whether or not to append to an existing
- * file.
- *
- * @throws FileNotFoundException
- * If the <code>filename</code> cannot be opened for writing.
- */
- public FileOutputStream(String filename, boolean append)
- throws FileNotFoundException {
- this(new File(filename), append);
- }
-
- /**
- * Close the FileOutputStream. This implementation closes the underlying OS
- * resources allocated to represent this stream.
- *
- * @throws IOException
- * If an error occurs attempting to close this FileOutputStream.
- */
- public void close() throws IOException {
- if (fd == null) {
- // if fd is null, then the underlying file is not opened, so nothing
- // to close
- return;
- }
- if (channel == null) {
- /*
- * if channel is null, then the channel doesn't need be taken care
- * of but the underlying file has been opened
- */
- synchronized (this) {
- if (fd.descriptor >= 0) {
- fileSystem.close(fd.descriptor);
- }
- fd.descriptor = -1;
- }
- } else {
- /*
- * if the FileOutputStream is constructed sucessfully, then channel
- * must be closed, which will close the underlying file
- */
- synchronized (channel) {
- synchronized (this) {
- // FIXME: System.in, out, err may not want to be closed?
- if (channel.isOpen() && fd.descriptor >= 0) {
- channel.close();
- }
- fd.descriptor = -1;
- }
- }
- }
- }
-
- /**
- * Frees any resources allocated to represent this FileOutputStream before
- * it is garbage collected. This method is called from the Java Virtual
- * Machine.
- *
- * @throws IOException
- * If an error occurs attempting to finalize this
- * FileOutputStream.
- */
- protected void finalize() throws IOException {
- close();
- }
-
- /**
- * Answers the FileChannel equivalent to this output stream.
- * <p>
- * The file channel is write-only and has an initial position within the
- * file that is the same as the current position of this FileOutputStream
- * within the file. All changes made to the underlying file descriptor state
- * via the channel are visible by the output stream and vice versa.
- * </p>
- *
- * @return the file channel representation for this FileOutputStream.
- */
- public FileChannel getChannel() {
- return channel;
- }
-
- /**
- * Answers a FileDescriptor which represents the lowest level representation
- * of a OS stream resource.
- *
- * @return a FileDescriptor representing this FileOutputStream.
- *
- * @throws IOException
- * If the Stream is already closed and there is no
- * FileDescriptor.
- */
- public final FileDescriptor getFD() throws IOException {
- return fd;
- }
-
- /**
- * Writes the entire contents of the byte array <code>buffer</code> to
- * this FileOutputStream.
- *
- * @param buffer
- * the buffer to be written
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * FileOutputStream.
- */
- public void write(byte[] buffer) throws IOException {
- write(buffer, 0, buffer.length);
- }
-
- /**
- * Writes <code>count</code> <code>bytes</code> from the byte array
- * <code>buffer</code> starting at <code>offset</code> to this
- * FileOutputStream.
- *
- * @param buffer
- * the buffer to be written
- * @param offset
- * offset in buffer to get bytes
- * @param count
- * number of bytes in buffer to write
- *
- * @throws IOException
- * If an error occurs attempting to write to this
- * FileOutputStream.
- * @throws IndexOutOfBoundsException
- * If offset or count are outside of bounds.
- * @throws NullPointerException
- * If buffer is <code>null</code>.
- */
- public void write(byte[] buffer, int offset, int count) throws IOException {
- if (buffer == null) {
- throw new NullPointerException();
- }
- if (count < 0 || offset < 0 || offset > buffer.length
- || count > buffer.length - offset) {
- throw new IndexOutOfBoundsException();
- }
-
- if( count == 0 ) {
- return;
- }
-
- openCheck();
- fileSystem.write(fd.descriptor, buffer, offset, count);
- }
-
- /**
- * Writes the specified byte <code>oneByte</code> to this
- * FileOutputStream. Only the low order byte of <code>oneByte</code> is
- * written.
- *
- * @param oneByte the byte to be written
- *
- * @throws IOException If an error occurs attempting to write to this
- * FileOutputStream.
- */
- public void write(int oneByte) throws IOException {
- openCheck();
- byte[] byteArray = new byte[1];
- byteArray[0] = (byte)oneByte;
- fileSystem.write(fd.descriptor, byteArray, 0, 1);
- }
-
- private synchronized void openCheck() throws IOException {
- if (fd.descriptor < 0) {
- throw new IOException();
- }
- }
-
-}
+/* Copyright 1998, 2006 The Apache Software Foundation or its licensors, as applicable
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package java.io;
+
+
+import java.nio.channels.FileChannel;
+
+import org.apache.harmony.luni.platform.IFileSystem;
+import org.apache.harmony.luni.platform.Platform;
+import org.apache.harmony.nio.FileChannelFactory;
+
+/**
+ * FileOutputStream is a class whose underlying stream is represented by a file
+ * in the operating system. The bytes that are written to this stream are passed
+ * directly to the underlying operating system equivalent function. Since
+ * overhead may be high in writing to the OS, FileOutputStreams are usually
+ * wrapped with a BufferedOutputStream to reduce the number of times the OS is
+ * called.
+ * <p>
+ * <code>BufferedOutputStream buf = new BufferedOutputStream(new FileOutputStream("aFile.txt"));</code>
+ *
+ * @see FileInputStream
+ */
+public class FileOutputStream extends OutputStream implements Closeable{
+
+ /**
+ * The FileDescriptor representing this FileOutputStream.
+ */
+ FileDescriptor fd;
+
+ // The unique file channel associated with this FileInputStream (lazily
+ // initialized).
+ private FileChannel channel;
+
+ private IFileSystem fileSystem = Platform.getFileSystem();
+
+ /**
+ * Constructs a new FileOutputStream on the File <code>file</code>. If
+ * the file exists, it is written over. See the constructor which can append
+ * to the file if so desired.
+ *
+ * @param file
+ * the File on which to stream reads.
+ *
+ * @throws FileNotFoundException
+ * If the <code>file</code> cannot be opened for writing.
+ *
+ * @see java.lang.SecurityManager#checkWrite(FileDescriptor)
+ */
+ public FileOutputStream(File file) throws FileNotFoundException {
+ this(file, false);
+ }
+
+ /**
+ * Constructs a new FileOutputStream on the File <code>file</code>. If
+ * the file exists, it is written over. The parameter <code>append</code>
+ * determines whether or not the file is opened and appended to or just
+ * opened empty.
+ *
+ * @param file
+ * the File on which to stream reads.
+ * @param append
+ * a boolean indicating whether or not to append to an existing
+ * file.
+ *
+ * @throws FileNotFoundException
+ * If the <code>file</code> cannot be opened for writing.
+ *
+ * @see java.lang.SecurityManager#checkWrite(FileDescriptor)
+ * @see java.lang.SecurityManager#checkWrite(String)
+ */
+ public FileOutputStream(File file, boolean append)
+ throws FileNotFoundException {
+ super();
+ SecurityManager security = System.getSecurityManager();
+ if (security != null)
+ security.checkWrite(file.getPath());
+ fd = new FileDescriptor();
+ fd.descriptor = fileSystem.open(file.properPath(true), append?IFileSystem.O_APPEND:IFileSystem.O_WRONLY);
+ channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+ append? IFileSystem.O_APPEND:IFileSystem.O_WRONLY);
+ }
+
+ /**
+ * Constructs a new FileOutputStream on the FileDescriptor <code>fd</code>.
+ * The file must already be open, therefore no <code>FileIOException</code>
+ * will be thrown.
+ *
+ * @param fd
+ * the FileDescriptor on which to stream writes.
+ *
+ * @see java.lang.SecurityManager#checkWrite(FileDescriptor)
+ */
+ public FileOutputStream(FileDescriptor fd) {
+ super();
+ if (fd == null) {
+ throw new NullPointerException(org.apache.harmony.luni.util.Msg
+ .getString("K006c")); //$NON-NLS-1$
+ }
+ SecurityManager security = System.getSecurityManager();
+ if (security != null)
+ security.checkWrite(fd);
+ this.fd = fd;
+ channel = FileChannelFactory.getFileChannel(this, fd.descriptor,
+ IFileSystem.O_WRONLY);
+ }
+
+ /**
+ * Constructs a new FileOutputStream on the file named <code>fileName</code>.
+ * If the file exists, it is written over. See the constructor which can
+ * append to the file if so desired. The <code>fileName</code> may be
+ * absolute or relative to the System property <code>"user.dir"</code>.
+ *
+ * @param filename
+ * the file on which to stream writes.
+ *
+ * @throws FileNotFoundException
+ * If the <code>filename</code> cannot be opened for writing.
+ */
+ public FileOutputStream(String filename) throws FileNotFoundException {
+ this(filename, false);
+ }
+
+ /**
+ * Constructs a new FileOutputStream on the file named <code>filename</code>.
+ * If the file exists, it is written over. The parameter <code>append</code>
+ * determines whether or not the file is opened and appended to or just
+ * opened empty. The <code>filename</code> may be absolute or relative to
+ * the System property <code>"user.dir"</code>.
+ *
+ * @param filename
+ * the file on which to stream writes.
+ * @param append
+ * a boolean indicating whether or not to append to an existing
+ * file.
+ *
+ * @throws FileNotFoundException
+ * If the <code>filename</code> cannot be opened for writing.
+ */
+ public FileOutputStream(String filename, boolean append)
+ throws FileNotFoundException {
+ this(new File(filename), append);
+ }
+
+ /**
+ * Close the FileOutputStream. This implementation closes the underlying OS
+ * resources allocated to represent this stream.
+ *
+ * @throws IOException
+ * If an error occurs attempting to close this FileOutputStream.
+ */
+ public void close() throws IOException {
+ if (fd == null) {
+ // if fd is null, then the underlying file is not opened, so nothing
+ // to close
+ return;
+ }
+ if (channel == null) {
+ /*
+ * if channel is null, then the channel doesn't need be taken care
+ * of but the underlying file has been opened
+ */
+ synchronized (this) {
+ if (fd.descriptor >= 0) {
+ fileSystem.close(fd.descriptor);
+ }
+ fd.descriptor = -1;
+ }
+ } else {
+ /*
+ * if the FileOutputStream is constructed sucessfully, then channel
+ * must be closed, which will close the underlying file
+ */
+ synchronized (channel) {
+ synchronized (this) {
+ // FIXME: System.in, out, err may not want to be closed?
+ if (channel.isOpen() && fd.descriptor >= 0) {
+ channel.close();
+ }
+ fd.descriptor = -1;
+ }
+ }
+ }
+ }
+
+ /**
+ * Frees any resources allocated to represent this FileOutputStream before
+ * it is garbage collected. This method is called from the Java Virtual
+ * Machine.
+ *
+ * @throws IOException
+ * If an error occurs attempting to finalize this
+ * FileOutputStream.
+ */
+ protected void finalize() throws IOException {
+ close();
+ }
+
+ /**
+ * Answers the FileChannel equivalent to this output stream.
+ * <p>
+ * The file channel is write-only and has an initial position within the
+ * file that is the same as the current position of this FileOutputStream
+ * within the file. All changes made to the underlying file descriptor state
+ * via the channel are visible by the output stream and vice versa.
+ * </p>
+ *
+ * @return the file channel representation for this FileOutputStream.
+ */
+ public FileChannel getChannel() {
+ return channel;
+ }
+
+ /**
+ * Answers a FileDescriptor which represents the lowest level representation
+ * of a OS stream resource.
+ *
+ * @return a FileDescriptor representing this FileOutputStream.
+ *
+ * @throws IOException
+ * If the Stream is already closed and there is no
+ * FileDescriptor.
+ */
+ public final FileDescriptor getFD() throws IOException {
+ return fd;
+ }
+
+ /**
+ * Writes the entire contents of the byte array <code>buffer</code> to
+ * this FileOutputStream.
+ *
+ * @param buffer
+ * the buffer to be written
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * FileOutputStream.
+ */
+ public void write(byte[] buffer) throws IOException {
+ write(buffer, 0, buffer.length);
+ }
+
+ /**
+ * Writes <code>count</code> <code>bytes</code> from the byte array
+ * <code>buffer</code> starting at <code>offset</code> to this
+ * FileOutputStream.
+ *
+ * @param buffer
+ * the buffer to be written
+ * @param offset
+ * offset in buffer to get bytes
+ * @param count
+ * number of bytes in buffer to write
+ *
+ * @throws IOException
+ * If an error occurs attempting to write to this
+ * FileOutputStream.
+ * @throws IndexOutOfBoundsException
+ * If offset or count are outside of bounds.
+ * @throws NullPointerException
+ * If buffer is <code>null</code>.
+ */
+ public void write(byte[] buffer, int offset, int count) throws IOException {
+ if (buffer == null) {
+ throw new NullPointerException();
+ }
+ if (count < 0 || offset < 0 || offset > buffer.length
+ || count > buffer.length - offset) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ if( count == 0 ) {
+ return;
+ }
+
+ openCheck();
+ fileSystem.write(fd.descriptor, buffer, offset, count);
+ }
+
+ /**
+ * Writes the specified byte <code>oneByte</code> to this
+ * FileOutputStream. Only the low order byte of <code>oneByte</code> is
+ * written.
+ *
+ * @param oneByte the byte to be written
+ *
+ * @throws IOException If an error occurs attempting to write to this
+ * FileOutputStream.
+ */
+ public void write(int oneByte) throws IOException {
+ openCheck();
+ byte[] byteArray = new byte[1];
+ byteArray[0] = (byte)oneByte;
+ fileSystem.write(fd.descriptor, byteArray, 0, 1);
+ }
+
+ private synchronized void openCheck() throws IOException {
+ if (fd.descriptor < 0) {
+ throw new IOException();
+ }
+ }
+
+}
Propchange: incubator/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/FileOutputStream.java
------------------------------------------------------------------------------
svn:eol-style = native