jackrabbit-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ju...@apache.org
Subject svn commit: r789774 - in /jackrabbit/branches/1.x: ./ jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/ jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/
Date Tue, 30 Jun 2009 14:55:12 GMT
Author: jukka
Date: Tue Jun 30 14:55:12 2009
New Revision: 789774

URL: http://svn.apache.org/viewvc?rev=789774&view=rev
Log:
1.x: Merged revision 779081 (JCR-2067)

Added:
    jackrabbit/branches/1.x/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/LazyFileInputStreamTest.java
      - copied unchanged from r779081, jackrabbit/trunk/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/LazyFileInputStreamTest.java
Modified:
    jackrabbit/branches/1.x/   (props changed)
    jackrabbit/branches/1.x/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/LazyFileInputStream.java
    jackrabbit/branches/1.x/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java

Propchange: jackrabbit/branches/1.x/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Tue Jun 30 14:55:12 2009
@@ -1 +1 @@
-/jackrabbit/trunk:770143-773197,773483,773525-773554,773584,773588,773828,773835-775756,775833,775836,775840,775868,775981,775986,776036,776256,776310,776313,776321-776322,776332,776356-776357,776362,776373,776650-776693,776737,776757,776776-776777,777024,777029,777034,777478,777509,777541,777548,777936,778445,778613,778616,778621,778645,778720,778802,779032,779040,779068-779071,779074,779076
+/jackrabbit/trunk:770143-773197,773483,773525-773554,773584,773588,773828,773835-775756,775833,775836,775840,775868,775981,775986,776036,776256,776310,776313,776321-776322,776332,776356-776357,776362,776373,776650-776693,776737,776757,776776-776777,777024,777029,777034,777478,777509,777541,777548,777936,778445,778613,778616,778621,778645,778720,778802,779032,779040,779068-779071,779074,779076,779081

Modified: jackrabbit/branches/1.x/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/LazyFileInputStream.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.x/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/LazyFileInputStream.java?rev=789774&r1=789773&r2=789774&view=diff
==============================================================================
--- jackrabbit/branches/1.x/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/LazyFileInputStream.java
(original)
+++ jackrabbit/branches/1.x/jackrabbit-core/src/main/java/org/apache/jackrabbit/core/data/LazyFileInputStream.java
Tue Jun 30 14:55:12 2009
@@ -17,13 +17,12 @@
 package org.apache.jackrabbit.core.data;
 
 import java.io.File;
+import java.io.FileDescriptor;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 
 import org.apache.commons.io.input.AutoCloseInputStream;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
 
 /**
  * This input stream delays opening the file until the first byte is read, and
@@ -32,7 +31,10 @@
  */
 public class LazyFileInputStream extends AutoCloseInputStream {
 
-    private static Logger log = LoggerFactory.getLogger(LazyFileInputStream.class);
+    /**
+     * The file descriptor to use.
+     */
+    protected final FileDescriptor fd;
 
     /**
      * The file to read from.
@@ -47,83 +49,119 @@
     protected boolean opened;
 
     /**
-     * Create a lazy input stream for the given file.
+     * Creates a new <code>LazyFileInputStream</code> for the given file. If
the
+     * file is unreadable, a FileNotFoundException is thrown.
      * The file is not opened until the first byte is read from the stream.
-     * 
+     *
      * @param file the file
+     * @throws java.io.FileNotFoundException
      */
-    public LazyFileInputStream(File file) throws FileNotFoundException {
+    public LazyFileInputStream(File file)
+            throws FileNotFoundException {
         super(null);
         if (!file.canRead()) {
             throw new FileNotFoundException(file.getPath());
-        }        
+        }
         this.file = file;
+        this.fd = null;
+    }
+
+    /**
+     * Creates a new <code>LazyFileInputStream</code> for the given file
+     * descriptor.
+     * The file is not opened until the first byte is read from the stream.
+     *
+     * @param fdObj
+     */
+    public LazyFileInputStream(FileDescriptor fd) {
+        super(null);
+        this.file = null;
+        this.fd = fd;
+    }
+
+    /**
+     * Creates a new <code>LazyFileInputStream</code> for the given file. If
the
+     * file is unreadable, a FileNotFoundException is thrown.
+     *
+     * @param name
+     * @throws java.io.FileNotFoundException
+     */
+    public LazyFileInputStream(String name) throws FileNotFoundException {
+        this(new File(name));
     }
 
     /**
      * Open the stream if required.
-     * 
-     * @throws IOException
+     *
+     * @throws java.io.IOException
      */
-    protected void openStream() throws IOException {
+    protected void open() throws IOException {
         if (!opened) {
             opened = true;
-            in = new FileInputStream(file);
+            if (fd != null) {
+                in = new FileInputStream(fd);
+            } else {
+                in = new FileInputStream(file);
+            }
         }
     }
-    
+
     public int read() throws IOException {
-        openStream();
+        open();
         return super.read();
     }
 
-    public int read(byte[] b) throws IOException {
-        return read(b, 0, b.length);
-    }
-
-    public int read(byte[] b, int off, int len) throws IOException {
-        openStream();
-        return super.read(b, off, len);
+    public int available() throws IOException {
+        open();
+        return super.available();
     }
 
     public void close() throws IOException {
         // make sure the file is not opened afterwards
         opened = true;
-        super.close();
+        
+        // only close the file if it was in fact opened
+        if (in != null) {
+            super.close();
+        }
     }
 
-    public long skip(long n) throws IOException {
-        openStream();
-        return super.skip(n);
+    public synchronized void reset() throws IOException {
+        open();
+        super.reset();
     }
 
-    public int available() throws IOException {
-        openStream();
-        return super.available();
+    public boolean markSupported() {
+        try {
+            open();
+        } catch (IOException e) {
+            throw new IllegalStateException(e.toString());
+        }
+        return super.markSupported();
     }
 
-    public void mark(int readlimit) {
+    public synchronized void mark(int readlimit) {
         try {
-            openStream();
+            open();
         } catch (IOException e) {
-            log.info("Error getting underlying stream: ", e);
+            throw new IllegalStateException(e.toString());
         }
         super.mark(readlimit);
     }
 
-    public void reset() throws IOException {
-        openStream();
-        super.reset();
+    public long skip(long n) throws IOException {
+        open();
+        return super.skip(n);
     }
 
-    public boolean markSupported() {
-        try {
-            openStream();
-        } catch (IOException e) {
-            log.info("Error getting underlying stream: ", e);
-            return false;
-        }
-        return super.markSupported();
+    public int read(byte[] b) throws IOException {
+        open();
+        return super.read(b, 0, b.length);
+    }
+
+    public int read(byte[] b, int off, int len) throws IOException {
+        open();
+        return super.read(b, off, len);
     }
 
 }

Modified: jackrabbit/branches/1.x/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java
URL: http://svn.apache.org/viewvc/jackrabbit/branches/1.x/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java?rev=789774&r1=789773&r2=789774&view=diff
==============================================================================
--- jackrabbit/branches/1.x/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java
(original)
+++ jackrabbit/branches/1.x/jackrabbit-core/src/test/java/org/apache/jackrabbit/core/data/TestAll.java
Tue Jun 30 14:55:12 2009
@@ -32,6 +32,7 @@
      */
     public static Test suite() {
         TestSuite suite = new TestSuite("Data tests");
+        suite.addTestSuite(LazyFileInputStreamTest.class);
         suite.addTestSuite(OpenFilesTest.class);
         suite.addTestSuite(DataStoreTest.class);
         suite.addTestSuite(NodeTypeTest.class);



Mime
View raw message