java io c++ streamsrazvanm/600.120/java-io-c... · 22.10.2004  · streams of characters reader...

31
Java IO and C++ Streams October 22, 2004 Operator Overloading in C++ - 2004-10-21 – p. 1/31

Upload: others

Post on 25-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Java IOand

C++ StreamsOctober 22, 2004

Operator Overloading in C++ - 2004-10-21 – p. 1/31

Page 2: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Outline

Java IO

InputStream/OutputStream

FilterInputStream/FilterOutputStream

DataInputStream/DataOutputStream

Reader/Writer

Encodings

C++ Streams

Operator Overloading in C++ - 2004-10-21 – p. 2/31

Page 3: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

java.io - IntroTwo main abstractions:

streams of bytesInputStream

abstract classis the superclass of all classes representing aninput stream of bytes

OutputStreamabstract classis the superclass of all classes representing anoutput stream of bytes

streams of characters

Operator Overloading in C++ - 2004-10-21 – p. 3/31

Page 4: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

java.io - IntroTwo main abstractions:

streams of bytes

streams of charactersReader

abstract classis the superclass of all classes representing aninput stream of characters

Writerabstract classis the superclass of all classes representing anoutput stream of characters

Operator Overloading in C++ - 2004-10-21 – p. 4/31

Page 5: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

OutputStreammust always provide at least a method that writes onebyte of output

methods:

void close()void flush()void write(byte[] b)void write(byte[] b, int off, int len)abstract void write(int b)

subclasses

Operator Overloading in C++ - 2004-10-21 – p. 5/31

Page 6: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

OutputStream (cont)subclasses:

ByteArrayOutputStreamdata is written into a byte arraythe buffer automatically grows as data is written toit

FileOutputStreamwriting data to a File or to a FileDescriptormeant for writing streams of raw byte

FilterOutputStreamObjectOutputStreamPipedOutputStream

Operator Overloading in C++ - 2004-10-21 – p. 6/31

Page 7: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

OutputStream (cont)subclasses:

ByteArrayOutputStreamFileOutputStreamFilterOutputStream

superclass of all classes that filter output streamssubclasses: BufferedOutputStream,CheckedOutputStream, CipherOutputStream,DataOutputStream, DeflaterOutputStream,DigestOutputStream, PrintStream

ObjectOutputStreamPipedOutputStream

Operator Overloading in C++ - 2004-10-21 – p. 7/31

Page 8: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

OutputStream (cont)subclasses:

ByteArrayOutputStreamFileOutputStreamFilterOutputStreamObjectOutputStream

writes primitive data types and graphs of Javaobjectsonly objects that support the java.io.Serializableinterface can be written to streamsprimitive data types can also be written

PipedOutputStreamcan be connected to a piped input stream tocreate a communications pipe

Operator Overloading in C++ - 2004-10-21 – p. 8/31

Page 9: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

FilterOutputStreamwrites data to another OutputStream

two kind of subclasses:processing of the data:

BufferedOutputStreamCheckedOutputStreamCipherOutputStreamDeflaterOutputStream· GZIPOutputStream· ZipOutputStreamDigestOutputStream

provides an expanded interface:DataOutputStreamPrintStream: polymorphic print and println, printf

Operator Overloading in C++ - 2004-10-21 – p. 9/31

Page 10: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

DataOutputStreamExtended interface provided by DataOutputStream:void writeBoolean(boolean v)void writeByte(int v)void writeBytes(String s)void writeChar(int v)void writeChars(String s)void writeDouble(double v)void writeFloat(float v)void writeInt(int v)void writeLong(long v)void writeShort(int v)void writeUTF(String str)

Operator Overloading in C++ - 2004-10-21 – p. 10/31

Page 11: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

OutputStream - ExampleFileOutputStream fileOut =

new FileOutputStream("myfile");// add bufferingBufferedOutputStream bufOut =

new BufferedOutputStream(fileOut);// add compressionGZIPOutputStream gzOut =

new GZIPOutputStream(bufOut);// interface for binary dataDataOutputStream out =

new DataOutputStream(gzOut);// some writingout.writeInt(n);out.writeDouble(d);

Operator Overloading in C++ - 2004-10-21 – p. 11/31

Page 12: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

OutputStream - What if?FileOutputStream fileOut =

new FileOutputStream("myfile");BufferedOutputStream bufOut =

new BufferedOutputStream(fileOut);GZIPOutputStream gzOut =

new GZIPOutputStream(bufOut);DataOutputStream out1 =

new DataOutputStream(gzOut);DataOutputStream out2 =

new DataOutputStream(bufOut);

out1.writeInt(n);out2.writeDouble(d);

Operator Overloading in C++ - 2004-10-21 – p. 12/31

Page 13: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

OutputStream - What if?FileOutputStream fileOut =

new FileOutputStream("myfile");BufferedOutputStream bufOut =

new BufferedOutputStream(fileOut);GZIPOutputStream gzOut =

new GZIPOutputStream(bufOut);DataOutputStream out1 =

new DataOutputStream(gzOut);DataOutputStream out2 =

new DataOutputStream(bufOut);

out1.writeInt(n);out2.writeDouble(d);

The output will be corrupted!

Operator Overloading in C++ - 2004-10-21 – p. 13/31

Page 14: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

OutputStream - A common construction

DataOutputStream out =new DataOutputStream(

new GZIPOutputStream(new BufferedOutputStream(

FileOutputStream("myfile"))));

out.writeInt(n);out.writeDouble(d);

Operator Overloading in C++ - 2004-10-21 – p. 14/31

Page 15: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

InputStreamsimilar with OutputStream but in reverse

subclasses:ByteArrayInputStreamFileInputStreamFilterInputStreamObjectInputStreamPipedInputStreamSequenceInputStream

represents the logical concatenation of other inputstreams

Operator Overloading in C++ - 2004-10-21 – p. 15/31

Page 16: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

The other world: charactersWriter

methods that a subclass must implement are:write(char[], int, int), flush(), and close()subclasses: BufferedWriter, CharArrayWriter,FilterWriter, OutputStreamWriter, FileWriter,PipedWriter, PrintWriter, StringWriter

Readermethods that a subclass must implement areread(char[], int, int) and close()subclasses: BufferedReader, CharArrayReader,FilterReader, InputStreamReader, PipedReader,StringReader

What is the big difference?

Operator Overloading in C++ - 2004-10-21 – p. 16/31

Page 17: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

The other world: charactersWriter

methods that a subclass must implement are:write(char[], int, int), flush(), and close()subclasses: BufferedWriter, CharArrayWriter,FilterWriter, OutputStreamWriter, FileWriter,PipedWriter, PrintWriter, StringWriter

Readermethods that a subclass must implement areread(char[], int, int) and close()subclasses: BufferedReader, CharArrayReader,FilterReader, InputStreamReader, PipedReader,StringReader

What is the big difference? Encodings!

Operator Overloading in C++ - 2004-10-21 – p. 17/31

Page 18: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

InputStreamReader/OutputStreamWriter

InputStreamReaderis a bridge from byte streams to character streamsreads bytes and decodes them into characters usinga specified charset

OutputStreamWriterthe opposite of InputStreamReader

Operator Overloading in C++ - 2004-10-21 – p. 18/31

Page 19: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

EncodingsJDKTM 5.0 Documentation

→ Tool Docs→ Internationalization Tools

→ native2ascii→ Supported Encodings

docs/guide/intl/encoding.doc.html

Operator Overloading in C++ - 2004-10-21 – p. 19/31

Page 20: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Another Universe:

C++ Streams

Operator Overloading in C++ - 2004-10-21 – p. 20/31

Page 21: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Reference

The C++ Programming Language, Third

Edition by Bjarne Stroustrup. Published by

Addison Wesley Longman, Inc.

Operator Overloading in C++ - 2004-10-21 – p. 21/31

Page 22: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Standard StreamsSeveral standard streams are declared in <iostream>:ostream cout // standard output stream

// of charostream cerr // standard unbuffered output

// stream for error messagesostream clog // standard output stream for

// error messageswostream wcout; // wide stream ver. of coutwostream wcerr; // wide stream ver. of cerrwostream wclog; // wide stream ver. of clog

Operator Overloading in C++ - 2004-10-21 – p. 22/31

Page 23: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

istreamProvides:

»get

getline

read

gcount

putback

tie

Operator Overloading in C++ - 2004-10-21 – p. 23/31

Page 24: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

ostreamProvides:

«put

write

Operator Overloading in C++ - 2004-10-21 – p. 24/31

Page 25: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Statebool good() const; // next op. might succeedbool eof() const; // end of input seenbool fail() const; // next op. will failbool bad() const; // stream is corruptediostate rdstate() const; // get io state flagsvoid clear(iostate f = goodbit);

// set io state flagsvoid setstate(iostate f) {clear(rdstate()|f); // add f to iostate flags

}operator void*() const; // nonzero if !fail()bool operator!() const {return fail();

}

Operator Overloading in C++ - 2004-10-21 – p. 25/31

Page 26: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Formatingstatic const fmtflagsskipws, // skip whitespace on inputleft // pad after valueright, // pad before valueinternal, // pad between sign and valueboolalpha, // use symbolic representationdec, // base 10 output (decimal)hex, // base 16 output (hexadecimal)oct, // base 8 output (octal)scientific, // d.ddddddEddfixed, // dddd.ddshowbase, // prefix oct by 0 and hex by 0xshowpoint, // print trailing zerosshowpos, // explicit ’+’ for positive intsuppercase; // ’E’, ’X’ rather than ’e’, ’x’

Operator Overloading in C++ - 2004-10-21 – p. 26/31

Page 27: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Formating (cont)// read flagsfmtflags flags() const;// set flagsfmtflags flags(fmtflags f);// add flagsfmtflags setf(fmtflags f) {return flags(flags()|f);

}// clear flagsvoid unsetf(fmtflags mask) {flags(flags()&˜mask);

}

Operator Overloading in C++ - 2004-10-21 – p. 27/31

Page 28: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Manipulatorsdeclared in <iomanip>, namespase std

Manipulators:boolalpha, noboolalphashowbase, noshowbaseshowpoint, noshowpointshowpos, noshowposskipws, noskipws, wsuppercase, nouppercaseinternalleft, rightdec, hex, octfixed, scientific

Operator Overloading in C++ - 2004-10-21 – p. 28/31

Page 29: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Manipulators (cont)Manipulators (cont):

endl, endsflushresetioflags, setioflagssetbasesetfillsetprecisionsetw

Operator Overloading in C++ - 2004-10-21 – p. 29/31

Page 30: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

Other kinds of streamsFile Streams

<fstream>ifstream, ofstream, fstream

String Streams<sstream>istringstream, ostringstream, stringstreamwistringstream, wostringstream, wstringstream

Stream Buffers

Operator Overloading in C++ - 2004-10-21 – p. 30/31

Page 31: Java IO C++ Streamsrazvanm/600.120/Java-Io-C... · 22.10.2004  · streams of characters Reader abstract class is the superclass of all classes representing an input stream of characters

That’s all!

Have a nice weekend!

Operator Overloading in C++ - 2004-10-21 – p. 31/31