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

Adapts a java.io.RandomAccessFile to FileOperations.

Author(s):
Christian Schulte
Version:
$Id: RandomAccessFileOperations.java 3210 2007-04-29 21:36:10Z schulte2005 $
 
 public final class RandomAccessFileOperations implements FileOperations
 {
     //--Implementation----------------------------------------------------------
 
     // 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());
 
     //----------------------------------------------------------Implementation--
     //--Constructors------------------------------------------------------------
 
     // 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.
 
     protected void initializeProperties(final Properties meta)
     {
         Property p;
 
         if(meta == null)
         {
             throw new NullPointerException("meta");
         }
 
         p = meta.getProperty("bufferSize");
         this. = ((java.lang.Integerp.getValue()).intValue();
 
     }
 
     //------------------------------------------------------------Constructors--
     //--Properties--------------------------------------------------------------
 
     // 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.
 
     protected int getBufferSize()
     {
         return this.;
     }
 
 
     //--------------------------------------------------------------Properties--
     //--FileOperations----------------------------------------------------------

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

    
Default temporary buffer.

Serial:
    private byte[] defaultBuffer;

    
RandomAccessFile requirement.
    private transient 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()));
        }
    }

    
Getter for property defaultBuffer.

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