You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by im...@apache.org on 2004/06/17 21:29:29 UTC
cvs commit: jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb SmbFileRandomAccessContent.java SmbFileObject.java SmbFileProvider.java
imario 2004/06/17 12:29:29
Modified: vfs/src/java/org/apache/commons/vfs/provider
AbstractFileObject.java DefaultFileContent.java
vfs build.xml project.xml
vfs/src/java/org/apache/commons/vfs Capability.java
FileContent.java Resources.properties
vfs/src/java/org/apache/commons/vfs/provider/local
DefaultLocalFileProvider.java LocalFile.java
vfs/src/java/org/apache/commons/vfs/provider/http
HttpFileObject.java HttpFileProvider.java
vfs/src/java/org/apache/commons/vfs/util
MonitorInputStream.java
vfs/src/test/org/apache/commons/vfs/test
ProviderTestSuite.java UrlStructureTests.java
vfs/src/java/org/apache/commons/vfs/provider/smb
SmbFileObject.java SmbFileProvider.java
Added: vfs/src/java/org/apache/commons/vfs/provider
AbstractRandomAccessContent.java
vfs/src/java/org/apache/commons/vfs RandomAccessContent.java
vfs/src/java/org/apache/commons/vfs/provider/local
LocalFileRandomAccessContent.java
vfs/src/java/org/apache/commons/vfs/provider/http
HttpRandomAccesContent.java
vfs/src/java/org/apache/commons/vfs/util
MonitorRandomAccessContent.java
RandomAccessMode.java
vfs/src/test/org/apache/commons/vfs/test
ProviderRandomReadTests.java
ProviderRandomReadWriteTests.java
vfs/src/java/org/apache/commons/vfs/provider/smb
SmbFileRandomAccessContent.java
Log:
RandomAccess
Random read/write for local and smb (jcifs) files.
Random read for http.
Use file.getContent().getRandomAccessContent(RandomAccessMode)
to get access to it.
As the jdk RandomAccessFile the DataInput/DataOutput interfaces are implemented.
Revision Changes Path
1.46 +62 -0 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java
Index: AbstractFileObject.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractFileObject.java,v
retrieving revision 1.45
retrieving revision 1.46
diff -u -r1.45 -r1.46
--- AbstractFileObject.java 3 Jun 2004 17:13:24 -0000 1.45
+++ AbstractFileObject.java 17 Jun 2004 19:29:28 -0000 1.46
@@ -27,7 +27,9 @@
import org.apache.commons.vfs.FileUtil;
import org.apache.commons.vfs.GlobalConfiguration;
import org.apache.commons.vfs.NameScope;
+import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.Selectors;
+import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.IOException;
import java.io.InputStream;
@@ -296,6 +298,19 @@
protected abstract InputStream doGetInputStream() throws Exception;
/**
+ * Creates access to the file for random i/o. Is only called
+ * if {@link #doGetType} returns {@link FileType#FILE}.
+ * <p/>
+ * <p>It is guaranteed that there are no open output streams for this file
+ * when this method is called.
+ * <p/>
+ */
+ protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception
+ {
+ throw new FileSystemException("vfs.provider/random-access-not-supported.error");
+ }
+
+ /**
* Creates an output stream to write the file content to. Is only
* called if:
* <ul>
@@ -953,6 +968,53 @@
catch (final Exception exc)
{
throw new FileSystemException("vfs.provider/read.error", name, exc);
+ }
+ }
+
+ /**
+ * Returns an input/output stream to use to read and write the content of the file in and
+ * random manner.
+ */
+ public RandomAccessContent getRandomAccessContent(final RandomAccessMode mode) throws FileSystemException
+ {
+ attach();
+ if (!type.hasContent())
+ {
+ throw new FileSystemException("vfs.provider/read-not-file.error", name);
+ }
+
+ if (mode.requestRead())
+ {
+ if (!getFileSystem().hasCapability(Capability.RANDOM_ACCESS_READ))
+ {
+ throw new FileSystemException("vfs.provider/random-access-read-not-supported.error");
+ }
+ if (!isReadable())
+ {
+ throw new FileSystemException("vfs.provider/read-not-readable.error", name);
+ }
+ }
+
+ if (mode.requestWrite())
+ {
+ if (!getFileSystem().hasCapability(Capability.RANDOM_ACCESS_WRITE))
+ {
+ throw new FileSystemException("vfs.provider/random-access-write-not-supported.error");
+ }
+ if (!isWriteable())
+ {
+ throw new FileSystemException("vfs.provider/write-read-only.error", name);
+ }
+ }
+
+ // Get the raw input stream
+ try
+ {
+ return doGetRandomAccessContent(mode);
+ }
+ catch (final Exception exc)
+ {
+ throw new FileSystemException("vfs.provider/random-access.error", name, exc);
}
}
1.22 +68 -9 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultFileContent.java
Index: DefaultFileContent.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/DefaultFileContent.java,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -r1.21 -r1.22
--- DefaultFileContent.java 21 May 2004 20:43:19 -0000 1.21
+++ DefaultFileContent.java 17 Jun 2004 19:29:28 -0000 1.22
@@ -20,8 +20,11 @@
import org.apache.commons.vfs.FileContentInfoFactory;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
+import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.util.MonitorInputStream;
import org.apache.commons.vfs.util.MonitorOutputStream;
+import org.apache.commons.vfs.util.MonitorRandomAccessContent;
+import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.IOException;
import java.io.InputStream;
@@ -44,6 +47,7 @@
private static final int STATE_NONE = 0;
private static final int STATE_READING = 1;
private static final int STATE_WRITING = 2;
+ private static final int STATE_RANDOM_ACCESS = 3;
private final AbstractFileObject file;
private int state = STATE_NONE;
@@ -53,6 +57,7 @@
private Map roAttrs;
private FileContentInfo fileContentInfo;
private final FileContentInfoFactory fileContentInfoFactory;
+ private RandomAccessContent rastr;
public DefaultFileContent(final AbstractFileObject file, final FileContentInfoFactory fileContentInfoFactory)
{
@@ -78,7 +83,7 @@
{
throw new FileSystemException("vfs.provider/get-size-not-file.error", file);
}
- if (state == STATE_WRITING)
+ if (state == STATE_WRITING || state == STATE_RANDOM_ACCESS)
{
throw new FileSystemException("vfs.provider/get-size-write.error", file);
}
@@ -99,7 +104,7 @@
*/
public long getLastModifiedTime() throws FileSystemException
{
- if (state == STATE_WRITING)
+ if (state == STATE_WRITING || state == STATE_RANDOM_ACCESS)
{
throw new FileSystemException("vfs.provider/get-last-modified-writing.error", file);
}
@@ -122,7 +127,7 @@
*/
public void setLastModifiedTime(final long modTime) throws FileSystemException
{
- if (state == STATE_WRITING)
+ if (state == STATE_WRITING || state == STATE_RANDOM_ACCESS)
{
throw new FileSystemException("vfs.provider/set-last-modified-writing.error", file);
}
@@ -218,7 +223,7 @@
{
throw new FileSystemException("vfs.provider/get-certificates-no-exist.error", file);
}
- if (state == STATE_WRITING)
+ if (state == STATE_WRITING || state == STATE_RANDOM_ACCESS)
{
throw new FileSystemException("vfs.provider/get-certificates-writing.error", file);
}
@@ -246,7 +251,7 @@
*/
public InputStream getInputStream() throws FileSystemException
{
- if (state == STATE_WRITING)
+ if (state == STATE_WRITING || state == STATE_RANDOM_ACCESS)
{
throw new FileSystemException("vfs.provider/read-in-use.error", file);
}
@@ -260,6 +265,24 @@
}
/**
+ * Returns an input/output stream to use to read and write the content of the file in an
+ * random manner.
+ */
+ public RandomAccessContent getRandomAccessContent(final RandomAccessMode mode) throws FileSystemException
+ {
+ if (state != STATE_NONE)
+ {
+ throw new FileSystemException("vfs.provider/read-in-use.error", file);
+ }
+
+ // Get the content
+ final RandomAccessContent rastr = file.getRandomAccessContent(mode);
+ this.rastr = new FileRandomAccessContent(rastr);
+ state = STATE_RANDOM_ACCESS;
+ return this.rastr;
+ }
+
+ /**
* Returns an output stream for writing the content.
*/
public OutputStream getOutputStream() throws FileSystemException
@@ -306,6 +329,19 @@
{
outstr.close();
}
+
+ // Close the random access stream
+ if (rastr != null)
+ {
+ try
+ {
+ rastr.close();
+ }
+ catch (IOException e)
+ {
+ throw new FileSystemException(e);
+ }
+ }
}
finally
{
@@ -326,6 +362,14 @@
}
/**
+ * Handles the end of random access.
+ */
+ private void endRandomAccess()
+ {
+ state = STATE_NONE;
+ }
+
+ /**
* Handles the end of output stream.
*/
private void endOutput() throws Exception
@@ -382,6 +426,25 @@
}
/**
+ * An input/output stream for reading/writing content on random positions
+ */
+ private final class FileRandomAccessContent extends MonitorRandomAccessContent
+ {
+ FileRandomAccessContent(final RandomAccessContent content)
+ {
+ super(content);
+ }
+
+ /**
+ * Called after the stream has been closed.
+ */
+ protected void onClose() throws IOException
+ {
+ endRandomAccess();
+ }
+ }
+
+ /**
* An output stream for writing content.
*/
private final class FileContentOutputStream
@@ -400,10 +463,6 @@
try
{
super.close();
- }
- catch (final FileSystemException e)
- {
- throw e;
}
catch (final IOException e)
{
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/AbstractRandomAccessContent.java
Index: AbstractRandomAccessContent.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs.provider;
import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.IOException;
/**
* Implements the DataOutput part of the RandomAccessContent interface and throws
* UnsupportedOperationException if one of those methods are called.
* (for read-only random access implementations)
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:28 $
*/
public abstract class AbstractRandomAccessContent implements RandomAccessContent
{
private final RandomAccessMode mode;
protected AbstractRandomAccessContent(final RandomAccessMode mode)
{
this.mode = mode;
}
public void writeDouble(double v) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeFloat(float v) throws IOException
{
throw new UnsupportedOperationException();
}
public void write(int b) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeByte(int v) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeChar(int v) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeInt(int v) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeShort(int v) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeLong(long v) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeBoolean(boolean v) throws IOException
{
throw new UnsupportedOperationException();
}
public void write(byte b[]) throws IOException
{
throw new UnsupportedOperationException();
}
public void write(byte b[], int off, int len) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeBytes(String s) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeChars(String s) throws IOException
{
throw new UnsupportedOperationException();
}
public void writeUTF(String str) throws IOException
{
throw new UnsupportedOperationException();
}
/**
* @deprecated see {@link java.io.DataInputStream#readLine()}
*/
public String readLine() throws IOException
{
throw new UnsupportedOperationException("deprecated");
}
}
1.24 +1 -1 jakarta-commons-sandbox/vfs/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/build.xml,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- build.xml 16 Jun 2004 20:21:35 -0000 1.23
+++ build.xml 17 Jun 2004 19:29:28 -0000 1.24
@@ -184,7 +184,7 @@
</get>
<get dest="${libdir}/junit-3.8.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/junit/jars/junit-3.8.1.jar">
</get>
- <get dest="${libdir}/commons-net-1.1.0.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-net/jars/commons-net-1.0.0.jar">
+ <get dest="${libdir}/commons-net-1.2.1.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-net/jars/commons-net-1.2.1.jar">
</get>
<get dest="${libdir}/commons-logging-1.0.3.jar" usetimestamp="true" ignoreerrors="true" src="http://www.ibiblio.org/maven/commons-logging/jars/commons-logging-1.0.3.jar">
</get>
1.35 +1 -1 jakarta-commons-sandbox/vfs/project.xml
Index: project.xml
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/project.xml,v
retrieving revision 1.34
retrieving revision 1.35
diff -u -r1.34 -r1.35
--- project.xml 16 Jun 2004 20:21:35 -0000 1.34
+++ project.xml 17 Jun 2004 19:29:28 -0000 1.35
@@ -48,7 +48,7 @@
<dependency>
<id>commons-net</id>
- <version>1.1.0</version>
+ <version>1.2.1</version>
</dependency>
<dependency>
1.11 +11 -1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Capability.java
Index: Capability.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Capability.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- Capability.java 20 May 2004 18:47:30 -0000 1.10
+++ Capability.java 17 Jun 2004 19:29:28 -0000 1.11
@@ -34,6 +34,16 @@
public static final Capability WRITE_CONTENT = new Capability("WRITE_CONTENT");
/**
+ * File content can be read in random mode.<br>
+ */
+ public static final Capability RANDOM_ACCESS_READ = new Capability("RANDOM_ACCESS_READ");
+
+ /**
+ * File content can be written in random mode.<br>
+ */
+ public static final Capability RANDOM_ACCESS_WRITE = new Capability("RANDOM_ACCESS_WRITE");
+
+ /**
* File content can be appended.
*/
public static final Capability APPEND_CONTENT = new Capability("APPEND_CONTENT");
1.14 +18 -0 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileContent.java
Index: FileContent.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/FileContent.java,v
retrieving revision 1.13
retrieving revision 1.14
diff -u -r1.13 -r1.14
--- FileContent.java 21 May 2004 20:43:29 -0000 1.13
+++ FileContent.java 17 Jun 2004 19:29:28 -0000 1.14
@@ -15,6 +15,8 @@
*/
package org.apache.commons.vfs;
+import org.apache.commons.vfs.util.RandomAccessMode;
+
import java.io.InputStream;
import java.io.OutputStream;
import java.security.cert.Certificate;
@@ -150,6 +152,22 @@
* or on error opening the stream.
*/
OutputStream getOutputStream() throws FileSystemException;
+
+ /**
+ * Returns an stream for reading/writing the file's content.
+ * <p/>
+ * If the file does not exist, and you use one of the write* methods,
+ * this method creates it, and the parent folder, if necessary.
+ * If the file does exist, parts of the file are replaced with whatever is written
+ * at a given position.
+ * <p/>
+ * <p>There may only be a single input or output stream open for the
+ * file at any time.
+ *
+ * @throws FileSystemException If the file is read-only, or is being read, or is being written,
+ * or on error opening the stream.
+ */
+ public RandomAccessContent getRandomAccessContent(final RandomAccessMode mode) throws FileSystemException;
/**
* Returns an output stream for writing the file's content.
1.37 +2 -0 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Resources.properties
Index: Resources.properties
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/Resources.properties,v
retrieving revision 1.36
retrieving revision 1.37
diff -u -r1.36 -r1.37
--- Resources.properties 16 Jun 2004 18:23:04 -0000 1.36
+++ Resources.properties 17 Jun 2004 19:29:28 -0000 1.37
@@ -8,6 +8,8 @@
vfs.provider/rename-not-supported.error=This file type does not support rename.
vfs.provider/write-append-not-supported.error=The file type does not support append mode.
vfs.provider/random-access-not-supported.error=The file type does not support random access.
+vfs.provider/random-access-read-not-supported.error=The file type does not support read in random access mode.
+vfs.provider/random-access-write-not-supported.error=The file type does not support write in random access mode.
vfs.provider/create-folder-not-supported.error=This file type does not support folder creation.
vfs.provider/get-last-modified-not-supported.error=This file type does not support retriving last modified time.
vfs.provider/set-last-modified-not-supported.error=This file type does not support setting last modified time.
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/RandomAccessContent.java
Index: RandomAccessContent.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs;
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
/**
* Description
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:28 $
*/
public interface RandomAccessContent extends DataOutput, DataInput
{
/**
* Returns the current offset in this file.
*
* @return the offset from the beginning of the file, in bytes,
* at which the next read or write occurs.
* @throws IOException if an I/O error occurs.
*/
public long getFilePointer() throws IOException;
/**
* Sets the file-pointer offset, measured from the beginning of this
* file, at which the next read or write occurs. The offset may be
* set beyond the end of the file. Setting the offset beyond the end
* of the file does not change the file length. The file length will
* change only by writing after the offset has been set beyond the end
* of the file.
*
* @param pos the offset position, measured in bytes from the
* beginning of the file, at which to set the file
* pointer.
* @throws IOException if <code>pos</code> is less than
* <code>0</code> or if an I/O error occurs.
*/
public void seek(long pos) throws IOException;
/**
* Returns the length of this file.
*
* @return the length of this file, measured in bytes.
* @throws IOException if an I/O error occurs.
*/
public long length() throws IOException;
/**
* Closes this random access file stream and releases any system
* resources associated with the stream. A closed random access
* file cannot perform input or output operations and cannot be
* reopened.
* <p/>
* <p> If this file has an associated channel then the channel is closed
* as well.
*
* @throws IOException if an I/O error occurs.
*/
public void close() throws IOException;
}
1.8 +4 -2 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/DefaultLocalFileProvider.java
Index: DefaultLocalFileProvider.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/DefaultLocalFileProvider.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- DefaultLocalFileProvider.java 19 May 2004 19:34:06 -0000 1.7
+++ DefaultLocalFileProvider.java 17 Jun 2004 19:29:28 -0000 1.8
@@ -52,7 +52,9 @@
Capability.READ_CONTENT,
Capability.URI,
Capability.WRITE_CONTENT,
- Capability.APPEND_CONTENT
+ Capability.APPEND_CONTENT,
+ Capability.RANDOM_ACCESS_READ,
+ Capability.RANDOM_ACCESS_WRITE
}));
private final LocalFileNameParser parser;
1.16 +8 -1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/LocalFile.java
Index: LocalFile.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/LocalFile.java,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -r1.15 -r1.16
--- LocalFile.java 10 May 2004 20:09:48 -0000 1.15
+++ LocalFile.java 17 Jun 2004 19:29:28 -0000 1.16
@@ -19,7 +19,9 @@
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.provider.AbstractFileObject;
+import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.File;
import java.io.FileInputStream;
@@ -195,7 +197,7 @@
protected OutputStream doGetOutputStream(boolean bAppend)
throws Exception
{
- return new FileOutputStream(file, bAppend);
+ return new FileOutputStream(file.getPath(), bAppend);
}
/**
@@ -205,5 +207,10 @@
throws Exception
{
return file.length();
+ }
+
+ protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception
+ {
+ return new LocalFileRandomAccessContent(file, mode);
}
}
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/local/LocalFileRandomAccessContent.java
Index: LocalFileRandomAccessContent.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs.provider.local;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.provider.AbstractRandomAccessContent;
import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/**
* RandomAccess for local files
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $
*/
class LocalFileRandomAccessContent extends AbstractRandomAccessContent
{
// private final LocalFile localFile;
final private RandomAccessFile raf;
LocalFileRandomAccessContent(final File localFile, final RandomAccessMode mode) throws FileSystemException
{
super(mode);
StringBuffer modes = new StringBuffer(2);
if (mode.requestRead())
{
modes.append('r');
}
if (mode.requestWrite())
{
modes.append('w');
}
try
{
raf = new RandomAccessFile(localFile, modes.toString());
}
catch (FileNotFoundException e)
{
throw new FileSystemException("vfs.provider/random-access-open-failed.error", localFile);
}
}
public long getFilePointer() throws IOException
{
return raf.getFilePointer();
}
public void seek(long pos) throws IOException
{
raf.seek(pos);
}
public long length() throws IOException
{
return raf.length();
}
public void close() throws IOException
{
raf.close();
}
public byte readByte() throws IOException
{
return raf.readByte();
}
public char readChar() throws IOException
{
return raf.readChar();
}
public double readDouble() throws IOException
{
return raf.readDouble();
}
public float readFloat() throws IOException
{
return raf.readFloat();
}
public int readInt() throws IOException
{
return raf.readInt();
}
public int readUnsignedByte() throws IOException
{
return raf.readUnsignedByte();
}
public int readUnsignedShort() throws IOException
{
return raf.readUnsignedShort();
}
public long readLong() throws IOException
{
return raf.readLong();
}
public short readShort() throws IOException
{
return raf.readShort();
}
public boolean readBoolean() throws IOException
{
return raf.readBoolean();
}
public int skipBytes(int n) throws IOException
{
return raf.skipBytes(n);
}
public void readFully(byte b[]) throws IOException
{
raf.readFully(b);
}
public void readFully(byte b[], int off, int len) throws IOException
{
raf.readFully(b, off, len);
}
public String readUTF() throws IOException
{
return raf.readUTF();
}
public void writeDouble(double v) throws IOException
{
raf.writeDouble(v);
}
public void writeFloat(float v) throws IOException
{
raf.writeFloat(v);
}
public void write(int b) throws IOException
{
raf.write(b);
}
public void writeByte(int v) throws IOException
{
raf.writeByte(v);
}
public void writeChar(int v) throws IOException
{
raf.writeChar(v);
}
public void writeInt(int v) throws IOException
{
raf.writeInt(v);
}
public void writeShort(int v) throws IOException
{
raf.writeShort(v);
}
public void writeLong(long v) throws IOException
{
raf.writeLong(v);
}
public void writeBoolean(boolean v) throws IOException
{
raf.writeBoolean(v);
}
public void write(byte b[]) throws IOException
{
raf.write(b);
}
public void write(byte b[], int off, int len) throws IOException
{
raf.write(b, off, len);
}
public void writeBytes(String s) throws IOException
{
raf.writeBytes(s);
}
public void writeChars(String s) throws IOException
{
raf.writeChars(s);
}
public void writeUTF(String str) throws IOException
{
raf.writeUTF(str);
}
}
1.8 +10 -3 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileObject.java
Index: HttpFileObject.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileObject.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- HttpFileObject.java 21 May 2004 20:43:30 -0000 1.7
+++ HttpFileObject.java 17 Jun 2004 19:29:29 -0000 1.8
@@ -25,8 +25,10 @@
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.provider.AbstractFileObject;
import org.apache.commons.vfs.util.MonitorInputStream;
+import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.IOException;
import java.io.InputStream;
@@ -152,10 +154,15 @@
return new HttpInputStream(getMethod);
}
+ protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception
+ {
+ return new HttpRandomAccesContent(this, mode);
+ }
+
/**
* Prepares a Method object.
*/
- private void setupMethod(final HttpMethod method)
+ void setupMethod(final HttpMethod method)
{
method.setPath(getName().getPath());
method.setFollowRedirects(true);
@@ -165,7 +172,7 @@
/**
* An InputStream that cleans up the HTTP connection on close.
*/
- private static class HttpInputStream
+ static class HttpInputStream
extends MonitorInputStream
{
private final GetMethod method;
1.9 +4 -2 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java
Index: HttpFileProvider.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpFileProvider.java,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- HttpFileProvider.java 27 May 2004 19:09:37 -0000 1.8
+++ HttpFileProvider.java 17 Jun 2004 19:29:29 -0000 1.9
@@ -41,10 +41,12 @@
{
final static Collection capabilities = Collections.unmodifiableCollection(Arrays.asList(new Capability[]
{
+ Capability.GET_TYPE,
Capability.READ_CONTENT,
Capability.URI,
Capability.GET_LAST_MODIFIED,
- Capability.ATTRIBUTES
+ Capability.ATTRIBUTES,
+ Capability.RANDOM_ACCESS_READ
}));
public HttpFileProvider()
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/http/HttpRandomAccesContent.java
Index: HttpRandomAccesContent.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs.provider.http;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.provider.AbstractRandomAccessContent;
import org.apache.commons.vfs.util.MonitorInputStream;
import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
class HttpRandomAccesContent extends AbstractRandomAccessContent
{
private final HttpFileObject fileObject;
private final HttpFileSystem fileSystem;
protected long filePointer = 0;
private DataInputStream dis = null;
private MonitorInputStream mis = null;
HttpRandomAccesContent(final HttpFileObject fileObject, RandomAccessMode mode)
{
super(mode);
this.fileObject = fileObject;
fileSystem = (HttpFileSystem) this.fileObject.getFileSystem();
}
public long getFilePointer() throws IOException
{
return filePointer;
}
public void seek(long pos) throws IOException
{
if (pos == 0)
{
// no change
return;
}
if (pos < 0)
{
throw new FileSystemException("vfs.provider/random-access-invalid-position.error",
new Object[]
{
new Long(pos)
});
}
if (dis != null)
{
close();
}
filePointer = pos;
}
private void createStream() throws IOException
{
if (dis != null)
{
return;
}
final GetMethod getMethod = new GetMethod();
fileObject.setupMethod(getMethod);
getMethod.setRequestHeader("Range", "bytes=" + filePointer + "-");
final int status = fileSystem.getClient().executeMethod(getMethod);
if (status != HttpURLConnection.HTTP_PARTIAL)
{
throw new FileSystemException("vfs.provider.http/get-range.error", new Object[]
{
fileObject.getName(),
new Long(filePointer)
});
}
mis = new HttpFileObject.HttpInputStream(getMethod);
dis = new DataInputStream(mis);
}
public void close() throws IOException
{
if (dis != null)
{
dis.close();
dis = null;
mis = null;
}
}
public long length() throws IOException
{
return fileObject.getContent().getSize();
}
public byte readByte() throws IOException
{
createStream();
byte data = dis.readByte();
filePointer += mis.getCount();
return data;
}
public char readChar() throws IOException
{
createStream();
char data = dis.readChar();
filePointer += mis.getCount();
return data;
}
public double readDouble() throws IOException
{
createStream();
double data = dis.readDouble();
filePointer += mis.getCount();
return data;
}
public float readFloat() throws IOException
{
createStream();
float data = dis.readFloat();
filePointer += mis.getCount();
return data;
}
public int readInt() throws IOException
{
createStream();
int data = dis.readInt();
filePointer += mis.getCount();
return data;
}
public int readUnsignedByte() throws IOException
{
createStream();
int data = dis.readUnsignedByte();
filePointer += mis.getCount();
return data;
}
public int readUnsignedShort() throws IOException
{
createStream();
int data = dis.readUnsignedShort();
filePointer += mis.getCount();
return data;
}
public long readLong() throws IOException
{
createStream();
long data = dis.readLong();
filePointer += mis.getCount();
return data;
}
public short readShort() throws IOException
{
createStream();
short data = dis.readShort();
filePointer += mis.getCount();
return data;
}
public boolean readBoolean() throws IOException
{
createStream();
boolean data = dis.readBoolean();
filePointer += mis.getCount();
return data;
}
public int skipBytes(int n) throws IOException
{
createStream();
int data = dis.skipBytes(n);
filePointer += mis.getCount();
return data;
}
public void readFully(byte b[]) throws IOException
{
createStream();
dis.readFully(b);
filePointer += mis.getCount();
}
public void readFully(byte b[], int off, int len) throws IOException
{
createStream();
dis.readFully(b, off, len);
filePointer += mis.getCount();
}
public String readUTF() throws IOException
{
createStream();
String data = dis.readUTF();
filePointer += mis.getCount();
return data;
}
}
1.6 +13 -1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/util/MonitorInputStream.java
Index: MonitorInputStream.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/util/MonitorInputStream.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- MonitorInputStream.java 10 May 2004 20:09:50 -0000 1.5
+++ MonitorInputStream.java 17 Jun 2004 19:29:29 -0000 1.6
@@ -29,10 +29,12 @@
extends BufferedInputStream
{
private boolean finished;
+ private long count;
public MonitorInputStream(final InputStream in)
{
super(in);
+ count = 0;
}
/**
@@ -48,6 +50,7 @@
final int ch = super.read();
if (ch != -1)
{
+ count++;
return ch;
}
@@ -70,6 +73,7 @@
final int nread = super.read(buffer, offset, length);
if (nread != -1)
{
+ count += nread;
return nread;
}
@@ -123,5 +127,13 @@
*/
protected void onClose() throws IOException
{
+ }
+
+ /**
+ * Get the nuber of bytes read by this input stream
+ */
+ public long getCount()
+ {
+ return count;
}
}
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/util/MonitorRandomAccessContent.java
Index: MonitorRandomAccessContent.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs.util;
import org.apache.commons.vfs.RandomAccessContent;
import java.io.IOException;
/**
* An RandomAccessContent that provides end-of-stream monitoring.
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $
*/
public class MonitorRandomAccessContent implements RandomAccessContent
{
private final RandomAccessContent content;
private boolean finished;
public MonitorRandomAccessContent(final RandomAccessContent content)
{
this.content = content;
}
/**
* Called after this stream is closed. This implementation does nothing.
*/
protected void onClose() throws IOException
{
}
/**
* Closes this output stream.
*/
public void close() throws IOException
{
if (finished)
{
return;
}
// Close the output stream
IOException exc = null;
try
{
content.close();
}
catch (final IOException ioe)
{
exc = ioe;
}
// Notify of end of output
exc = null;
try
{
onClose();
}
catch (final IOException ioe)
{
exc = ioe;
}
finished = true;
if (exc != null)
{
throw exc;
}
}
public long getFilePointer() throws IOException
{
return content.getFilePointer();
}
public void seek(long pos) throws IOException
{
content.seek(pos);
}
public long length() throws IOException
{
return content.length();
}
public void write(int b) throws IOException
{
content.write(b);
}
public void write(byte[] b) throws IOException
{
content.write(b);
}
public void write(byte[] b, int off, int len) throws IOException
{
content.write(b, off, len);
}
public void writeBoolean(boolean v) throws IOException
{
content.writeBoolean(v);
}
public void writeByte(int v) throws IOException
{
content.writeByte(v);
}
public void writeShort(int v) throws IOException
{
content.writeShort(v);
}
public void writeChar(int v) throws IOException
{
content.writeChar(v);
}
public void writeInt(int v) throws IOException
{
content.writeInt(v);
}
public void writeLong(long v) throws IOException
{
content.writeLong(v);
}
public void writeFloat(float v) throws IOException
{
content.writeFloat(v);
}
public void writeDouble(double v) throws IOException
{
content.writeDouble(v);
}
public void writeBytes(String s) throws IOException
{
content.writeBytes(s);
}
public void writeChars(String s) throws IOException
{
content.writeChars(s);
}
public void writeUTF(String str) throws IOException
{
content.writeUTF(str);
}
public void readFully(byte[] b) throws IOException
{
content.readFully(b);
}
public void readFully(byte[] b, int off, int len) throws IOException
{
content.readFully(b, off, len);
}
public int skipBytes(int n) throws IOException
{
return content.skipBytes(n);
}
public boolean readBoolean() throws IOException
{
return content.readBoolean();
}
public byte readByte() throws IOException
{
return content.readByte();
}
public int readUnsignedByte() throws IOException
{
return content.readUnsignedByte();
}
public short readShort() throws IOException
{
return content.readShort();
}
public int readUnsignedShort() throws IOException
{
return content.readUnsignedShort();
}
public char readChar() throws IOException
{
return content.readChar();
}
public int readInt() throws IOException
{
return content.readInt();
}
public long readLong() throws IOException
{
return content.readLong();
}
public float readFloat() throws IOException
{
return content.readFloat();
}
public double readDouble() throws IOException
{
return content.readDouble();
}
public String readLine() throws IOException
{
return content.readLine();
}
public String readUTF() throws IOException
{
return content.readUTF();
}
}
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/util/RandomAccessMode.java
Index: RandomAccessMode.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs.util;
/**
* An enumerated type representing the modes of a random access content.
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $
*/
public class RandomAccessMode
{
/**
* read
*/
public static final RandomAccessMode READ = new RandomAccessMode(true, false);
/**
* read/write
*/
public static final RandomAccessMode READWRITE = new RandomAccessMode(true, true);
private final boolean read;
private final boolean write;
private RandomAccessMode(final boolean read, final boolean write)
{
this.read = read;
this.write = write;
}
public boolean requestRead()
{
return read;
}
public boolean requestWrite()
{
return write;
}
}
1.21 +3 -1 jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderTestSuite.java
Index: ProviderTestSuite.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderTestSuite.java,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -r1.20 -r1.21
--- ProviderTestSuite.java 3 Jun 2004 17:14:16 -0000 1.20
+++ ProviderTestSuite.java 17 Jun 2004 19:29:29 -0000 1.21
@@ -52,8 +52,10 @@
addTests(NamingTests.class);
addTests(ContentTests.class);
addTests(ProviderReadTests.class);
+ addTests(ProviderRandomReadTests.class);
addTests(ProviderWriteTests.class);
addTests(ProviderWriteAppendTests.class);
+ addTests(ProviderRandomReadWriteTests.class);
addTests(ProviderRenameTests.class);
addTests(ProviderDeleteTests.class);
addTests(LastModifiedTests.class);
1.8 +9 -1 jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/UrlStructureTests.java
Index: UrlStructureTests.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/UrlStructureTests.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- UrlStructureTests.java 10 May 2004 20:09:44 -0000 1.7
+++ UrlStructureTests.java 17 Jun 2004 19:29:29 -0000 1.8
@@ -17,6 +17,7 @@
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileObject;
+import org.apache.commons.vfs.provider.http.HttpFileSystem;
import java.io.IOException;
@@ -47,6 +48,13 @@
public void testFolderURL() throws Exception
{
final FileObject folder = getReadFolder().resolveFile("dir1");
+ if (folder.getFileSystem() instanceof HttpFileSystem)
+ {
+ // bad hack, but this test might not fail on HttpFileSystem as there are no direcotries.
+ // A Directory do have a content on http. e.g a generated directory listing or the index.html page.
+ return;
+ }
+
assertTrue(folder.exists());
// Try getting the content of a folder
1.1 jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderRandomReadTests.java
Index: ProviderRandomReadTests.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs.test;
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.util.RandomAccessMode;
/**
* RanomdRead-only test cases for file providers.
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $
*/
public class ProviderRandomReadTests
extends AbstractProviderTestCase
{
private final String TEST_DATA = "This is a test file.";
/**
* Returns the capabilities required by the tests of this test case.
*/
protected Capability[] getRequiredCaps()
{
return new Capability[]
{
Capability.GET_TYPE,
Capability.RANDOM_ACCESS_READ
};
}
/**
* Read a file
*/
public void testRandomRead() throws Exception
{
FileObject file = null;
try
{
file = getReadFolder().resolveFile("file1.txt");
RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READ);
// read first byte
byte c = ra.readByte();
assertEquals(c, TEST_DATA.charAt(0));
// start at pos 4
ra.seek(3);
c = ra.readByte();
assertEquals(c, TEST_DATA.charAt(3));
c = ra.readByte();
assertEquals(c, TEST_DATA.charAt(4));
// restart at pos 4
ra.seek(3);
c = ra.readByte();
assertEquals(c, TEST_DATA.charAt(3));
c = ra.readByte();
assertEquals(c, TEST_DATA.charAt(4));
// advance to pos 11
ra.seek(10);
c = ra.readByte();
assertEquals(c, TEST_DATA.charAt(10));
c = ra.readByte();
assertEquals(c, TEST_DATA.charAt(11));
}
finally
{
if (file != null)
{
file.close();
}
}
}
}
1.1 jakarta-commons-sandbox/vfs/src/test/org/apache/commons/vfs/test/ProviderRandomReadWriteTests.java
Index: ProviderRandomReadWriteTests.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs.test;
import org.apache.commons.vfs.Capability;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.util.RandomAccessMode;
/**
* RanomdRead/Write test cases for file providers.
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $
*/
public class ProviderRandomReadWriteTests
extends AbstractProviderTestCase
{
private final String TEST_DATA = "This is a test file.";
/**
* Returns the capabilities required by the tests of this test case.
*/
protected Capability[] getRequiredCaps()
{
return new Capability[]
{
Capability.GET_TYPE,
Capability.CREATE,
Capability.RANDOM_ACCESS_READ,
Capability.RANDOM_ACCESS_WRITE
};
}
/**
* Sets up a scratch folder for the test to use.
*/
protected FileObject createScratchFolder() throws Exception
{
FileObject scratchFolder = getWriteFolder();
// Make sure the test folder is empty
scratchFolder.delete(Selectors.EXCLUDE_SELF);
scratchFolder.createFolder();
return scratchFolder;
}
/**
* Read a file
*/
public void testRandomWrite() throws Exception
{
FileObject file = null;
try
{
file = createScratchFolder().resolveFile("random_write.txt");
file.createFile();
RandomAccessContent ra = file.getContent().getRandomAccessContent(RandomAccessMode.READWRITE);
// write first byte
ra.writeByte(TEST_DATA.charAt(0));
// start at pos 4
ra.seek(3);
ra.writeByte(TEST_DATA.charAt(3));
ra.writeByte(TEST_DATA.charAt(4));
// restart at pos 4 (but overwrite with different content)
ra.seek(3);
ra.writeByte(TEST_DATA.charAt(7));
ra.writeByte(TEST_DATA.charAt(8));
// advance to pos 11
ra.seek(10);
ra.writeByte(TEST_DATA.charAt(10));
ra.writeByte(TEST_DATA.charAt(11));
// now read
ra.seek(0);
assertEquals(ra.readByte(), TEST_DATA.charAt(0));
ra.seek(3);
assertEquals(ra.readByte(), TEST_DATA.charAt(7));
assertEquals(ra.readByte(), TEST_DATA.charAt(8));
ra.seek(10);
assertEquals(ra.readByte(), TEST_DATA.charAt(10));
assertEquals(ra.readByte(), TEST_DATA.charAt(11));
}
finally
{
if (file != null)
{
file.close();
}
}
}
}
1.15 +10 -0 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java
Index: SmbFileObject.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbFileObject.java,v
retrieving revision 1.14
retrieving revision 1.15
diff -u -r1.14 -r1.15
--- SmbFileObject.java 10 May 2004 20:09:53 -0000 1.14
+++ SmbFileObject.java 17 Jun 2004 19:29:29 -0000 1.15
@@ -23,7 +23,9 @@
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileType;
+import org.apache.commons.vfs.RandomAccessContent;
import org.apache.commons.vfs.provider.AbstractFileObject;
+import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.InputStream;
import java.io.OutputStream;
@@ -167,5 +169,13 @@
protected OutputStream doGetOutputStream(boolean bAppend) throws Exception
{
return new SmbFileOutputStream(file, bAppend);
+ }
+
+ /**
+ * random access
+ */
+ protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception
+ {
+ return new SmbFileRandomAccessContent(file, mode);
}
}
1.8 +4 -2 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbFileProvider.java
Index: SmbFileProvider.java
===================================================================
RCS file: /home/cvs/jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbFileProvider.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- SmbFileProvider.java 19 May 2004 19:34:07 -0000 1.7
+++ SmbFileProvider.java 17 Jun 2004 19:29:29 -0000 1.8
@@ -48,7 +48,9 @@
Capability.READ_CONTENT,
Capability.URI,
Capability.WRITE_CONTENT,
- Capability.APPEND_CONTENT
+ Capability.APPEND_CONTENT,
+ Capability.RANDOM_ACCESS_READ,
+ Capability.RANDOM_ACCESS_WRITE
}));
public SmbFileProvider()
1.1 jakarta-commons-sandbox/vfs/src/java/org/apache/commons/vfs/provider/smb/SmbFileRandomAccessContent.java
Index: SmbFileRandomAccessContent.java
===================================================================
/*
* Copyright 2002, 2003,2004 The Apache Software Foundation.
*
* 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 org.apache.commons.vfs.provider.smb;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbRandomAccessFile;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.provider.AbstractRandomAccessContent;
import org.apache.commons.vfs.util.RandomAccessMode;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.UnknownHostException;
/**
* RandomAccess for smb files
*
* @author <a href="mailto:imario@apache.org">Mario Ivankovits</a>
* @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $
*/
class SmbFileRandomAccessContent extends AbstractRandomAccessContent
{
private final SmbRandomAccessFile raf;
SmbFileRandomAccessContent(final SmbFile smbFile, final RandomAccessMode mode) throws FileSystemException
{
super(mode);
StringBuffer modes = new StringBuffer(2);
if (mode.requestRead())
{
modes.append('r');
}
if (mode.requestWrite())
{
modes.append('w');
}
try
{
raf = new SmbRandomAccessFile(smbFile, modes.toString());
}
catch (MalformedURLException e)
{
throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
}
catch (SmbException e)
{
throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
}
catch (UnknownHostException e)
{
throw new FileSystemException("vfs.provider/random-access-open-failed.error", smbFile, e);
}
}
public long getFilePointer() throws IOException
{
return raf.getFilePointer();
}
public void seek(long pos) throws IOException
{
raf.seek(pos);
}
public long length() throws IOException
{
return raf.length();
}
public void close() throws IOException
{
raf.close();
}
public byte readByte() throws IOException
{
return raf.readByte();
}
public char readChar() throws IOException
{
return raf.readChar();
}
public double readDouble() throws IOException
{
return raf.readDouble();
}
public float readFloat() throws IOException
{
return raf.readFloat();
}
public int readInt() throws IOException
{
return raf.readInt();
}
public int readUnsignedByte() throws IOException
{
return raf.readUnsignedByte();
}
public int readUnsignedShort() throws IOException
{
return raf.readUnsignedShort();
}
public long readLong() throws IOException
{
return raf.readLong();
}
public short readShort() throws IOException
{
return raf.readShort();
}
public boolean readBoolean() throws IOException
{
return raf.readBoolean();
}
public int skipBytes(int n) throws IOException
{
return raf.skipBytes(n);
}
public void readFully(byte b[]) throws IOException
{
raf.readFully(b);
}
public void readFully(byte b[], int off, int len) throws IOException
{
raf.readFully(b, off, len);
}
public String readUTF() throws IOException
{
return raf.readUTF();
}
public void writeDouble(double v) throws IOException
{
raf.writeDouble(v);
}
public void writeFloat(float v) throws IOException
{
raf.writeFloat(v);
}
public void write(int b) throws IOException
{
raf.write(b);
}
public void writeByte(int v) throws IOException
{
raf.writeByte(v);
}
public void writeChar(int v) throws IOException
{
raf.writeChar(v);
}
public void writeInt(int v) throws IOException
{
raf.writeInt(v);
}
public void writeShort(int v) throws IOException
{
raf.writeShort(v);
}
public void writeLong(long v) throws IOException
{
raf.writeLong(v);
}
public void writeBoolean(boolean v) throws IOException
{
raf.writeBoolean(v);
}
public void write(byte b[]) throws IOException
{
raf.write(b);
}
public void write(byte b[], int off, int len) throws IOException
{
raf.write(b, off, len);
}
public void writeBytes(String s) throws IOException
{
raf.writeBytes(s);
}
public void writeChars(String s) throws IOException
{
raf.writeChars(s);
}
public void writeUTF(String str) throws IOException
{
raf.writeUTF(str);
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org