commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Elifarley Callado Coelho Cruz <elifar...@gmail.com>
Subject Re: [VFS] questions and suggestions
Date Tue, 10 May 2005 21:24:45 GMT
Philippe, 

I suggest you use the method 'FileChannel.transferTo' to copy data
between local files. See its Javadocs:

--
Transfers bytes from this channel's file to the given writable byte 
 channel. 
This method is potentially much more efficient than a simple loop 
 that reads from this channel and writes to the target channel. 
 Many operating systems can transfer bytes directly from the 
 filesystem cache to the target channel without actually copying 
 them. 
--

Below is a suggested implementation:

--
	public static long copyFile(File source, File destination) throws IOException {

		checkCopyMove(source, destination, FileCopyKit.COPY);

		long bytesTransfered = -1L;

		FileInputStream input = new FileInputStream(source);
		FileChannel srcChannel = input.getChannel();
		final long srcSize = srcChannel.size();

		FileOutputStream output = new FileOutputStream(destination, false);
		FileChannel destChannel = output.getChannel();
		long destSize = -1L;

		try {
			bytesTransfered = srcChannel.transferTo(0L, srcSize, destChannel);

		} finally {
			try {
				destSize = destChannel.size();
				destChannel.close();
			} catch (Throwable t) {
				// Ignore
			}
		}

		try {
			srcChannel.close();
		} catch (Throwable t) {
			// Ignore
		}

		if (bytesTransfered != srcSize || srcSize != destSize) {
			String message = "Failed to copy full contents from " + source + " to "
					+ destination;
			throw new IOException(message);
		}

		return bytesTransfered;

	}
--

On 5/10/05, Philippe Poulard <Philippe.Poulard@sophia.inria.fr> wrote:
> hi,
> 
> as I'm trying to add a new provider for VFS ("xmldb" scheme), I have
> some questions/suggestions to ask/submit :
> 
> =================================================
> 
> Suggestion : Using NIO
> 
> I've seen that copying an input stream to an output stream is made with
> FileUtil ; however, when dealing with local files, NIO is not used ;
> i've seen that IS and OS are wrapped in monitored IS/OS, but native
> FileIS and FileOS could be used for a fast copy, like this :
> 
>      /**
>       * Perform a fast copy from file to file.
>       * @param input The input to read from.
>       * @param output The output to write to.
>       * @throws IOException When an I/O error occurs.
>       */
>      public static void fastCopy( FileInputStream input,
> FileOutputStream output ) throws IOException {
>          FileChannel fcin = input.getChannel();
>          FileChannel fcout = output.getChannel();
>          ByteBuffer buffer = ByteBuffer.allocateDirect( 1024 );
>          while (true) {
>              buffer.clear();
>              int r = fcin.read( buffer );
>              if (r==-1) {
>                  break;
>              }
>              buffer.flip();
>              fcout.write( buffer );
>          }
>      }
> 
>      /**
>       * Copy an input to an output.
>       * If both arguments are related to files,
>       * a fast copy is performed.
>       * @param input The input to read from.
>       * @param output The output to write to.
>       * @throws IOException When an I/O error occurs.
>       * @see #fastCopy(FileInputStream, FileOutputStream)
>       */
>      public static void copy( InputStream input, OutputStream output )
>              throws IOException {
>          if ( input instanceof FileInputStream && output instanceof
> FileOutputStream ) {
>              // very special case
>              fastCopy( (FileInputStream) input, (FileOutputStream) output);
>              return;
>          }
>          byte[] buf = new byte[1024];
>          int len;
>          while ((len = input.read(buf)) > 0) {
>              output.write(buf, 0, len);
>          }
>          input.close();
>          output.close();
>      }
> Isn't there a way to test if the wrapper could be bypassed if IS and OS
> are good candidates to fast copy ?
> 
> Anyway, in the original code, the IS is put in a byte[] ; isn't that
> dangerous when dealing simultaneously with very large files ?
> 
> =================================================
> 
> Question : why not use java.net.URI parser ?
> 
> I've seen that the code contains its own URI parser ; why not use those
> provided in java.net ?
> Additionally, the java.net.URI parser is improved : although that didn't
> cause any troubles, I encountered files that were stored with
> "file:////" instead of "file:///" ; did I miss something ?
> 
> =================================================
> 
> Suggestion : dealing with opaque URIs
> 
> the FileObject method URL getURL() is somewhat restrictive ; it should
> be replaced by URI getURI() because a FileObject is not necessary
> accessible with an URL
> if the URI is really an URL, it can be easily transformed to an URL
> if the URI is not an URL, the method willgetURL() will fail
> 
> this is important because I also plan to implement some URN schemes, if
> I have enough time
> 
> =================================================
> 
> Suggestion : adding a capability to deal with I/O services
> 
> URIs that represent remote connexions to some serveurs may also support
> "services" ; an example is XML:DB (the new provider that I try to implement)
> with XML:DB it is possible to get a service for special purpose
> interactions with the server, for example a query service.
> Providers that support a new Capability.SERVICE could supply a new
> method such as getService( String name, String version) that gives a
> Service like those defined in XML:DB
> like this :
>      /** A very general purpose service. */
>      public interface Service {
>          public void addParam( Object name, Object value );
>          public void removeParam( Object name );
>          public Map getParams();
>          public Object justDoIt();
>      }
> (just think about better method names)
> 
> =================================================
> 
> Question : I didn't found in the todo list something that looks like a
> synchronized copy ; is there an obvious implementation or nobody ever
> thought about it ?
> 
> =================================================
> 
> Misc : I've seen in the Todo list : "Add more selectors: XPath"
> 
> I use myself VFS to traverse file systems with XPath ; that' fine
> because one can use XPath expressions like this :
> io:file( '/path/to/my/xml/docs' )//*[@is-file][ends-with( name(), '.xml' )]
> I create a wrapper for FileObject (that also extend FileObject) that
> gives an XML-friendly object, that is to say that behaves like an XML
> element that is traversable with XPath
> 
> have a look here :
> http://disc.inria.fr/perso/philippe.poulard/xml/active-tags/io/io.html
> this is an Active Tags module tightly coupled to VFS capabilities
> have a look at the Active Tags frontpage too :
> http://disc.inria.fr/perso/philippe.poulard/xml/active-tags/
> 
> at this moment, the implementation works fine with VFS
> 
> =================================================
> 
> that's all for the moment ; VFS is a great tool, thanks to the
> developers team :)
> 
> --
> Cordialement,
> 
>             ///
>            (. .)
>   -----ooO--(_)--Ooo-----
> |   Philippe Poulard    |
>   -----------------------
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: commons-user-help@jakarta.apache.org
> 
> 


-- 
There are 10 types of people in the world:
Those who understand binary and those who don't

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org


Mime
View raw message