File IO Serialization
Total Page:16
File Type:pdf, Size:1020Kb
File IO serialization HOM HVD Streams and Java-I/O Streams in Java File IO serialization java.nio.file Object Streams and serialization Pieter van den Hombergh Serialisation formats Richard van den Ham WARNING Javascript Object Notation Fontys Hogeschool voor Techniek en Logistiek March 13, 2018 HOMHVD/FHTenL File IO serialization March 13, 2018 1/23 File IO Topics serialization HOM HVD Streams and Streams and Java-I/O Java-I/O Streams in Java Streams in Java java.nio.file java.nio.file Object Streams and serialization Serialisation formats WARNING Object Streams and serialization Javascript Object Notation Serialisation formats WARNING Javascript Object Notation HOMHVD/FHTenL File IO serialization March 13, 2018 2/23 File IO Streams in Java serialization HOM HVD Streams and Java-I/O Streams in Java java.nio.file Object Streams and serialization Serialisation formats WARNING Javascript Object Notation Figure: Taken from the Oracle/Sun Java tutorial Read or write information from different sources and types. sources: network, files, devices types: text, picture, sound Streams are FIFOs, uni-directional HOMHVD/FHTenL File IO serialization March 13, 2018 3/23 File IO Two basic stream types serialization HOM Byte Streams HVD java.io.InputStream, java.io.OutputStream Streams and Java-I/O read and write pdf, mp3, raw... Streams in Java java.nio.file Character Streams (16-bit Unicode) Object Streams java.io.Reader, java.io.Writer and serialization simplifies reading and writing characters, character-arrays or Strings Serialisation formats WARNING Javascript Object Notation HOMHVD/FHTenL File IO serialization March 13, 2018 4/23 File IO Input Stream Overview serialization HOM HVD ByteStreamClass for Input CharStreamClass for Input Streams and Java-I/O InputStream Reader Streams in Java BufferedInputStream BufferedReader java.nio.file Object Streams LineNumberInputStream LineNumberReader and serialization Serialisation ByteArrayInputStream CharArrayReader formats WARNING (none) InputStreamReader Javascript Object Notation DataInputStream (none) FilterInputStream FilterReader PushbackInputStream PushbackReader PipedInputStream PipedReader StringBufferInputStream (deprecated under 1.1) StringReader SequenceInputStream (none) HOMHVD/FHTenL File IO serialization March 13, 2018 5/23 File IO Output Stream Overview serialization HOM HVD ByteStreamClass for Output CharStreamClass for Output Streams and OutputStream Writer Java-I/O Streams in Java BufferedOutputStream BufferedWriter java.nio.file Object Streams ByteArrayOutputStream CharArrayWriter and serialization DataOutputStream (none) Serialisation formats (none) OutputStreamWriter WARNING Javascript Object Notation FileOutputStream FileWriter PrintStream PrintWriter PipedOutputStream PipedWriter (none) StringWriter HOMHVD/FHTenL File IO serialization March 13, 2018 6/23 File IO ByteStreams - File Input and Output serialization HOM «interface» java.lang.AutoClosable used to enable HVD java 7 "try with +close(): void resources" Streams and Java-I/O «interface» «interface» Streams in Java java.io.Closable java.io.Flushable java.nio.file +close(): void +flush(): void Object Streams and serialization java.io.InputStream java.io.OutputStream Serialisation formats + avialable(): int + write(b : int): void WARNING + mark(readlimit:int): void + write(b: byte[]): void + markSupported: boolean + write(b: byte[],off:int, len:int): void Javascript Object Notation + read(): int + reade(buffer: byte[]): int + read(b: byte[],off: int, len:int): int + reset(): void + skip(n: long): long java.io.FileInputStream java.io.FileOutputStream + FileInputStream(file: File) + FileOutputStream(file: File) + FileInputStream(name: String) + FileOutputStream(name: String) + FileInputStream(fdObj: FileDescription) + FileOutputStream(file: File, append:boolean) # finalize(): void + FileOutputStream(name: String, append:boolean) + getChannel(): FileChannel + FileOutputStream(fdObj: FileDescription) + getFD(): FileDescriptor # finalize(): void + getChannel(): FileChannel + getFD(): FileDescriptor Both Input and Output are streams of bytes. HOMHVD/FHTenL File IO serialization March 13, 2018 7/23 File IO CharacterStreams - Writer and Reader serialization HOM HVD «interface» used to enable java.lang.AutoClosable java 7 "try with Streams and + close(): void resources" Java-I/O «interface» Streams in Java java.lang.Appendable java.nio.file «interface» «interface» «interface» + append( char c): Appendable java.io.Flushable java.io.Closable java.lang.Readable + append(csq: CharSequence): Appendable Object Streams + append(csq: CharSequence csq, start:int,+ flush(): end: int):void Appendable+ close(): void + read(cb: CharBuffer): int and serialization Serialisation formats WARNING Javascript Object Notation java.io.Writer java.io.Reader +Writer() + Reader() +Writer(lock: Object) + Reader(lock:Object) + write(cbuf: char[]): Writer + mark(readAheadLimit:int): void + write(csq : CharSequence ): Writer + markSupported: boolean + write(csq : CharSequence, start: int, end: int ): Writer + read(): int + read(buf: char[]) : int + read(buf: char[], off: int , len: int): int + read(target: CharBuffer): int + ready(): boolean + reset(): void + skip(n: long): long Reader reads character data (text) Writer writes character data HOMHVD/FHTenL File IO serialization March 13, 2018 8/23 File IO Audio Stream in Servlets serialization HOM HVD Idea: Use Servlet Output Stream to stream MP3s via HTTP Streams and set ContentType to “audio/mpeg” Java-I/O Streams in Java read MP3 files via InputStream java.nio.file Object Streams write the bytes into the Servlet Output Stream and serialization Serialisation formats WARNING Javascript Object Notation HOMHVD/FHTenL File IO serialization March 13, 2018 9/23 File IO Wrapping Streams serialization HOM HVD Streams can be combined Streams and Depending on source and type Java-I/O Streams in Java useful additions java.nio.file buffering Object Streams and serialization encryption Serialisation filtering formats ... WARNING Javascript Object Notation HOMHVD/FHTenL File IO serialization March 13, 2018 10/23 File IO Wrapping Streams - Buffering serialization HOM HVD Buffered streams speed up input and output by reducing the number of Streams and reads and writes. Java-I/O Streams in Java They employ a buffered array of bytes or characters that act as a cache java.nio.file Object Streams If no buffersize is specified, the default size is 512 bytes or characters. and serialization A buffered output stream calls the write method only when its buffer fills up Serialisation formats or when the flush() method is called WARNING Javascript Object Notation FileReader reader = new FileReader ( "/home/hom/test.dat" ); BufferedReader bufferedIn = new BufferedReader ( reader ); Useful additional function: String line = bufferedIn . readLine (); HOMHVD/FHTenL File IO serialization March 13, 2018 11/23 File IO Wrapping Streams - On-the-fly encryption serialization HOM HVD Java IO streams implement the decorator design pattern Streams and new/additional behaviour can be added to an existing stream dynamically Java-I/O Streams in Java might remind you of the ISO/OSI reference model for network protocols java.nio.file Object Streams and serialization Serialisation formats WARNING Javascript Object Notation HOMHVD/FHTenL File IO serialization March 13, 2018 12/23 File IO TIP: The java.nio.file package serialization HOM HVD Since Java 1.4 there is a new java.nio package, which adds extra Streams and buffering capabilities. Java-I/O Streams in Java Since Java7 the package java.nio.file has been added. The you can java.nio.file find some very useful classes to help you with your daily file hassling code. Object Streams and serialization In particular java.nio.file.Files has many useful methods. Serialisation List<String>Files.readAllLines(Path path) formats WARNING (since Java8 ) does what its name says. Javascript Object Notation Stream<String>Files.lines(Path path) (since Java8) creates a Stream of String. Streams are a new Java 8 feature. You can do things like: public static void main(String[] args) throws IOException { Path path = Paths.get("/etc/passwd"); Files.lines(path).forEach(System.out::println); } HOMHVD/FHTenL File IO serialization March 13, 2018 13/23 File IO Serialization serialization HOM HVD Streams and Java-I/O Streams in Java java.nio.file Object Streams and serialization Serialisation formats Serialization is the process of converting an object into a sequence of bits. WARNING Javascript Object Notation This is necessary to transport objects via streams. Serialization respectively Deserialization is also known as Mashalling/Unmarshalling (broader scope). HOMHVD/FHTenL File IO serialization March 13, 2018 14/23 File IO What makes an Object, data wise serialization HOM Quiz: HVD Streams and Java-I/O Streams in Java java.nio.file Object Streams and serialization Serialisation If you imagine some objects you’ve encountered, what is the closest data formats WARNING structure that describes the structure of all objects? Javascript Object Notation HOMHVD/FHTenL File IO serialization March 13, 2018 15/23 File IO Serialization in Java serialization HOM HVD Not every object is serializable (e.g. Thread, or Socket). Quiz: Why not? (Think transparency, security). Streams and Java-I/O Streams in Java In Java, the Serializable-Interface “marks” serializable objects java.nio.file The stream implementations ObjectInputStream and Object Streams ObjectOutputStream can be used to serialize/deserialize serializable and serialization Serialisation objects