Author: imario Date: Wed Feb 16 11:25:31 2005 New Revision: 154072 URL: http://svn.apache.org/viewcvs?view=rev&rev=154072 Log: FtpRandomAccessContent aka restart filetransfer bugfix HttpRandomAccessContent - extended testcase
Added: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpRandomAccessContent.java Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/RandomAccessContent.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractRandomAccessContent.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClient.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpRandomAccesContent.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/local/LocalFileRandomAccessContent.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileRandomAccessContent.java jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/util/MonitorRandomAccessContent.java jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/test/ProviderRandomReadTests.java jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/util/DelegatingFileSystemOptionsBuilderTest.java Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/RandomAccessContent.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/RandomAccessContent.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/RandomAccessContent.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/RandomAccessContent.java Wed Feb 16 11:25:31 2005 @@ -18,12 +18,13 @@ import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; +import java.io.InputStream; /** * Description * * @author <a href="mailto:[EMAIL PROTECTED]">Mario Ivankovits</a> - * @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:28 $ + * @version $Revision: 1.1 $ $Date$ */ public interface RandomAccessContent extends DataOutput, DataInput { @@ -43,6 +44,8 @@ * 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. + * <br/> + * <b>Notice: If you use [EMAIL PROTECTED] #getInputStream()} you have to reget the InputStream after calling [EMAIL PROTECTED] #seek(long)}</b> * * @param pos the offset position, measured in bytes from the * beginning of the file, at which to set the file @@ -72,4 +75,12 @@ * @throws IOException if an I/O error occurs. */ public void close() throws IOException; + + /** + * get the inputstream interface + * <br/> + * <b>Notice: If you use [EMAIL PROTECTED] #seek(long)} you have to reget the InputStream</b> + * @return + */ + public InputStream getInputStream(); } Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractRandomAccessContent.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractRandomAccessContent.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractRandomAccessContent.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/AbstractRandomAccessContent.java Wed Feb 16 11:25:31 2005 @@ -26,7 +26,7 @@ * (for read-only random access implementations) * * @author <a href="mailto:[EMAIL PROTECTED]">Mario Ivankovits</a> - * @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:28 $ + * @version $Revision: 1.1 $ $Date$ */ public abstract class AbstractRandomAccessContent implements RandomAccessContent { Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FTPClientWrapper.java Wed Feb 16 11:25:31 2005 @@ -160,7 +160,12 @@ public boolean completePendingCommand() throws IOException { - return getFtpClient().completePendingCommand(); + if (ftpClient != null) + { + return getFtpClient().completePendingCommand(); + } + + return true; } public InputStream retrieveFileStream(String relPath) throws IOException @@ -176,6 +181,24 @@ } } + public InputStream retrieveFileStream(String relPath, long restartOffset) throws IOException + { + try + { + FTPClient client = getFtpClient(); + client.setRestartOffset(restartOffset); + return client.retrieveFileStream(relPath); + } + catch (FTPConnectionClosedException e) + { + disconnect(); + + FTPClient client = getFtpClient(); + client.setRestartOffset(restartOffset); + return client.retrieveFileStream(relPath); + } + } + public OutputStream appendFileStream(String relPath) throws IOException { try @@ -200,5 +223,24 @@ disconnect(); return getFtpClient().storeFileStream(relPath); } + } + + public boolean abort() throws IOException + { + try + { + // [EMAIL PROTECTED]: 2005-02-14 + // it should be better to really "abort" the transfer, but + // currently I didnt manage to make it work - so lets "abort" the hard way. + // return getFtpClient().abort(); + + disconnect(); + return true; + } + catch (FTPConnectionClosedException e) + { + disconnect(); + } + return true; } } Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClient.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClient.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClient.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpClient.java Wed Feb 16 11:25:31 2005 @@ -44,8 +44,11 @@ boolean completePendingCommand() throws IOException; InputStream retrieveFileStream(String relPath) throws IOException; + InputStream retrieveFileStream(String relPath, long restartOffset) throws IOException; OutputStream appendFileStream(String relPath) throws IOException; OutputStream storeFileStream(String relPath) throws IOException; + + public boolean abort() throws IOException; } Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileObject.java Wed Feb 16 11:25:31 2005 @@ -20,9 +20,11 @@ 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.MonitorInputStream; import org.apache.commons.vfs.util.MonitorOutputStream; +import org.apache.commons.vfs.util.RandomAccessMode; import java.io.IOException; import java.io.InputStream; @@ -419,6 +421,11 @@ return new FtpInputStream(client, instr); } + protected RandomAccessContent doGetRandomAccessContent(final RandomAccessMode mode) throws Exception + { + return new FtpRandomAccessContent(this, mode); + } + /** * Creates an output stream to write the file content to. */ @@ -436,10 +443,22 @@ } } + String getRelPath() + { + return relPath; + } + + FtpInputStream getInputStream(long filePointer) throws IOException + { + final FtpClient client = ftpFs.getClient(); + final InputStream instr = client.retrieveFileStream(relPath, filePointer); + return new FtpInputStream(client, instr); + } + /** * An InputStream that monitors for end-of-file. */ - private class FtpInputStream + class FtpInputStream extends MonitorInputStream { private final FtpClient client; @@ -450,6 +469,11 @@ this.client = client; } + void abort() throws IOException + { + client.abort(); + close(); + } /** * Called after the stream has been closed. */ Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileProvider.java Wed Feb 16 11:25:31 2005 @@ -32,7 +32,7 @@ * A provider for FTP file systems. * * @author <a href="mailto:[EMAIL PROTECTED]">Adam Murdoch</a> - * @version $Revision: 1.12 $ $Date: 2005/01/11 16:14:46 $ + * @version $Revision: 1.12 $ $Date$ */ public class FtpFileProvider extends AbstractOriginatingFileProvider @@ -54,6 +54,7 @@ Capability.URI, Capability.WRITE_CONTENT, Capability.APPEND_CONTENT, + Capability.RANDOM_ACCESS_READ, })); public FtpFileProvider() Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpFileSystemConfigBuilder.java Wed Feb 16 11:25:31 2005 @@ -23,7 +23,7 @@ * The config builder for various ftp configuration options * * @author <a href="mailto:[EMAIL PROTECTED]">Mario Ivankovits</a> - * @version $Revision: 1.9 $ $Date: 2004/12/21 21:55:41 $ + * @version $Revision: 1.9 $ $Date$ */ public class FtpFileSystemConfigBuilder extends FileSystemConfigBuilder { @@ -99,7 +99,7 @@ */ public void setPassiveMode(FileSystemOptions opts, boolean passiveMode) { - setParam(opts, PASSIVE_MODE, Boolean.valueOf(passiveMode)); + setParam(opts, PASSIVE_MODE, passiveMode?Boolean.TRUE:Boolean.FALSE); } /** Added: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpRandomAccessContent.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpRandomAccessContent.java?view=auto&rev=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpRandomAccessContent.java (added) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/ftp/FtpRandomAccessContent.java Wed Feb 16 11:25:31 2005 @@ -0,0 +1,241 @@ +/* + * 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.ftp; + +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.io.InputStream; +import java.io.FilterInputStream; + +class FtpRandomAccessContent extends AbstractRandomAccessContent +{ + private final FtpFileObject fileObject; + + protected long filePointer = 0; + private DataInputStream dis = null; + private FtpFileObject.FtpInputStream mis = null; + + FtpRandomAccessContent(final FtpFileObject fileObject, RandomAccessMode mode) + { + super(mode); + + this.fileObject = fileObject; + // fileSystem = (FtpFileSystem) this.fileObject.getFileSystem(); + } + + public long getFilePointer() throws IOException + { + return filePointer; + } + + public void seek(long pos) throws IOException + { + if (pos == filePointer) + { + // 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; + } + + // FtpClient client = fileSystem.getClient(); + mis = fileObject.getInputStream(filePointer); + dis = new DataInputStream(new FilterInputStream(mis) + { + public int read() throws IOException + { + int ret = super.read(); + if (ret > -1) + { + filePointer++; + } + return ret; + } + + public int read(byte b[]) throws IOException + { + int ret = super.read(b); + if (ret > -1) + { + filePointer+=ret; + } + return ret; + } + + public int read(byte b[], int off, int len) throws IOException + { + int ret = super.read(b, off, len); + if (ret > -1) + { + filePointer+=ret; + } + return ret; + } + + public void close() throws IOException + { + FtpRandomAccessContent.this.close(); + } + }); + } + + + public void close() throws IOException + { + if (dis != null) + { + mis.abort(); + + // this is to avoid recursive close + DataInputStream oldDis = dis; + dis = null; + oldDis.close(); + mis = null; + } + } + + public long length() throws IOException + { + return fileObject.getContent().getSize(); + } + + public byte readByte() throws IOException + { + createStream(); + byte data = dis.readByte(); + return data; + } + + public char readChar() throws IOException + { + createStream(); + char data = dis.readChar(); + return data; + } + + public double readDouble() throws IOException + { + createStream(); + double data = dis.readDouble(); + return data; + } + + public float readFloat() throws IOException + { + createStream(); + float data = dis.readFloat(); + return data; + } + + public int readInt() throws IOException + { + createStream(); + int data = dis.readInt(); + return data; + } + + public int readUnsignedByte() throws IOException + { + createStream(); + int data = dis.readUnsignedByte(); + return data; + } + + public int readUnsignedShort() throws IOException + { + createStream(); + int data = dis.readUnsignedShort(); + return data; + } + + public long readLong() throws IOException + { + createStream(); + long data = dis.readLong(); + return data; + } + + public short readShort() throws IOException + { + createStream(); + short data = dis.readShort(); + return data; + } + + public boolean readBoolean() throws IOException + { + createStream(); + boolean data = dis.readBoolean(); + return data; + } + + public int skipBytes(int n) throws IOException + { + createStream(); + int data = dis.skipBytes(n); + return data; + } + + public void readFully(byte b[]) throws IOException + { + createStream(); + dis.readFully(b); + } + + public void readFully(byte b[], int off, int len) throws IOException + { + createStream(); + dis.readFully(b, off, len); + } + + public String readUTF() throws IOException + { + createStream(); + String data = dis.readUTF(); + return data; + } + + public InputStream getInputStream() + { + return dis; + } +} \ No newline at end of file Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpRandomAccesContent.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpRandomAccesContent.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpRandomAccesContent.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/http/HttpRandomAccesContent.java Wed Feb 16 11:25:31 2005 @@ -23,6 +23,8 @@ import java.io.DataInputStream; import java.io.IOException; +import java.io.InputStream; +import java.io.FilterInputStream; import java.net.HttpURLConnection; class HttpRandomAccesContent extends AbstractRandomAccessContent @@ -49,7 +51,7 @@ public void seek(long pos) throws IOException { - if (pos == 0) + if (pos == filePointer) { // no change return; @@ -92,7 +94,38 @@ } mis = new HttpFileObject.HttpInputStream(getMethod); - dis = new DataInputStream(mis); + dis = new DataInputStream(new FilterInputStream(mis) + { + public int read() throws IOException + { + int ret = super.read(); + if (ret > -1) + { + filePointer++; + } + return ret; + } + + public int read(byte b[]) throws IOException + { + int ret = super.read(b); + if (ret > -1) + { + filePointer+=ret; + } + return ret; + } + + public int read(byte b[], int off, int len) throws IOException + { + int ret = super.read(b, off, len); + if (ret > -1) + { + filePointer+=ret; + } + return ret; + } + }); } @@ -115,7 +148,6 @@ { createStream(); byte data = dis.readByte(); - filePointer += mis.getCount(); return data; } @@ -123,7 +155,6 @@ { createStream(); char data = dis.readChar(); - filePointer += mis.getCount(); return data; } @@ -131,7 +162,6 @@ { createStream(); double data = dis.readDouble(); - filePointer += mis.getCount(); return data; } @@ -139,7 +169,6 @@ { createStream(); float data = dis.readFloat(); - filePointer += mis.getCount(); return data; } @@ -147,7 +176,6 @@ { createStream(); int data = dis.readInt(); - filePointer += mis.getCount(); return data; } @@ -155,7 +183,6 @@ { createStream(); int data = dis.readUnsignedByte(); - filePointer += mis.getCount(); return data; } @@ -163,7 +190,6 @@ { createStream(); int data = dis.readUnsignedShort(); - filePointer += mis.getCount(); return data; } @@ -171,7 +197,6 @@ { createStream(); long data = dis.readLong(); - filePointer += mis.getCount(); return data; } @@ -179,7 +204,6 @@ { createStream(); short data = dis.readShort(); - filePointer += mis.getCount(); return data; } @@ -187,7 +211,6 @@ { createStream(); boolean data = dis.readBoolean(); - filePointer += mis.getCount(); return data; } @@ -195,7 +218,6 @@ { createStream(); int data = dis.skipBytes(n); - filePointer += mis.getCount(); return data; } @@ -203,21 +225,23 @@ { 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; + } + + public InputStream getInputStream() + { + return dis; } } Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/local/LocalFileRandomAccessContent.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/local/LocalFileRandomAccessContent.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/local/LocalFileRandomAccessContent.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/local/LocalFileRandomAccessContent.java Wed Feb 16 11:25:31 2005 @@ -23,17 +23,20 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.io.RandomAccessFile; +import java.io.InputStream; +import java.io.EOFException; /** * RandomAccess for local files * * @author <a href="mailto:[EMAIL PROTECTED]">Mario Ivankovits</a> - * @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $ + * @version $Revision: 1.1 $ $Date$ */ class LocalFileRandomAccessContent extends AbstractRandomAccessContent { // private final LocalFile localFile; final private RandomAccessFile raf; + final private InputStream rafis; LocalFileRandomAccessContent(final File localFile, final RandomAccessMode mode) throws FileSystemException { @@ -52,6 +55,41 @@ try { raf = new RandomAccessFile(localFile, modes.toString()); + rafis = new InputStream() + { + public int read() throws IOException + { + try + { + return raf.readByte(); + } + catch (EOFException e) + { + return -1; + } + } + + public long skip(long n) throws IOException + { + raf.seek(raf.getFilePointer() + n); + return n; + } + + public void close() throws IOException + { + raf.close(); + } + + public int read(byte b[]) throws IOException + { + return raf.read(b); + } + + public int read(byte b[], int off, int len) throws IOException + { + return raf.read(b, off, len); + } + }; } catch (FileNotFoundException e) { @@ -217,5 +255,10 @@ public void writeUTF(String str) throws IOException { raf.writeUTF(str); + } + + public InputStream getInputStream() + { + return rafis; } } Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileRandomAccessContent.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileRandomAccessContent.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileRandomAccessContent.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/provider/smb/SmbFileRandomAccessContent.java Wed Feb 16 11:25:31 2005 @@ -23,6 +23,7 @@ import org.apache.commons.vfs.util.RandomAccessMode; import java.io.IOException; +import java.io.InputStream; import java.net.MalformedURLException; import java.net.UnknownHostException; @@ -30,11 +31,12 @@ * RandomAccess for smb files * * @author <a href="mailto:[EMAIL PROTECTED]">Mario Ivankovits</a> - * @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $ + * @version $Revision: 1.1 $ $Date$ */ class SmbFileRandomAccessContent extends AbstractRandomAccessContent { private final SmbRandomAccessFile raf; + private final InputStream rafis; SmbFileRandomAccessContent(final SmbFile smbFile, final RandomAccessMode mode) throws FileSystemException { @@ -53,6 +55,34 @@ try { raf = new SmbRandomAccessFile(smbFile, modes.toString()); + rafis = new InputStream() + { + public int read() throws IOException + { + return raf.readByte(); + } + + public long skip(long n) throws IOException + { + raf.seek(raf.getFilePointer() + n); + return n; + } + + public void close() throws IOException + { + raf.close(); + } + + public int read(byte b[]) throws IOException + { + return raf.read(b); + } + + public int read(byte b[], int off, int len) throws IOException + { + return raf.read(b, off, len); + } + }; } catch (MalformedURLException e) { @@ -226,5 +256,10 @@ public void writeUTF(String str) throws IOException { raf.writeUTF(str); + } + + public InputStream getInputStream() + { + return rafis; } } Modified: jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/util/MonitorRandomAccessContent.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/util/MonitorRandomAccessContent.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/util/MonitorRandomAccessContent.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/java/org/apache/commons/vfs/util/MonitorRandomAccessContent.java Wed Feb 16 11:25:31 2005 @@ -18,12 +18,13 @@ import org.apache.commons.vfs.RandomAccessContent; import java.io.IOException; +import java.io.InputStream; /** * An RandomAccessContent that provides end-of-stream monitoring. * * @author <a href="mailto:[EMAIL PROTECTED]">Mario Ivankovits</a> - * @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $ + * @version $Revision: 1.1 $ $Date$ */ public class MonitorRandomAccessContent implements RandomAccessContent { @@ -43,7 +44,7 @@ } /** - * Closes this output stream. + * Closes this content. */ public void close() throws IOException { @@ -240,5 +241,10 @@ public String readUTF() throws IOException { return content.readUTF(); + } + + public InputStream getInputStream() + { + return content.getInputStream(); } } Modified: jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/test/ProviderRandomReadTests.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/test/ProviderRandomReadTests.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/test/ProviderRandomReadTests.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/test/ProviderRandomReadTests.java Wed Feb 16 11:25:31 2005 @@ -24,7 +24,7 @@ * RanomdRead-only test cases for file providers. * * @author <a href="mailto:[EMAIL PROTECTED]">Mario Ivankovits</a> - * @version $Revision: 1.1 $ $Date: 2004/06/17 19:29:29 $ + * @version $Revision: 1.1 $ $Date$ */ public class ProviderRandomReadTests extends AbstractProviderTestCase @@ -57,30 +57,37 @@ // read first byte byte c = ra.readByte(); assertEquals(c, TEST_DATA.charAt(0)); + assertEquals("fp", ra.getFilePointer(), 1); // start at pos 4 ra.seek(3); c = ra.readByte(); assertEquals(c, TEST_DATA.charAt(3)); + assertEquals("fp", ra.getFilePointer(), 4); c = ra.readByte(); assertEquals(c, TEST_DATA.charAt(4)); + assertEquals("fp", ra.getFilePointer(), 5); // restart at pos 4 ra.seek(3); c = ra.readByte(); assertEquals(c, TEST_DATA.charAt(3)); + assertEquals("fp", ra.getFilePointer(), 4); c = ra.readByte(); assertEquals(c, TEST_DATA.charAt(4)); + assertEquals("fp", ra.getFilePointer(), 5); // advance to pos 11 ra.seek(10); c = ra.readByte(); assertEquals(c, TEST_DATA.charAt(10)); + assertEquals("fp", ra.getFilePointer(), 11); c = ra.readByte(); assertEquals(c, TEST_DATA.charAt(11)); + assertEquals("fp", ra.getFilePointer(), 12); } finally { Modified: jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/util/DelegatingFileSystemOptionsBuilderTest.java URL: http://svn.apache.org/viewcvs/jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/util/DelegatingFileSystemOptionsBuilderTest.java?view=diff&r1=154071&r2=154072 ============================================================================== --- jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/util/DelegatingFileSystemOptionsBuilderTest.java (original) +++ jakarta/commons/sandbox/vfs/trunk/src/test/org/apache/commons/vfs/util/DelegatingFileSystemOptionsBuilderTest.java Wed Feb 16 11:25:31 2005 @@ -30,7 +30,7 @@ * Some tests for the DelegatingFileSystemOptionsBuilder * * @author <a href="mailto:[EMAIL PROTECTED]">Mario Ivankovits</a> - * @version $Revision: 1.1 $ $Date: 2004/06/28 19:47:05 $ + * @version $Revision: 1.1 $ $Date$ */ public class DelegatingFileSystemOptionsBuilderTest extends TestCase { @@ -100,7 +100,7 @@ catch (FileSystemException e) { assertEquals(e.getCause().getClass(), InvocationTargetException.class); - assertEquals(e.getCause().getCause().getClass(), NumberFormatException.class); + assertEquals(((InvocationTargetException) e.getCause()).getTargetException().getClass(), NumberFormatException.class); } try --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]