I've read messages of people complaining about OutOfMemoryErrors so I post my 
solution to the problem...

To upload very large files in a database store I had to make these changes:

1. Remove the Logging filter from web.xml. The Logging Filter replaces the 
Tomcat ServletInputStream with a XServletInputStreamFacade. These stream 
keeps in memory all the read data in a buffer called XByteBuffer. If I have 
understood the source correctly, after an upload you end with the file in 
memory in two places: NodeRevisionContent and XByteBuffer.

2. Use a NodeRevisionContent that writes all bytes in a temporary file (in the 
attachment).

After closing the stream returned by streamContent() this NodeRevisionContent 
can't be used anymore, because the file is deleted. Maybe this could be a 
problem with some stores...

getContentBytes() is a dangerous method, but is used only by ExtendedStore to 
cache small files.

With these changes you can start Tomcat with default memory settings and 
upload very large files without any problem. Now I'm writing a 
NodeRevisionContent that uses a fixed amount of vm memory to keep in memory 
small files.

Best regards,
-- 
Davide Savazzi
/*
 * $Header: /cvs/star/slide-oracle-store/src/java/org/apache/slide/content/NodeRevisionContent.java,v 1.4 2004/05/12 08:32:30 dsavazzi Exp $
 * $Revision: 1.4 $
 * $Date: 2004/05/12 08:32:30 $
 *
 * ====================================================================
 *
 * Copyright 1999-2002 The Apache Software Foundation 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package org.apache.slide.content;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import org.apache.slide.common.ObjectValidationFailedException;
import org.apache.slide.util.Messages;


/**
 * Encapsultes the contents of a revision.
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Remy Maucherat</a>
 * @author Juergen Pill
 * @version $Revision: 1.4 $
 */
public final class NodeRevisionContent implements Serializable {

    // Public Methods
    
    public InputStream streamContent() throws IOException {
        return new FilterInputStream(new FileInputStream(fsBuffer)) {
            public void close() throws IOException {
                try {
                    super.close();
                } finally {
                    freeContent();
                }
            }
        };
    }
    
    public void setContent(InputStream in) {
        try {
            fsBuffer = File.createTempFile(
                "Slide", Long.toString(System.currentTimeMillis()));
            OutputStream out = new FileOutputStream(fsBuffer);
            try {
                copy(in, out, STREAM_BUFFER_SIZE);
            } finally {
                out.close();
            }
        } catch (IOException ioe) {
            // TODO
            ioe.printStackTrace();
        }
    }
    
    public void freeContent() {
        if (fsBuffer != null) {
            fsBuffer.delete();
        }
    }
    
    /**
     * @deprecated
     */
    public byte[] getContentBytes() {
        if (fsBuffer == null) return null;
        byte[] result = null;
        
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            InputStream in = new FileInputStream(fsBuffer);
            try {
                copy(in, out, STREAM_BUFFER_SIZE);
                result = out.toByteArray();
            } finally {
                in.close();
            }
        } catch (IOException ioe) {
            // TODO
            ioe.printStackTrace();
        }
        
        return result;
    }
    
    /**
     * Validate.
     */
    public void validate() {
        if (fsBuffer == null)
            throw new ObjectValidationFailedException
                (Messages.message
                 (NodeRevisionContent.class.getName() + ".noContent"));
    }

    /**
     ** Read the data from the stream and return the result in a byte array.
     ** Return null in case of an error.
     **/
    public static byte[] read(InputStream in) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        copy(in, out, STREAM_BUFFER_SIZE);
        return out.toByteArray();
    }
    
    public static void copy(InputStream in, OutputStream out, int bufferSize) 
        throws IOException 
    {
        int read = 0;
        byte buffer[] = new byte[bufferSize];
        while ((read = in.read(buffer, 0, bufferSize)) != -1) {
            out.write(buffer, 0, read);
        }
    }
    
    
    // Attributes
    
    protected File fsBuffer = null;
    protected static final int STREAM_BUFFER_SIZE = 10 * 1024; // 10KB
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to