正文

Note of java netwrok programming(1)2006-04-18 18:33:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/yexin218/12725.html

分享到:

1.The buffered stream won’t send the data to the server until it gets more data from the underlying stream, but the underlying stream won’t send more data until it gets data from the server, which won’t send data until it gets the data that’s stuck in the buffer~!

2.Finally, when you're done with a stream, close it by invoking its close( ) method. This releases any resources associated with the stream, such as file handles or ports. Once an output stream has been closed, further writes to it throw IOExceptions. However, some kinds of streams may still allow you to do things with the object. For instance, a closed ByteArrayOutputStream can still be converted to an actual byte array and a closed DigestOutputStream can still return its digest.

 

 

3.The inputstream class also allow the programs to back up and reread data they’ve already read.

 Public void mark(int readAheadLimit)

 Public void reset() throws IOException

public boolean markSupported( )

Marking and resetting are usually implemented by storing every byte read from the marked position on in an internal buffer. However, not all input streams support this. Before trying to use marking and resetting, check to see whether the markSupported() method returns true. In order to reread data, mark the current position in the stream with the mark( ) method. At a later point, you can reset the stream to the marked position using the reset( ) method. Subsequent reads then return data starting from the marked position, However, you may not be able to reset as far back as you like. The number of bytes you can read from the mark and still reset is determined by the readAheadLimit argument to mark( ). If you try to reset too far back, an IOException is thrown. Furthermore, there can be only one mark in a stream at any given time. Marking a second location erases the first mark. Ps:

PushbackInputStream does not allow marking and resetting.

4. The first problem is that the output from println() is platform-dependent. Depending on what system runs your code. Lines may sometimes be broken with a linefeed, a carriage return, or a carriage return/linefeed pair. This doesn’t cause problems when writing to the console, but it’s a disaster for writing network clients and servers that must follow a precise protocol. The second problem is that PrintStream assumes the default encoding of the platform on which it’s running. However, this encoding may not be what the server or client expects! The third problem is that prontStream eats all exceptions. This makes printStream suitable for textbook programs such as Hello World, since simple console output can be taught without burdening students with first learning about exception handling and all that implies.

5. Compressing Streams

  Compressing and decompressing data with these classes is almost trivially easy. You simply chain the filter to the underlying stream and read or write it like normal. Supposed that you want to read the compressed file abc.gz

   FileInputStream fin = new FileInputStream(“abc.gz”0

   GZipInputStream  gzin = new GZipInputStream(fin);

  FileOutputStream fout = new FileOutputStream(“abc”);

   Int b =0;

While ((b = gzin.read())!=-1) fout.wirte(b);

Gzin.close(); out.flush();out.close();

 

In fact, ZipoutputStream and ZipInputSteam are more complicated,each file in z zip archive is represented as a ZipEntry object whose getNeame() method returns the original name of the file. For example:

FileInputStream fin = new FileInputStream(“abc.zip”);

ZipInputStream zin = new ZipInputStream(fin);

ZipEntry ze =null;

int b =0;

while((ze=zin.getNextEntry()!=null){

  FileOutPutStream fout = new FIleOutputStream(ze.getName());

while((b = zin.read()!=-1) fout.write(b);

zin.closeEntry();

fout.flush();

fout.close();}

zin.close();

阅读(3745) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册