Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   *  jDTAUS - DTAUS fileformat.
   *  Copyright (c) 2005 Christian Schulte <cs@schulte.it>
   *
   *  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 4371 2007-08-23 02:52:01Z 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 _dependency0;

    
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 _bufferSize;

    
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 setLength(long newLengththrows IOException
    {
        if(newLength < 0L)
        {
            throw new IllegalArgumentException(Long.toString(newLength));
        }
        this.assertNotClosed();
        this.getRandomAccessFile().setLength(newLength);
        this. = newLength;
    }
    public long getFilePointer() throws IOException
    {
        this.assertNotClosed();
        return this.getRandomAccessFile().getFilePointer();
    }
    public void setFilePointer(long pos)  throws IOException
    {
        this.assertNotClosed();
        this.getRandomAccessFile().seek(pos);
    }
    public void write(byte[] bufint offint len)  throws IOException
    {
        this.assertNotClosed();
        final RandomAccessFile file = this.getRandomAccessFile();
        final long pointer = file.getFilePointer();
        file.write(bufofflen);
        if(this. !=  &&
            pointer + len > this.)
        {
            this. = file.length();
        }
    }
    public int read(byte[] bufint offint lenthrows IOException
    {
        this.assertNotClosed();
        return this.getRandomAccessFile().read(bufofflen);
    }
    public void read(final OutputStream outthrows 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.read(buf, 0 , buf.length);
                assert read != FileOperations.EOF : "Unexpected end of file.";
                toRead -= read;
                out.write(buf, 0, read);
            } while(toRead > 0L);
        }
    }
    public void write(final InputStream inthrows IOException
    {
        if(in == null)
        {
            throw new NullPointerException("in");
        }
        this.assertNotClosed();
        int read;
        final byte[] buf = this.getDefaultBuffer();
        while((read = in.read(buf, 0, buf.length)) != FileOperations.EOF)
        {
            this.write(buf, 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 RandomAccessFileOperations(final 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 Integer(this.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.
                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