Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

  • Home
  • Materials
  • Java Glossary
  • Source Example
  • Cheat sheet (S1-S6)
  • Grading
  • Links
Session 10

Some tricks with streams

Streams can read data from different places

If the data has to be read from the console, Java standard input/output streams can be used:

InputStream myInput = System.in;
OutputStream myOutput = System.out;

To read the data from the Internet, use the class URL:

 InputStream myInput = new URL("http://ut.ee/").openStream();

The classes ZipInputStream and ZipOutputStream can be used for reading/writing data from/into zip files. Here is an example of zipping a file:

import java.io.*;
import java.util.zip.*;

public class Zipper {
  public static void toZip(String fileToZip, String zipFile) throws Exception {
    ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile));
    FileInputStream inputFile = new FileInputStream(fileToZip);
    // prepare for zipping
    zipOut.putNextEntry(new ZipEntry(fileToZip));
    // read from the file and write into zip file
    byte[] buf = new byte[1024]; 
    int len;
    while ((len = inputFile.read(buf)) > 0) {
      zipOut.write(buf, 0, len); // (data, offset, length)
    }
    inputFile.close();
    zipOut.closeEntry();
    zipOut.close();
  }
}

ByteArrayOutputStream

Sometimes it is more convenient to process data in one unit, not in a stream. If the data is available only as a byte stream, it is possible to put all the bytes into byte[]. The ByteArrayOutputStream class helps merge the bytes:

InputStream myStream = new FileInputStream("myPicture.jpg");
ByteArrayOutputStream myCollector = new ByteArrayOutputStream(); // collect all bytes here
byte[] buf = new byte[1024];
int len;
while ((len = myStream.read(buf)) > 0) { // check and assignment as a loop expression
  myCollector.write(buf, 0, len); // add the next portion of bytes
}
myStream.close();
byte[] myFile = myCollector.toByteArray(); // all bytes as one unit

There is an analogical class for the output - ByteArrayInputStream.

Be careful - the file might not fit into the RAM!

Do not forget to close the stream

Each stream has the close method. If this method is not used after the program has finished its work with the resource, some weird cases may happen: the buffered output stream may not return the data from the buffer, the operating system can limit the number of the opened files or block the file. Therefore, do not forget to close the resource!

InputStream and OutputStream are not the only input and output streams

We have discussed the classes InputStreamReader and OutputStreamWriter. These are the subclasses of the abstract classes Reader and Writer. However, Reader and Writer form another input/output hierarchy which operate on characters.

Do not forget to set the character set

Whenever an input/output stream is used, set the character encoding. However, some classes like FileReader and FileWriter do not allow to set the encoding and use the encoding set of the operating system. Try to avoid such classes since the default encoding of the operating system may not be UTF-8 - this may cause problems on other computers when the data is read or written. Instead of the FileReader and FileWriter classes use InputStreamReader and OutputStreamWriter.

The length of the stream is not set

An input stream is a structure from where the bytes are read. The stream is endless; therefore, the input stream does not have methods like size or length which would help define the number of the left bytes. On the other hand, the stream has the available() method.

Session 10
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment