You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/10/09 06:54:48 UTC

svn commit: r823410 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/io/ main/native/os/unix/ test/org/apache/commons/runtime/

Author: mturk
Date: Fri Oct  9 04:54:47 2009
New Revision: 823410

URL: http://svn.apache.org/viewvc?rev=823410&view=rev
Log:
Add standard files support

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSeekMethod.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java
    commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java?rev=823410&r1=823409&r2=823410&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileInstance.java Fri Oct  9 04:54:47 2009
@@ -41,19 +41,6 @@
  */
 public class FileInstance implements Closeable, Flushable
 {
-    /**
-     * Seek from the current position.
-     */
-    public static final int SEEK_CUR = 0;
-    /**
-     * Seek from the file begin
-     */
-    public static final int SEEK_SET = 1;
-    /**
-     * Seek from the currect end-of-file position
-     */
-    public static final int SEEK_END = 2;
-
     /* File's descriptor object
      */
     private Descriptor fd;
@@ -144,7 +131,7 @@
         }
     }
 
-    /** Create new random access file object from the {@code fd}.
+    /** Create new FileInstance object from the {@code fd}.
      */
     public FileInstance(Descriptor fd)
     {
@@ -152,6 +139,68 @@
     }
 
     /**
+     * The "standard" input stream {@code FileInstance}.
+     */
+    public static final FileInstance STDIN;
+    /**
+     * The "standard" output stream {@code FileInstance}.
+     */
+    public static final FileInstance STDOUT;
+    /**
+     * The "standard" error stream {@code FileInstance}.
+     */
+    public static final FileInstance STDERR;
+
+    static {
+        /* Initialize the "standard" FileInstances.
+         * Note that those descriptors can be NULL in
+         * case the FileWrapper.open call fails.
+         */
+        STDIN  = new FileInstance(FileWrapper.open(0, null));
+        STDOUT = new FileInstance(FileWrapper.open(1, null));
+        STDERR = new FileInstance(FileWrapper.open(2, null));
+    }
+
+    /**
+     * Create new {@code FileInstance} that is bound to the spacified standard
+     * I/O device (standard input, standard output, or standard error).
+     * <p>
+     * FileInstance returned can be used by applications that need to
+     * read from or write to the console. If the standard handle is redirected
+     * the returned {@code FileInstance} is pointer to that redirected file.
+     * </p>
+     * <p>
+     * If the application doesn't have standard handles (e.g. daemon applicaton)
+     * and the handles are not redirected, the returned {@code FileInstance}
+     * contains invalid file {@code Descriptor}. Any further operation on such
+     * {@code FileInstance} will fail with {@link ClosedDescriptorException}.
+     * </p>
+     * <p>
+     * Parameter {@code which} determines which one of the stadard streams to
+     * open and can have the following values:
+     * <p>
+     * <pre>
+     * 0    "standard" input stream.
+     * 1    "standard" output stream.
+     * 2    "standard" error stream.
+     * </pre>
+     * @param which standard stream to open.
+     *
+     * @return new {@code FileInstance} connected to standard stream.
+     *
+     */
+    public static FileInstance openStdStream(int which,
+                                             EnumSet<FileOpenMode> mode)
+        throws IllegalArgumentException
+    {
+        if (which < 0 || which > 2) {
+            throw new IllegalArgumentException();
+        }
+        Descriptor fd = FileWrapper.open(which, mode);
+        return new FileInstance(fd);
+    }
+
+    /**
      * Clear the file errors.
      * <p>
      * Method clears all pending file errors and resets the
@@ -421,28 +470,22 @@
             throw new IllegalArgumentException();
         }
         synchronized (sync) {
-            FileWrapper.seek(fd, SEEK_SET, pos);
+            FileWrapper.seek(fd, FileSeekMethod.SET.valueOf(), pos);
         }
     }
 
     /**
      * Moves this file's file pointer to a new position, from where following
      * {@code read}, {@code write} or {@code skip} operations are done.
-     * Depending on the {@code moveMethod} the starting point for the
-     * file pointer move can be one of the following values:
-     * <pre>
-     * SEEK_SET    The starting point is zero (0) or the beginning of the file.
-     * SEEK_CUR    The starting point is the current value of the file pointer.
-     * SEEK_END    The starting point is the current end-of-file position.
-     * <pre>
+     *
      * @param moveMethod
      *          Starting point for the file pointer move.
      * @param off
      *          Move offset in bytes.
      *
      * @throws IllegalArgumentException
-     *          If {@code moveMethod < SEEK_CUR} or
-     *          {@code moveMethod > SEEK_END}.
+     *          If {@code moveMethod == FileSeekMethod.SET} and
+     *          {@code off < 0}.
      * @throws ClosedDescriptorException
      *          If this file is closed.
      * @throws AsyncClosedDescriptorException
@@ -450,20 +493,17 @@
      *          operation is in progress.
      * @throws IOException
      *          If some other I/O error occurs.
+     * @see FileSeekMethod
      */
-    public void moveFilePointer(int moveMethod, long off)
+    public void moveFilePointer(FileSeekMethod moveMethod, long off)
         throws IOException
     {
-        if (moveMethod < SEEK_CUR || moveMethod > SEEK_END) {
-            // move method is invalid
-            throw new IllegalArgumentException();
-        }
-        if (moveMethod == SEEK_SET && off < 0) {
+        if (moveMethod == FileSeekMethod.SET && off < 0L) {
             // seek position is negative
             throw new IllegalArgumentException();
         }
         synchronized (sync) {
-            FileWrapper.seek(fd, moveMethod, off);
+            FileWrapper.seek(fd, moveMethod.valueOf(), off);
         }
     }
 
@@ -491,11 +531,11 @@
     {
         if (count > 0) {
             synchronized (sync) {
-                long cur = FileWrapper.seek(fd, SEEK_CUR, 0L);
-                long eof = FileWrapper.seek(fd, SEEK_END, 0L);
+                long cur = FileWrapper.seek(fd, FileSeekMethod.CUR.valueOf(), 0L);
+                long eof = FileWrapper.seek(fd, FileSeekMethod.END.valueOf(), 0L);
 
                 int cnt = (int)((cur + count > eof) ? eof - cur : count);
-                FileWrapper.seek(fd, SEEK_SET, cnt);
+                FileWrapper.seek(fd, FileSeekMethod.SET.valueOf(), cnt);
                 return cnt;
             }
         }
@@ -519,7 +559,7 @@
     public long getFilePointer()
         throws IOException
     {
-        return FileWrapper.seek(fd, SEEK_CUR, 0L);
+        return FileWrapper.seek(fd, FileSeekMethod.CUR.valueOf(), 0L);
     }
 
     /**
@@ -539,10 +579,10 @@
         throws IOException
     {
         synchronized (sync) {
-            long cur = FileWrapper.seek(fd, SEEK_CUR, 0L);
-            long end = FileWrapper.seek(fd, SEEK_END, 0L);
+            long cur = FileWrapper.seek(fd, FileSeekMethod.CUR.valueOf(), 0L);
+            long end = FileWrapper.seek(fd, FileSeekMethod.END.valueOf(), 0L);
 
-            FileWrapper.seek(fd, SEEK_SET, cur);
+            FileWrapper.seek(fd, FileSeekMethod.SET.valueOf(), cur);
             return end;
         }
     }

Added: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSeekMethod.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSeekMethod.java?rev=823410&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSeekMethod.java (added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSeekMethod.java Fri Oct  9 04:54:47 2009
@@ -0,0 +1,43 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 org.apache.commons.runtime.io;
+
+/**
+ * FileSeekMethod determines how the seek operation
+ * should be performed.
+ */
+public enum FileSeekMethod
+{
+    /** Seek from the current position. */
+    CUR(    0),
+     /** Seek from the file begin. */
+    SET(    1),
+    /** Seek from the currect end-of-file position. */
+    END(    2);
+
+    private int value;
+    private FileSeekMethod(int v)
+    {
+        value = v;
+    }
+
+    public int valueOf()
+    {
+        return value;
+    }
+
+}

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileSeekMethod.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java?rev=823410&r1=823409&r2=823410&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/io/FileWrapper.java Fri Oct  9 04:54:47 2009
@@ -185,6 +185,32 @@
         return open1(path.getPath(), imode, iprot);
     }
 
+    private static native Descriptor open2(int which, int mode)
+        throws IOException, SecurityException;
+    /**
+     * Open standart file Descriptor.
+     * <p>
+     * The underlying OS file must be closed by {@code Descriptor.close()}.
+     * </p>
+     *
+     * @return opened file descriptor.
+     * @throws IOException on error.
+     */
+    public static Descriptor open(int which, EnumSet<FileOpenMode> mode)
+    {
+        int imode = 0;
+        if (mode != null) {
+            imode = FileOpenMode.bitmapOf(mode);
+        }
+        try {
+            // Call the native method.
+            return open2(which, imode);
+        } catch (Exception ex) {
+            // Ignore
+        }
+        return Descriptor.NULL;
+    }
+
     private static native int lock0(int fd, int type)
         throws IOException;
     /**

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c?rev=823410&r1=823409&r2=823410&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/fsysio.c Fri Oct  9 04:54:47 2009
@@ -180,7 +180,7 @@
     if (!(flags & ACR_FOPEN_NOCLEANUP))
         oflags |= O_CLOEXEC;
 #endif
-#if defined(_LARGEFILE64_SOURCE)
+#if defined(_LARGEFILE64_SOURCE) && defined(O_LARGEFILE)
     oflags |= O_LARGEFILE;
 #endif
     if (prot == ACR_FPROT_OS_DEFAULT)

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java?rev=823410&r1=823409&r2=823410&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestFileSys.java Fri Oct  9 04:54:47 2009
@@ -120,5 +120,17 @@
         f.close();
     }
 
+    public void testFileStdout()
+        throws Exception
+    {
+        byte [] buf = { (byte)'H', (byte)'i', (byte)'\n' };
+
+        assertEquals("FileType", FileType.PIPE,
+                                 FileInstance.STDOUT.getFileType());
+        int wr = FileInstance.STDOUT.write(buf);
+        assertEquals("Written", 3, wr);
+    }
+
+
 }