🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #986

Re: OutOfMemoryError

#986
From: Alan Shiers
Date: Tue, 02 Jul 2002 15:07
165 lines
6395 bytes
Hi John,

This is interesting.  I've not come across this before.  If there is a
restriction as to the size of a file one can upload or download, how do
we find out what the default is?

So, to explain, this is how I have things set up now.

1) On the client-side app I read the size of the file and send out the
file size value to the server app via DataOutputStream. I then fill a
byte array with the entire file on the client-side app.

2) Then I use a for loop to send the contents of the file, one byte at a
time, out through the DataOutputStream.

3) On the server-side app I receive the file size value via
DataInputStream and dimension a byte array to accomodate the incoming
file.  

4) I use a for loop to retieve the contents of the file via
DataInputStream one byte at a time on the server-side app.

So far this has been working great, that is until I tried to upload this
enormously huge 60Mb file. That's when I got this OutOfMemoryError.

Maybe I should be using a Vector instead of dimensioning a byte array
everytime? 

Taking my four step algorithm above, how would you change it to safely
download large files?

Should the changes I make be only on the server side or should I
implement a change at both ends?


Following is the function I have been using to recieve files on the
server-side app. The client-side function that sends the file data is
pretty much the same.  This will give you a better idea as to how it is
set up for now:

private void receiveFile(String pathFilename)
{
    //System.out.println("Incoming path: " + pathFilename);
    //this method is called when a file is being uploaded to the server
    //and is to be saved at the specified directory on the server.

    int fileLength = 0;
    //read in the length of the file for the byte array later in this
function
    try
    {
      fileLength = input.readInt();
    }
    catch(IOException io){io.printStackTrace();}

    /*Before we save any file, we need to ensure there is
     *enough free space available in the user's
     *home directory to accomodate it, depending on
     *how much initial space was allotted to the user via
     *his Member Profile. If there is no space left,
     *we simply return out of here.
     */
    boolean enoughSpace = isEnoughSpaceAvailable(fileLength);
    if (enoughSpace == false) return;

    File myFile = new File(pathFilename);
    byte[] b;
    FileOutputStream fileOut = null;

    try
    {
       b = new byte[fileLength];
       //load up the array
       //System.out.println("Byte array length = " + b.length);
       for(int i = 0; i < fileLength; i++)
       {
         b[i] = input.readByte();
       }

       //If there is a problem loading up the array "b",
       //then the exception generated should prevent the code
       //from proceeding here to the creation of a new file on the
server.
       myFile.createNewFile(); //create the file
       //Ensure the file exists before proceeding.
       if(myFile.exists()) //Method exists() throws a SecurityException
if
                          //there is an access violation.
       {
          try
          {
            fileOut = new FileOutputStream(myFile);
          }
          catch(FileNotFoundException fnfe)
          {
            System.out.println("receiveFile() function -
FileNotFoundException\n");
            fnfe.printStackTrace();
          }
          //System.out.println("Finished reading in byte data");
          //write it to the file
          fileOut.write(b);
          //System.out.println("Finished writing out byte data to
file");
          //Close the stream
          if(fileOut != null)
            fileOut.close();
          //System.out.println("Closed stream");
          output.writeInt(session.UPLOAD_SUCCESSFUL);
          output.flush();
       }
    }
    catch(SecurityException se)
    {
      if(DirAdmin.hasInterface)
        myServer.display("SecurityException occurred in method
ReceiveFile");
      else
        System.out.println("SecurityException occurred in method
ReceiveFile");
      try
      {
        output.writeInt(session.UPLOAD_UNSUCCESSFUL_SECURITY_EXCEPTION);
        //Close the stream
        if(fileOut != null)
          fileOut.close();
        output.flush();
      }
      catch(IOException ioe){}
    }
    catch(IOException ioe)
    {
       try
       {
         System.out.println("IOException in receiveFile() method\n" +
                            "FileName = " + pathFilename + "\n" +
                            "byte array length = " + fileLength);
         output.writeInt(session.UPLOAD_UNSUCCESSFUL);
         //Close the stream
         if(fileOut != null)
           fileOut.close();
         output.flush();
       }
       catch(IOException io){}
    }
    catch(Exception e)
    {
        try
        {
          System.out.println("Exception in receiveFile() method\n" +
                             "FileName = " + pathFilename + "\n" +
                             "byte array length = " + fileLength);
          output.writeInt(session.UPLOAD_UNSUCCESSFUL);
          //Close the stream
          if(fileOut != null)
            fileOut.close();
          output.flush();
        }
        catch(IOException io){}
     }
}

Please advise,

Alan Shiers
Technical Details
Message-ID: <3D21C1CA.F2D953E2@hfx.eastlink.ca>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!news.tele.dk!small.news.tele.dk!213.73.101.75!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!dnews
References: <3D20CC5D.B70473C9@hfx.eastlink.ca> <MPG.178acb912dba8f7998b79d@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,28eab50e784856ac
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2002-07-02 07:11:27 PST
Message-ID: <3D21C1CA.F2D953E2@hfx.eastlink.ca>
Date: Tue, 02 Jul 2002 11:07:54 -0400
From: Alan Shiers <ashiers@hfx.eastlink.ca>
Organization: Mustard Seed Software
X-Mailer: Mozilla 4.78 [en] (Win98; U)
X-Accept-Language: en
MIME-Version: 1.0
Newsgroups: borland.public.jbuilder.java.api
Subject: Re: OutOfMemoryError
References: <3D20CC5D.B70473C9@hfx.eastlink.ca> <MPG.178acb912dba8f7998b79d@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 24.222.101.2
X-Trace: dnews 1025618711 24.222.101.2 (2 Jul 2002 07:05:11 -0700)
Lines: 165
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!news.tele.dk!small.news.tele.dk!213.73.101.75!eusc.inter.net!newsfeed01.sul.t-online.de!t-online.de!dnews