Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

closing inputStream

 
gururaj kulkarni
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!
In the following program Iam closing inputStream after reading 5 members.
But after closing also it is reading next 4 numbers.
Willu just clarify??
import java.io.*;

class ByteArrayInputStreamDemo
{

public static void main(String z[]) throws IOException
{
byte b[]={97,98,99,100,101,102,103,104,105};

ByteArrayInputStream input1=new ByteArrayInputStream(b);


//while(( x=input1.read())!=-1)
for(int i=0;i<5;i++)
{

int x=input1.read();
System.out.print(x+" ");
}

System.out.println("the num of unread bytes are"+input1.available());
System.out.println("Stream is closed");
input1.close();
for(int i=0;i<4;i++)
{
int x=input1.read() ;
System.out.print(x+" ");
}
}
}
 
Apu Nahasapeemapetilon
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you even compile the code snippet?
ByteArrayInputStream does not have a close() method in Java 1.1.8.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure it does - it's inherited from InputStream. Admittedly the 1.1.8 docs arent very good at showing this clearly, which is one of many reasons to use 1.2.2 or 1.3 instead.
It is rather odd that you can still read a stream after it's been closed, at least in this case. Looking carefully at the API, there doesn't seem to be anything said about this behavior one way or another - "closing" an InputStream is never really defined. The base close() method inherited from InputStream is specifically said not to do anything; for the overriding method in ByteInputStream (overridden in Java 1.2 and 1.3 at least) it just says that the method closes the stream, nothing more. Although common sense would be that a closed stream could no longer be used, that doesn't seem to apply here.
My guess is that the close() method is there so that, in case there is anything special that needs to be done to a stream when it's done which would not automatically be done as part of garbage collection or exiting the JVM, or which should not be made to wait until one of those events occurs, then calling the close() method provides a mechanism for that to occur. For example if a socket or file must be closed before another thread can access it, then calling the close() method takes care of it. And so it's good to be in the habit of calling close() for any stream so that you don't forget. However it turns out that it isn't actually necessary for all streams as they are currently implemented.
 
Apu Nahasapeemapetilon
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apu hates,....abhors methods that don't behave.
If you open the stream, it will flow.
Iffin you stream.close() and stream still flow..
Apu want better close().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic