readLine from a Jar file

0 views
Skip to first unread message

Guy Sussman

unread,
Jun 29, 2004, 12:39:18 PM6/29/04
to
I have a jar (cfg.jar) which contains a configuration file (cfg.txt)
for an application. The format of the configuration file is such that
the '#' char at the beginning of the line indicates comments to be
ignored. The uncommented lines contain key value strings for the
application. Below is the first few lines of the expanded
configuration file
#****************************************************
# Lines beginning with "#" char are comments.
SEQUENCE_SELECT_DISAPPEAR_PAUSE=500
#****************************************************
# Pause duration after sequence finishes
SEQUENCE_SELECT_REAPPEAR_PAUSE=250


The following code outputs no lines of the compressed cfg.txt file.
When the System.out.println line is uncommented in main, the output
looks as follows:

false
#****************************************************
true


public class ReadJarCfg
{
public static void main(String[]args){
try{
MyJarLineReader reader = new MyJarLineReader();
String line = reader.readLine();
while(line != null){
System.out.println(line == null);
if( line.startsWith("#") ){
//System.out.println(line);
line = reader.readLine();
System.out.println(line == null);
}
else{
System.out.println(line);
line = reader.readLine();
}
}
}catch(IOException e2){System.out.println("Exception caught");}
}
}

class MyJarLineReader
{
String cfgJar = new String("C:\\Program
Files\\LptJarResrcs\\ReadJar\\cfg.jar");

private File lptJar = new File(cfgJar);
private JarFile jarFile = null;
private ZipEntry cfgTextZip = new ZipEntry("cfg.txt");
private InputStream iStream;

public MyJarLineReader()throws IOException{
jarFile = new JarFile(lptJar, true, jarFile.OPEN_READ);
iStream = jarFile.getInputStream(cfgTextZip);
}

public String readLine() throws IOException
{
BufferedReader reader = new LineNumberReader( new
InputStreamReader(iStream) );
return reader.readLine();
}
}

I have expanded the cfg.jar file to confirm that it is not corrupted.

Thanks in advance for any help

Chris

unread,
Jun 29, 2004, 1:51:11 PM6/29/04
to
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Guy Sussman wrote:

[snip]


> class MyJarLineReader
> {
> String cfgJar = new String("C:\\Program
> Files\\LptJarResrcs\\ReadJar\\cfg.jar");
>
> private File lptJar = new File(cfgJar);
> private JarFile jarFile = null;
> private ZipEntry cfgTextZip = new ZipEntry("cfg.txt");
> private InputStream iStream;
>
> public MyJarLineReader()throws IOException{
> jarFile = new JarFile(lptJar, true, jarFile.OPEN_READ);
> iStream = jarFile.getInputStream(cfgTextZip);
> }
>
> public String readLine() throws IOException
> {
> BufferedReader reader = new LineNumberReader( new
> InputStreamReader(iStream) );
> return reader.readLine();
> }
> }

[snip]

Hi,
The problem is simple. You're creating a LineNumberReader for each
line you try to read. That's not going to work. LineNumberReader is a
type of BufferedReader. This means it buffers data from the
underlying InputStreamReader, which acquires that buffered data from
the underlying InputStream. The first call to readLine() creates a
BufferedReader. The call to readLine() tells the buffered reader to
read a line. Because it's *buffered*, it actually reads a whole load
of data. That way, next time, it can hopefully return another line
from its own buffer, rather than having to ask the InputStreamReader
for any more data. Buffering is a good thing, because it enhances
performance, but only if you use it properly. Otherwise, it can lose
data, as you see here. What your problem is, is that you are creating
a new BufferedReader (actually LineNumberReader) for each line. This
isn't going to work. You want to create a *single* LineNumberReader
(probably in the constructor) and save it in a variable in place of
the InputStream. Then, for each readLine() call, simply invoke
readLine() on the saved Reader.

Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA4aw0nwjA8LryK2IRAiQLAKCSnbo3MmAObCNSaqF728bV/sb3KgCgnJ6P
/qeQZOJZBM3TWU2DaURE6+c=
=F6uH
-----END PGP SIGNATURE-----

Reply all
Reply to author
Forward
0 new messages