🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Too many open files error thrown from java.io.File

#105
From: Scott
Date: Sun, 04 Apr 2004 22:42
178 lines
5617 bytes
Lori,

Thanks for your post.  Your courage in reading the source for the MP3
library got me doing the same.

Sorry my reply is so long, but I had two streams of questions.  One on Java
object variable management, the other on the files...

MP3File does not expose the ID3 or ID3v2 tag objects.  So I can't read
MP3.ID3.checkForTag().  I will have to make my own instance (or make my own
extension of MP3File class to access thed protected fields).

So in terms of Java object and variable management, this is the right
approach:
// gives me a unique file reference each time through the loop

MP3File mp3 = new MP3File (files[x].getPath());

// the right way to create a unique ID3 object variable to check on the
unique mp3file in the loop

if (new ID3(mp3).checkForTag()) {

// i am adding the ID3 object that represents the current file in the loop.
this is the only way to do it

id3s.add(mp3);

}



In terms of the concept of "open files", let me see if I understand:

* The File object won't use up a system resource.  No need to worry that
there isn't a File.close() [actually, that would explain why there isn't
one]

* RandomAccessFile and related classes, such as that in ID3.checkForTag(),
do use a system resource. Does that happen with the object is created
[RandomAccessFile myfile = new RandomAccessFile(aFileObject)] or only when
an attempt to access the file occurs [e.g., seek() or length()]?

* If an RAF-related exception is thrown and the subroutine is exited, an
open RAF will not be closed -- or at least not until it's GC'd, which may
not be right away.

* So any methods that use RAF-type classes should use a try/finally or some
other mechanism to ensure an RAF is closed() before exiting.

So in the example we're talking about, something like this, where I've added
raf.close() in some spots would be helpful (if not the ideal approach):
public boolean checkForTag() throws IOException

{

// Create random access file

RandomAccessFile raf = new RandomAccessFile(mp3_file, "r");

if (raf.length()<129) {

// file to short for an ID3 tag

raf.close();

return false;

} else {

// go to position where "TAG" must be

long seekPos = raf.length()-128;

raf.seek(seekPos);


byte buffer[] = new byte[3];


if (raf.read(buffer,0,3)!=3) {

// something terrible happened

raf.close();

throw new IOException("Read beyond end of file");

}

String testTag=new String(buffer,0,3,encoding);

if (!testTag.equals("TAG")) {

raf.close();

return false;

} else {

raf.close();

return true;

}

}

}

Hey! congratulaations for getting all the way down here.



Scott





"Lori M Olson (TeamB)" <javadragon@techie.com> wrote in message
news:407073e5$1@newsgroups.borland.com...
> Scott wrote:
>
> > Hi,
> >
> > I'm using an MP3 class library from http://www.vdheide.de/projects.html.
It
> > is the same library in the J-Jukebox project.
> >
> > The library offers a set of classes to read MP3 files.  The main class
is
> > MP3File, which extends File.
> >
> > I've got code that scans all of my MP3 and looks for those that have ID3
> > tags (the older way to mark the artist, song title, etc. in the MP3
file).
> > The code appears below.
> >
> > I've got about 1800 files to scan.  At file # 897, I get a
> > java.io.IOException: Too many open files.
> >
> > Can anyone suggest how to avoid this problem (aside from only handling
897
> > files at a time)?  If there was a File.close() command, I'd happily
execute
> > it.
>
> Your MP3 library is flawed in this respect.  In the ID3 class, the
> checkForTag() method opens a file and fails to close it.  You'll need to
> fix that method if you want to get past this problem.
>
> Since you are new to programming, I'll give you a hint for making this
> fix simpler ... it is considered bad programming practice to have
> multiple return statements within a method.
>
> Poking thru the source also reveals that new MP3File() will create a new
> ID3 object.  So you probably don't need to create one yourself.
>
> -- 
>
> Regards,
>
> Lori Olson (TeamB)
>
> ------------
>
> Save yourself, and everyone else, some time and search the
> newsgroups and the FAQ-O-Matic before posting your next
> question.
>
> Google Advanced Newsgroup Search
>      http://www.google.ca/advanced_group_search
> Other Newsgroup Searches:
>      http://www.borland.com/newsgroups/ngsearch.html
> Joi Ellis's FAQ-O-Matic:
>      http://www.visi.com/~gyles19/fom-serve/cache/1.html
>
Technical Details
Message-ID: <40708fae$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <406edc6e$1@newsgroups.borland.com> <407073e5$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089d0,606a3d8ee4cf8a04
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2004-04-04 16:09:42 PST
Reply-To: "Scott" <scotthpowell@yahoo.com>
From: "Scott" <scotthpowell@yahoo.com>
Newsgroups: borland.public.jbuilder.java.api
References: <406edc6e$1@newsgroups.borland.com> <407073e5$1@newsgroups.borland.com>
Subject: Re: Too many open files error thrown from java.io.File
Date: Sun, 4 Apr 2004 18:42:33 -0400
Lines: 180
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 24.239.150.17
Message-ID: <40708fae$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1081118638 24.239.150.17 (4 Apr 2004 15:43:58 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail