streams input, output, file i/o, tokenization, serialization and compression

Post on 15-Jan-2016

249 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Streams

input, output, FILE I/O, tokenization, serialization and compression

What is a Stream?

• an unformatted sequence of bytes

• There are several stream oriented classed in the java.io package.– InputStream – interface– OutputStream – interface– FileInputStream implements InputStream

Why would I want a stream?

• To input and output data.

• To perform elementary data processing

• Streaming data is data that flows without stopping.

• Higher performance

• Binary data processing

What kind of Streams are there?

• Two kinds:– InputStream (FileInputStream)– OutputStream (FileOutputStream)– you need a File instance to

• make a new instance of a FileInputStream or

• a FileOutputStream

Byte Streams

Introducing java.io

Where are streams used?

• File IO

• Socket IO

• Web-based IO

• Concurrent computing

• Distributed computing

• keyboard and terminal

• printer and the scanner

Streams for input and output• Abstract base classes for input and output functionality. • Defines basic set of methods. • Used to get data from files, other objects, etc.

Two base structures: 1.InputStream/OutputStream

Byte streams 8-bit 2.Reader/Writer

Character streams 16-bit UNICODE

Internationalization Efficiency Buffer (and not byte) operations Better locking scheme

9

Streams and I/O• basic classes for file IO

– FileInputStream, for reading from a file– FileOutputStream, for writing to a file

• Example:Open a file "myfile.txt" for reading

FileInputStream fis = new FileInputStream("myfile.txt");

Open a file "outfile.txt" for writing

FileOutputStream fos = new FileOutputStream ("myfile.txt");

InputStream methods

• int read()• int read(byte[] )• int read(byte[], int, int)

• void close• int available()• skip(long)

OutputStream methods

• void write(int)• void write(byte[] )• void write(byte[], int, int)

• void close()• void flush()

Basic Stream Classes

• FileInputStream

• FileOutputStream

• DataInputStream – byte readByte(), long readLong(), double readDouble()

• DataOutputStream– writeByte(byte), writeLong(long), writeDouble(double)

• PipedInputStream - PipedOutputStream

How do I use a FileInputStream?

public static FileInputStream getFileInputStream(String prompt) {

try {

return new FileInputStream(getReadFile(prompt));

} catch (IOException e) {

System.out.println("Er: FileOutputStream in Futil.java");

}

return null;

}

14

Display File Contents

import java.io.*;public class FileToOut1 { public static void main(String args[]) { try { FileInputStream infile = new FileInputStream("testfile.txt"); byte buffer[] = new byte[50]; int nBytesRead; do { nBytesRead = infile.read(buffer); System.out.write(buffer, 0, nBytesRead); } while (nBytesRead == buffer.length); } catch (FileNotFoundException e) { System.err.println("File not found"); } catch (IOException e) { System.err.println("Read failed"); } }}

copying a file... public static void binaryCopyFile(FileInputStream fis,

FileOutputStream fos)

throws IOException {

byte buffer[] = new byte[512];

int count;

while ((count = fis.read(buffer)) > 0)

fos.write(buffer, 0, count);

}

Byte Example (EOF)

import java.io.*;

public class CopyBytes {

public static void main(String[] args)

throws IOException {

File inputFile = new File("farrago.txt");

File outputFile = new File("outagain.txt");

FileInputStream in = new FileInputStream(inputFile);

FileOutputStream out = new FileOutputStream(outputFile);

int c;

while ((c = in.read()) != -1) //EOF condition test

out.write(c);

in.close();

out.close();

}

}

Working with Filter Streams

• Filters data being read from or written to a stream

• Some streams buffer data, some count data, others convert data to another form. – DataInputStream & DataOutputStream – BufferedInputStream & BufferedOutputStream

– LineNumberInputStream – PushbackInputStream (Character based)– PrintStream (This is an output stream.)

DataInputStream & DataOutputStream

Conceptually, data looks like this, although in binary form (non-text): 19.99 12 Java T-shirt 9.99 8 Java Mug The data might have been created with the following:DataOutputStream out =

new DataOutputStream(new FileOutputStream("invoice1.bin")); for (int i = 0; i < prices.length; i ++) { out.writeDouble(prices[i]); //out.writeChar('\t'); out.writeInt(units[i]); //out.writeChar('\t'); out.writeChars(descs[i]); out.writeChar('\n'); }out.close();

DataInputStream in = new DataInputStream(new FileInputStream("invoice1.bin"));

// break on EOF

while (true) {

price = in.readDouble();

unit = in.readInt();

desc = new StringBuffer(20);

//end of line (carriage return linefeed)

char lineSep = System.getProperty("line.separator").charAt(0);

char chr;

while (((chr = in.readChar()) != lineSep) && chr != -1) {

desc.append(chr);

}

if (chr == -1) break;

System.out.println("You've ordered " + unit + " units of “ +

desc + " at $" + price);

total = total + unit * price;

}

System.out.println("For a TOTAL of: $" + total);

in.close();

write out the bytes

public static boolean writeBytes(File f, byte b[]) { FileOutputStream fos = null;

try {

fos = new FileOutputStream(f);

fos.write(b);

fos.close();

return true;

} catch (IOException e) {

System.out.println("Futil.writeBytes,Could not open" + f);

return false;

}

}

How big is that file?

public static int available(File file) {

FileInputStream fis = null;

int sizeInBytes = -1;

try {

fis = new FileInputStream(file);

sizeInBytes = fis.available();

fis.close();

} catch (IOException e) {

System.out.println("Futil:Could not open file");

}

return sizeInBytes;

}

22

Filters

•Once a stream (e.g., file) has been opened, we can attach filters •Filters make reading/writing more efficient•Most popular filters: • For basic types:

•DataInputStream, DataOutputStream• For objects:

•ObjectInputStream, ObjectOutputStream

23

Writing data to a file using Filters

import java.io.*;public class GenerateData { public static void main(String args[]) { try { FileOutputStream fos = new FileOutputStream("stuff.dat"); DataOutputStream dos = new DataOutputStream(fos); dos.writeInt(2); dos.writeDouble(2.7182818284590451); dos.writeDouble(3.1415926535); dos.close(); fos.close(); } catch (FileNotFoundException e) { System.err.println("File not found"); } catch (IOException e) { System.err.println("Read or write failed"); } }}

24

Reading data from a file using filtersimport java.io.*;public class ReadData { public static void main(String args[]) { try { FileInputStream fis = new FileInputStream("stuff.dat"); DataInputStream dis = new DataInputStream(fis); int n = dis.readInt(); System.out.println(n); for( int i = 0; i < n; i++ ) { System.out.println(dis.readDouble()); } dis.close(); fis.close(); } catch (FileNotFoundException e) { System.err.println("File not found"); } catch (IOException e) { System.err.println("Read or write failed"); } }}

Random-Access Files

• A program can start reading or writing a random-access file at any place and read or write any number of bytes at a time.

• “Random-access file” is an abstraction: any file can be treated as a random-access file.

• You can open a random-access file both for reading and writing at the same time.

Random Access Files

new RandomAccessFile(“data.txt", "rw"); int skipBytes(int) --Moves the file pointer forward

the specified number of bytes void seek(long) --Positions the file pointer just

before the specified byte long getFilePointer() --Returns the current byte

location of the file pointer The RandomAccessFile class implements both the DataInput

and DataOutput interfaces Does not extend stream classNo support for Serialized

Objects

Random-Access Files (cont’d)• A binary file containing fixed-length data

records is suitable for random-access treatment.

• A random-access file may be accompanied by an “index” (either in the same or a different file), which tells the address of each record.

• Tape : CD == Stream : Random-access

Serialization

• a way to write instance to a stream

• Anything that implements the interface serializable can be serialized

• Most java core classes implement serializable.

• All primitive data types can be serialized

29

Object serialization

Write objects to a file, instead of writing primitive types.

Use the ObjectInputStream, ObjectOutputStream classes, the same way that filters are used.

Why do I need serialization?

• Persistence (data storage)

• Transmission via a stream

• comparison

• Distributed computing (CORBA, RMI, etc.)

• serialization is an application of streams.

ObjectOutputStream (Serialization)

FileOutputStream out = new FileOutputStream("theTime");

ObjectOutputStream s = new ObjectOutputStream(out);

s.writeObject("Today"); s.writeObject(new Date()); s.flush(); The writeObject method throws a

NotSerializableException if it's given an object that is not serializable. An object is serializable only if its class implements the Serializable interface.

ObjectInputStream (Deserialization)

FileInputStream in = new FileInputStream("theTime");

ObjectInputStream s = new ObjectInputStream(in);

String today = (String)s.readObject();

Date date = (Date)s.readObject();

ObjectInputStream stream implements the DataInput interface that defines methods for reading primitive data types

How can I use serialization?

• Make an instance of an ObjectOutputStream

• ObjectOutputStream resides in java.io.

• To read back use ObjectInputStream.

• Object o = ois.read();

• oos.write(o);

• Customer c = (Customer)o; //CNFE?

34

Write an object to a fileimport java.io.*;import java.util.*;public class WriteDate { public WriteDate () { Date d = new Date(); try {

FileOutputStream f = new FileOutputStream("date.ser");ObjectOutputStream s = new ObjectOutputStream (f);s.writeObject (d);s.close ();

} catch (IOException e) { e.printStackTrace(); } public static void main (String args[]) { new WriteDate (); }}

35

Read an object from a file

import java.util.*;public class ReadDate { public ReadDate () { Date d = null; ObjectInputStream s = null; try { FileInputStream f = new FileInputStream ("date.ser"); s = new ObjectInputStream (f); } catch (IOException e) { e.printStackTrace(); } try { d = (Date)s.readObject (); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InvalidClassException e) { e.printStackTrace(); } catch (StreamCorruptedException e) { e.printStackTrace(); } catch (OptionalDataException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println ("Date serialized at: "+ d); } public static void main (String args[]) { new ReadDate (); }}

How do I control what is saved?

• I need a special java keyword for this!

• transient is a keyword...for example

class Customer implements Serializable {

transient String name = null;

}

java.util.zip Package

CheckedInputStream & CheckedOutputStream • An input and output stream pair that maintains a checksum as the

data is being read or written.

DeflaterOutputStream & InflaterInputStream • Compresses or uncompresses the data as it is being read or written.

GZIPInputStream & GZIPOutputStream • Reads and writes compressed data in the GZIP format.

ZipInputStream & ZipOutputStream • Reads and writes compressed data in the ZIP format

Save a gzip file• public void saveGzDb(final File writeFile) throws IOException {• if (writeFile == null) return;• FileOutputStream fos• = new FileOutputStream(writeFile);• GZIPOutputStream gos• = new GZIPOutputStream(fos);• ObjectOutputStream oos• = new ObjectOutputStream(gos);• oos.writeObject(addressVector);

• oos.flush();• oos.close();• gos.finish();• fos.close();• System.out.println("saved to:"+writeFile);• }

Read in the gzip object

• FileInputStream fis• = new FileInputStream(readFileName);• GZIPInputStream gis• = new GZIPInputStream(fis);• ObjectInputStream ois• = new ObjectInputStream(gis);• final Object o = ois.readObject();• addressVector = (Vector) o;

Playing Audio from a File

• audio is stored in a file format.

• One way to open and play the audio is via the ULawCodec.

• The ULawCodec resides in the sound package.

• Run the main and it will open and play an au file.

Project ideas

• write a java program that will prompt the user for a directory containing AU files.

• Play all au files contained in the directory or the subdirectories from the root selected by the users.

• Use the sound.UlawCodec class to help you.• You can use the web to get multiple au files.

Whats a codec?

• Coder + decoder = codec.

• Why do I need a codec?

• How many codec do I need?

• Can I writing my own codec?

• When should I write my own codec?

How does the Ulaw Codec work?

• public UlawCodec() {

• String fileName =

• Futil.getReadFileName(

• "select an au file");

• readAUFile(fileName);

• }

How do you get the bytes from a file?

• private void readUlawDataFromAFile(String fileName) throws IOException {

• FileInputStream fis = new FileInputStream(fileName);

• ulawData = readData(fis);• fis.close();• }

Reading from an input stream

• public static byte[] readData(InputStream is) throws IOException {

• AudioStream as = new AudioStream(is);• int length = as.getLength();• byte b[] = new byte[length];• as.read(b, 0, length);• return b;• }

How do you play the sound?

• public static void playFromFile() {

• UlawCodec ulc = new UlawCodec();

• ulc.play();

• //ulc.writeAUFile();

• }

How does play work?

• public void play() {• stop();• AudioData audioData =• new AudioData(ulawData);• audioDataStream = new

AudioDataStream(audioData);• AudioPlayer.player.start(audioDataStream);• }

What is audioData?

• a class in package sun.audio;• public class AudioData {• AudioFormat format;• byte[] buffer;

• public AudioData(byte[] bytes) { /* compiled code */ }

• AudioData(AudioFormat audioFormat, byte[] bytes) { /* compiled code */ }

• }

What is an AudioDataStream?

• public class AudioDataStream extends ByteArrayInputStream {

• AudioData ad;

• public AudioDataStream(AudioData audioData) { /* compiled code */ }

• AudioData getAudioData() { /* compiled code */ }• }

What is an AudioPlayer?• public class AudioPlayer extends Thread {• private AudioDevice devAudio;• private static boolean DEBUG;• public static final AudioPlayer player;

• private static ThreadGroup getAudioThreadGroup() { /* compiled code */ }

• private static AudioPlayer getAudioPlayer() { /* compiled code */ }

• private AudioPlayer() { /* compiled code */ }

• public synchronized void start(InputStream inputStream) { /* compiled code */ }

• public synchronized void stop(InputStream inputStream) { /* compiled code */ }

• public void run() { /* compiled code */ }

• AudioPlayer(sun.audio.AudioPlayer$1 audioPlayer$1) { /* compiled code */ }

top related