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;
 import  org.jdtaus.core.logging.spi.Logger;

Implementation of elementary I/O operations in heap memory.

This implementation performs IO in memory. The value of property length is limited to a maximum of Integer.MAX_VALUE (4 GB).

Author(s):
Christian Schulte
Version:
$Id: MemoryFileOperations.java 2927 2007-04-20 19:53:53Z schulte2005 $
 
 public final class MemoryFileOperations implements
     FileOperations, SerializableCloneable
 {
 
     //--Fields------------------------------------------------------------------
 
    
Data to operate on.

Serial:
 
     private byte[] data = { (byte) 0 };

    
FilePointer.

Serial:
 
     private long filePointer = 0L;

    
Actual length.

Serial:
 
     private int length = 0;

    
Default temporary buffer.

Serial:
 
     private byte[] defaultBuffer;
 
     //------------------------------------------------------------------Fields--
     //--Implementation----------------------------------------------------------
 
     // This section is generated by jdtaus-source-plugin.
 
    
Meta-data describing the implementation.
 
     private static final Implementation META =
         ModelFactory.getModel().getModules().
         getImplementation(MemoryFileOperations.class.getName());
 
     //----------------------------------------------------------Implementation--
     //--Constructors------------------------------------------------------------
 
     // This section is generated by jdtaus-source-plugin.
 
    
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 generated by jdtaus-source-plugin.

    
Property bufferSize.

Serial:
    private int _bufferSize;

    
Gets the value of property bufferSize.

Returns:
the value of property bufferSize.
    protected int getBufferSize()
    {
        return this.;
    }
    //--------------------------------------------------------------Properties--
    //--Dependencies------------------------------------------------------------
    // This section is generated by jdtaus-source-plugin.

    
Configured MemoryManager implementation.
    private transient MemoryManager _dependency1;

    
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(MemoryFileOperations.class,
                "MemoryManager");
            if(ModelFactory.getModel().getModules().
                getImplementation(MemoryFileOperations.class.getName()).
                getDependencies().getDependency("MemoryManager").
                isBound())
            {
                this. = ret;
            }
        }
        if(ret instanceof ContextInitializer && !((ContextInitializer) ret).
            isInitialized(ContextFactory.getContext()))
        {
            ((ContextInitializer) ret).initialize(ContextFactory.getContext());
        }
        return ret;
    }
    
Configured Logger implementation.
    private transient Logger _dependency0;

    
Gets the configured Logger implementation.

Returns:
the configured Logger implementation.
    private Logger getLogger()
    {
        Logger ret = null;
        if(this. != null)
        {
           ret = this.;
        }
        else
        {
            ret = (Logger) ContainerFactory.getContainer().
                getDependency(MemoryFileOperations.class,
                "Logger");
            if(ModelFactory.getModel().getModules().
                getImplementation(MemoryFileOperations.class.getName()).
                getDependencies().getDependency("Logger").
                isBound())
            {
                this. = ret;
            }
        }
        if(ret instanceof ContextInitializer && !((ContextInitializer) ret).
            isInitialized(ContextFactory.getContext()))
        {
            ((ContextInitializer) ret).initialize(ContextFactory.getContext());
        }
        return ret;
    }
    //------------------------------------------------------------Dependencies--
    //--FileOperations----------------------------------------------------------
    public long getLength()
    {
        return this.;
    }

    

Throws:
IllegalArgumentException if newLength is negative or greater than Integer.MAX_VALUE.
    public void setLength(final long newLength)
    {
        if(newLength < 0L || newLength > .)
        {
            throw new IllegalArgumentException(Long.toString(newLength));
        }
        this.resize((intnewLength);
        if(this. > this..length)
        {
            this. = this..length;
        }
        this. = (intnewLength;
    }
    public long getFilePointer()
    {
        return this.;
    }
    public void setFilePointer(final long pos)
    {
        // Preconditions.
        if(pos < 0L || pos > .)
        {
            throw new IllegalArgumentException(Long.toString(pos));
        }
        this. = pos;
    }
    public int read(final byte[] buffinal int offfinal int len)
    {
        final int ret;
        // Preconditions.
        if(buf == null)
        {
            throw new NullPointerException("buf");
        }
        if(off < 0)
        {
            throw new ArrayIndexOutOfBoundsException(off);
        }
        if(len < 0)
        {
            throw new ArrayIndexOutOfBoundsException(len);
        }
        if(off + len > buf.length)
        {
            throw new ArrayIndexOutOfBoundsException(off + len);
        }
        if(this. + len > .)
        {
            throw new ArrayIndexOutOfBoundsException(.);
        }
        if(len == 0)
        {
            ret = 0;
        }
        else if(this. >= this.)
        {
            // EOF
            ret = -1;
        }
        else if(this. + len > this.)
        {
            // less than len byte before EOF
            final int remaining = (int) (this. - this.);
            System.arraycopy(this., (intthis.bufoff,
                remaining);
            this. += remaining;
            ret = remaining;
        }
        else
        {
            // copy len byte into buf.
            System.arraycopy(this., (intthis.bufofflen);
            this. += len;
            ret = len;
        }
        return ret;
    }
    public void write(final byte[] buffinal int offfinal int len)
    {
        // Preconditions.
        if(buf == null)
        {
            throw new NullPointerException("buf");
        }
        if(off < 0)
        {
            throw new ArrayIndexOutOfBoundsException(off);
        }
        if(len < 0)
        {
            throw new ArrayIndexOutOfBoundsException(len);
        }
        if(off + len > buf.length)
        {
            throw new ArrayIndexOutOfBoundsException(off + len);
        }
        final long newLen = this. + len;
        if(newLen > .)
        {
            throw new ArrayIndexOutOfBoundsException(.);
        }
        if(newLen > this. - 1L)
        {
            this.setLength(newLen);
        }
        System.arraycopy(bufoffthis., (intthis.len);
        this. += len;
    }
    public void read(final OutputStream outthrows IOException
    {
        if(out == null)
        {
            throw new NullPointerException("out");
        }
        out.write(this., 0, this.);
    }
    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--
    //--MemoryFileOperations----------------------------------------------------

    
Creates a new MemoryFileOperations instance of no length.
    public MemoryFileOperations()
    {
        this.initializeProperties(.getProperties());
        this.assertValidProperties();
    }

    
Creates a new MemoryFileOperations instance holding buf.

Parameters:
buf bytes to initialize the instance with.
Throws:
NullPointerException if buf is null.
    public MemoryFileOperations(final byte[] buf)
    {
        this();
        if(buf == null)
        {
            throw new NullPointerException("buf");
        }
        this. = buf;
        this. = buf.length;
        this. = 0L;
    }

    
Gets the file contents.

Returns:
the file contents of the instance.
Throws:
OutOfMemoryError if not enough memory is available.
    public byte[] getData()
    {
        final int length = (intthis.getLength();
        final byte[] ret = this.getMemoryManager().allocateBytes(length);
        System.arraycopy(this., 0, ret, 0, length);
        return ret;
    }

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

    
Resizes the internal buffer.

Parameters:
newSize maximum size the internal buffer needs to hold.
    private void resize(int newSize)
    {
        final int oldLength = this..length;
        while(this..length < newSize)
        {
            final byte[] newData = this.getMemoryManager().
                allocateBytes(this..length * 2);
            Arrays.fill(newData, (byte) 0);
            System.arraycopy(this., 0, newData, 0, this..length);
            this. = newData;
        }
        if(oldLength != this..length &&
            this.getLogger().isDebugEnabled())
        {
            final MessageFormat fmt = MemoryFileOperationsBundle.
                getLogResizeMessage(Locale.getDefault());
            this.getLogger().debug(fmt.format(new Object[] {
                new Long(this..length) }));
        }
    }
    //----------------------------------------------------MemoryFileOperations--
    //--Object------------------------------------------------------------------

    
Indicates whether some other object is equal to this one by comparing the contents from memory.

Parameters:
o the reference object with which to compare.
Returns:
true if this object is the same as o; false otherwise.
    public boolean equals(final Object o)
    {
        boolean ret = o == this;
        final MemoryFileOperations that;
        if(!ret && o instanceof MemoryFileOperations)
        {
            that = (MemoryFileOperationso;
            ret = Arrays.equals(this.getData(), that.getData());
        }
        return ret;
    }

    
Returns a hash code value for this object.

Returns:
a hash code value for this object.
    public int hashCode()
    {
        return this.getData().hashCode();
    }

    
Creates and returns a deep copy of this object.

Returns:
a clone of this instance.
    public Object clone()
    {
        try
        {
            return super.clone();
        }
        catch(CloneNotSupportedException e)
        {
            throw new AssertionError(e);
        }
    }
    //------------------------------------------------------------------Object--
New to GrepCode? Check out our FAQ X