Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  jDTAUS Core Utilities
   *  Copyright (c) 2005 Christian Schulte
   *
   *  Christian Schulte, Haldener Strasse 72, 58095 Hagen, Germany
   *  <cs@jdtaus.org> (+49 2331 3543887)
   *
   *  This library is free software; you can redistribute it and/or
   *  modify it under the terms of the GNU Lesser General Public
  *  License as published by the Free Software Foundation; either
  *  version 2.1 of the License, or any later version.
  *
  *  This library is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  *  Lesser General Public License for more details.
  *
  *  You should have received a copy of the GNU Lesser General Public
  *  License along with this library; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  *
  */
 package org.jdtaus.core.io.util;
 
 import  org.jdtaus.core.container.ContainerFactory;
 import  org.jdtaus.core.container.ContextFactory;
 import  org.jdtaus.core.container.ContextInitializer;
 import  org.jdtaus.core.container.Implementation;
 import  org.jdtaus.core.container.ModelFactory;
 import  org.jdtaus.core.container.Properties;
 import  org.jdtaus.core.container.Property;
 import  org.jdtaus.core.container.PropertyException;
 import  org.jdtaus.core.io.FileOperations;
 import  org.jdtaus.core.lang.spi.MemoryManager;

Adapts a java.io.RandomAccessFile to FileOperations.

Author(s):
Christian Schulte
Version:
$Id: RandomAccessFileOperations.java 5034 2008-07-04 11:04:09Z schulte2005 $
 
 public final class RandomAccessFileOperations implements FileOperations
 {
     //--Implementation----------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausImplementation
     // This section is managed by jdtaus-container-mojo.
 
    
Meta-data describing the implementation.
 
     private static final Implementation META =
         ModelFactory.getModel().getModules().
         getImplementation(RandomAccessFileOperations.class.getName());
 // </editor-fold>//GEN-END:jdtausImplementation
 
     //----------------------------------------------------------Implementation--
     //--Constructors------------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausConstructors
     // This section is managed by jdtaus-container-mojo.
 
    
Initializes the properties of the instance.

Parameters:
meta the property values to initialize the instance with.
Throws:
NullPointerException if meta is null.
 
     private void initializeProperties(final Properties meta)
     {
         Property p;
 
         if(meta == null)
         {
             throw new NullPointerException("meta");
         }
 
         p = meta.getProperty("bufferSize");
         this. = ((java.lang.Integerp.getValue()).intValue();
 
     }
 // </editor-fold>//GEN-END:jdtausConstructors
 
     //------------------------------------------------------------Constructors--
     //--Dependencies------------------------------------------------------------
 
 // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausDependencies
     // This section is managed by jdtaus-container-mojo.
 
    
Configured MemoryManager implementation.
 
     private transient MemoryManager dMemoryManager;

    
Gets the configured MemoryManager implementation.

Returns:
the configured MemoryManager implementation.
    private MemoryManager getMemoryManager()
    {
        MemoryManager ret = null;
        if(this. != null)
        {
            ret = this.;
        }
        else
        {
            ret = (MemoryManager) ContainerFactory.getContainer().
                getDependency(RandomAccessFileOperations.class,
                "MemoryManager");
            if(ModelFactory.getModel().getModules().
                getImplementation(RandomAccessFileOperations.class.getName()).
                getDependencies().getDependency("MemoryManager").
                isBound())
            {
                this. = ret;
            }
        }
        if(ret instanceof ContextInitializer && !((ContextInitializer) ret).
            isInitialized(ContextFactory.getContext()))
        {
            ((ContextInitializer) ret).initialize(ContextFactory.getContext());
        }
        return ret;
    }
// </editor-fold>//GEN-END:jdtausDependencies
    //------------------------------------------------------------Dependencies--
    //--Properties--------------------------------------------------------------
// <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:jdtausProperties
    // This section is managed by jdtaus-container-mojo.

    
Property bufferSize.

Serial:
    private int pBufferSize;

    
Gets the value of property bufferSize.

Returns:
the value of property bufferSize.
    private int getBufferSize()
    {
        return this.;
    }
// </editor-fold>//GEN-END:jdtausProperties
    //--------------------------------------------------------------Properties--
    //--FileOperations----------------------------------------------------------

    
Cache for the value of property length.
    private transient long cachedLength = ;
    private static final long NO_CACHEDLENGTH = .;
    public long getLength() throws IOException
    {
        this.assertNotClosed();
        return this. != 
            ? this.
            : ( this. =
            this.getRandomAccessFile().length() );
    }
    public void setLengthlong newLength ) throws IOException
    {
        if ( newLength < 0L )
        {
            throw new IllegalArgumentException( Long.toStringnewLength ) );
        }
        this.assertNotClosed();
        this.getRandomAccessFile().setLengthnewLength );
        this. = newLength;
    }
    public long getFilePointer() throws IOException
    {
        this.assertNotClosed();
        return this.getRandomAccessFile().getFilePointer();
    }
    public void setFilePointerlong pos ) throws IOException
    {
        this.assertNotClosed();
        this.getRandomAccessFile().seekpos );
    }
    public void writebyte[] bufint offint len ) throws IOException
    {
        this.assertNotClosed();
        final RandomAccessFile file = this.getRandomAccessFile();
        final long pointer = file.getFilePointer();
        file.writebufofflen );
        if ( this. !=  &&
            pointer + len > this. )
        {
            this. = file.length();
        }
    }
    public int readbyte[] bufint offint len ) throws IOException
    {
        this.assertNotClosed();
        return this.getRandomAccessFile().readbufofflen );
    }
    public void readfinal OutputStream out ) throws IOException
    {
        if ( out == null )
        {
            throw new NullPointerException"out" );
        }
        this.assertNotClosed();
        int read;
        long toRead = this.getLength();
        final byte[] buf = this.getDefaultBuffer();
        if ( toRead > 0L )
        {
            this.setFilePointer( 0L );
            do
            {
                read = this.readbuf, 0, buf.length );
                assert read != FileOperations.EOF : "Unexpected end of file.";
                toRead -= read;
                out.writebuf, 0, read );
            }
            while ( toRead > 0L );
        }
    }
    public void writefinal InputStream in ) throws IOException
    {
        if ( in == null )
        {
            throw new NullPointerException"in" );
        }
        this.assertNotClosed();
        int read;
        final byte[] buf = this.getDefaultBuffer();
        while ( ( read = in.readbuf, 0, buf.length ) ) != FileOperations.EOF )
        {
            this.writebuf, 0, read );
        }
    }

    
Closes the RandomAccessFile backing the instance.

Throws:
IOException if closing the RandomAccessFile backing the instance fails.
    public void close() throws IOException
    {
        this.assertNotClosed();
        this.getRandomAccessFile().close();
        this. = true;
    }
    //----------------------------------------------------------FileOperations--
    //--RandomAccessFileOperations----------------------------------------------

    
Default temporary buffer.
    private byte[] defaultBuffer;

    
Flags the instance as beeing closed.
    private boolean closed;

    
RandomAccessFile requirement.
    private RandomAccessFile randomAccessFile;

    
Creates a new RandomAccessFileOperations instance adapting file.

Parameters:
file an RandomAccessFile instance to use as a FileOperations implementation.
Throws:
NullPointerException if file is null.
    public RandomAccessFileOperationsfinal RandomAccessFile file )
    {
        super();
        if ( file == null )
        {
            throw new NullPointerException"file" );
        }
        this.initializeProperties.getProperties() );
        this.assertValidProperties();
        this. = file;
    }

    
Gets the random access file operations are performed with.

Returns:
the RandomAccessFile instance operations are performed with or null.
    {
        return this.;
    }

    
Checks configured properties.

Throws:
PropertyException if property bufferSize is negative or 0.
    private void assertValidProperties()
    {
        if ( this.getBufferSize() <= 0 )
        {
            throw new PropertyException( "bufferSize",
                                         new Integerthis.getBufferSize() ) );
        }
    }

    
Checks that the instance is not closed.

Throws:
IOException if the instance is closed.
    private void assertNotClosed() throws IOException
    {
        if ( this. )
        {
            throw new IOException( RandomAccessFileOperationsBundle.getInstance().
                                   getAlreadyClosedText( Locale.getDefault() ) );
        }
    }

    
Getter for property defaultBuffer.

Returns:
a buffer for operations which need temporary memory.
    private byte[] getDefaultBuffer()
    {
        if ( this. == null )
        {
            this. = this.getMemoryManager().
                allocateBytes( this.getBufferSize() );
        }
        return this.;
    }
    //----------------------------------------------RandomAccessFileOperations--
New to GrepCode? Check out our FAQ X