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

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

Upload: patience-shireman

Post on 15-Jan-2016

249 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Streams input, output, FILE I/O, tokenization, serialization and compression

Streams

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

Page 2: 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

Page 3: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 4: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 5: Streams input, output, FILE I/O, tokenization, serialization and compression

Byte Streams

Page 6: Streams input, output, FILE I/O, tokenization, serialization and compression

Introducing java.io

Page 7: Streams input, output, FILE I/O, tokenization, serialization and compression

Where are streams used?

• File IO

• Socket IO

• Web-based IO

• Concurrent computing

• Distributed computing

• keyboard and terminal

• printer and the scanner

Page 8: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 9: Streams input, output, FILE I/O, tokenization, serialization and compression

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");

Page 10: Streams input, output, FILE I/O, tokenization, serialization and compression

InputStream methods

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

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

Page 11: Streams input, output, FILE I/O, tokenization, serialization and compression

OutputStream methods

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

• void close()• void flush()

Page 12: Streams input, output, FILE I/O, tokenization, serialization and compression

Basic Stream Classes

• FileInputStream

• FileOutputStream

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

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

• PipedInputStream - PipedOutputStream

Page 13: Streams input, output, FILE I/O, tokenization, serialization and compression

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;

}

Page 14: Streams input, output, FILE I/O, tokenization, serialization and compression

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"); } }}

Page 15: Streams input, output, FILE I/O, tokenization, serialization and compression

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);

}

Page 16: Streams input, output, FILE I/O, tokenization, serialization and compression

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();

}

}

Page 17: Streams input, output, FILE I/O, tokenization, serialization and compression

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.)

Page 18: Streams input, output, FILE I/O, tokenization, serialization and compression

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();

Page 19: Streams input, output, FILE I/O, tokenization, serialization and compression

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();

Page 20: Streams input, output, FILE I/O, tokenization, serialization and compression

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;

}

}

Page 21: Streams input, output, FILE I/O, tokenization, serialization and compression

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;

}

Page 22: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 23: Streams input, output, FILE I/O, tokenization, serialization and compression

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"); } }}

Page 24: Streams input, output, FILE I/O, tokenization, serialization and compression

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"); } }}

Page 25: Streams input, output, FILE I/O, tokenization, serialization and compression

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.

Page 26: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 27: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 28: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 29: Streams input, output, FILE I/O, tokenization, serialization and compression

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.

Page 30: Streams input, output, FILE I/O, tokenization, serialization and compression

Why do I need serialization?

• Persistence (data storage)

• Transmission via a stream

• comparison

• Distributed computing (CORBA, RMI, etc.)

• serialization is an application of streams.

Page 31: Streams input, output, FILE I/O, tokenization, serialization and compression

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.

Page 32: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 33: Streams input, output, FILE I/O, tokenization, serialization and compression

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?

Page 34: Streams input, output, FILE I/O, tokenization, serialization and compression

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 (); }}

Page 35: Streams input, output, FILE I/O, tokenization, serialization and compression

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 (); }}

Page 36: Streams input, output, FILE I/O, tokenization, serialization and compression

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;

}

Page 37: Streams input, output, FILE I/O, tokenization, serialization and compression

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

Page 38: Streams input, output, FILE I/O, tokenization, serialization and compression

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);• }

Page 39: Streams input, output, FILE I/O, tokenization, serialization and compression

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;

Page 40: Streams input, output, FILE I/O, tokenization, serialization and compression

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.

Page 41: Streams input, output, FILE I/O, tokenization, serialization and compression

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.

Page 42: Streams input, output, FILE I/O, tokenization, serialization and compression

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?

Page 43: Streams input, output, FILE I/O, tokenization, serialization and compression

How does the Ulaw Codec work?

• public UlawCodec() {

• String fileName =

• Futil.getReadFileName(

• "select an au file");

• readAUFile(fileName);

• }

Page 44: Streams input, output, FILE I/O, tokenization, serialization and compression

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();• }

Page 45: Streams input, output, FILE I/O, tokenization, serialization and compression

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;• }

Page 46: Streams input, output, FILE I/O, tokenization, serialization and compression

How do you play the sound?

• public static void playFromFile() {

• UlawCodec ulc = new UlawCodec();

• ulc.play();

• //ulc.writeAUFile();

• }

Page 47: Streams input, output, FILE I/O, tokenization, serialization and compression

How does play work?

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

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

Page 48: Streams input, output, FILE I/O, tokenization, serialization and compression

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 */ }

• }

Page 49: Streams input, output, FILE I/O, tokenization, serialization and compression

What is an AudioDataStream?

• public class AudioDataStream extends ByteArrayInputStream {

• AudioData ad;

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

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

Page 50: Streams input, output, FILE I/O, tokenization, serialization and compression

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 */ }