/* * [Test.java] * * Summary: Test file to ensure all code FileIO can generate will at least compile. * * Copyright: (c) 1997-2017 Roedy Green, Canadian Mind Products, http://mindprod.com * * Licence: This software may be copied and used freely for any purpose but military. * http://mindprod.com/contact/nonmil.html * * Requires: JDK 1.8+ * * Created with: JetBrains IntelliJ IDEA IDE http://www.jetbrains.com/idea/ * * Version History: * 5.7 2008-04-02 add build number to title */ package com.mindprod.fileio; import com.mindprod.ledatastream.LEDataInputStream; import com.mindprod.ledatastream.LEDataOutputStream; import com.mindprod.ledatastream.LERandomAccessFile; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.CharArrayReader; import java.io.CharArrayWriter; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.PipedInputStream; import java.io.PipedOutputStream; import java.io.PrintWriter; import java.io.RandomAccessFile; import java.io.StringReader; import java.io.StringWriter; import java.net.HttpURLConnection; import java.net.Socket; import java.net.URL; import java.net.URLConnection; import java.net.URLEncoder; import java.util.zip.GZIPInputStream; import java.util.zip.GZIPOutputStream; /** * Test file to ensure all code FileIO can generate will at least compile. * * @author Roedy Green, Canadian Mind Products * @version 5.7 2008-04-02 add build number to title * @since 1997 */ final class Test { // -------------------------- STATIC METHODS -------------------------- private static int readBytesBlocking( InputStream in, byte b[], int off, int len ) throws IOException { return 0; } // -------------------------- OTHER METHODS -------------------------- void dummy() { try { // console raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a console. // Console requires human-readable text. } // console raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed console. // Console requires human-readable text. } // console raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered console. // Console requires human-readable text. } // console raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed console. // Console requires human-readable text. } // console raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a console. // Console requires human-readable text. } // console raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed console. // Console requires human-readable text. } // console raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered console. // Console requires human-readable text. } // console raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed console. // Console requires human-readable text. } // console Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a console. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( System.out ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // console Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed console. // Console requires human-readable text. } // console Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered console. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( System.out ); // B U F F E R BufferedWriter bw = new BufferedWriter( osw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // console Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed console. // Console requires human-readable text. } // console Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a console. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( System.in ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // console Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed console. // Console requires human-readable text. } // console Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered console. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( System.in ); // B U F F E R BufferedReader br = new BufferedReader( isr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // console Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed console. // Console requires human-readable text. } // console encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a console. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( System.out, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // console encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed console. // Console requires human-readable text. } // console encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered console. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( System.out, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedWriter bw = new BufferedWriter( eosw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // console encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed console. // Console requires human-readable text. } // console encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a console. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( System.in, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // console encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed console. // Console requires human-readable text. } // console encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered console. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( System.in, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedReader br = new BufferedReader( eisr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // console encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed console. // Console requires human-readable text. } // console Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a console. // Console requires human-readable text. } // console Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed console. // Console requires human-readable text. } // console Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered console. // Console requires human-readable text. } // console Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed console. // Console requires human-readable text. } // console Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a console. // Console requires human-readable text. } // console Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed console. // Console requires human-readable text. } // console Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered console. // Console requires human-readable text. } // console Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed console. // Console requires human-readable text. } // console big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a console. // Console requires human-readable text. } // console big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed console. // Console requires human-readable text. } // console big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered console. // Console requires human-readable text. } // console big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed console. // Console requires human-readable text. } // console big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a console. // Console requires human-readable text. } // console big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed console. // Console requires human-readable text. } // console big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered console. // Console requires human-readable text. } // console big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed console. // Console requires human-readable text. } // console little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a console. // Console requires human-readable text. } // console little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed console. // Console requires human-readable text. } // console little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered console. // Console requires human-readable text. } // console little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed console. // Console requires human-readable text. } // console little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a console. // Console requires human-readable text. } // console little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed console. // Console requires human-readable text. } // console little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered console. // Console requires human-readable text. } // console little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed console. // Console requires human-readable text. } // console serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a console. // Console requires human-readable text. } // console serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed console. // Console requires human-readable text. } // console serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered console. // Console requires human-readable text. } // console serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed console. // Console requires human-readable text. } // console serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a console. // Console requires human-readable text. } // console serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed console. // Console requires human-readable text. } // console serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered console. // Console requires human-readable text. } // console serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed console. // Console requires human-readable text. } // sequential file raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; fos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H fos.flush(); // C L O S E fos.close(); } // sequential file raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( fos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; gzos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H gzos.flush(); // C L O S E gzos.close(); } // sequential file raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( fos, 4096/* buffsize in bytes */ ); // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; bos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H bos.flush(); // C L O S E bos.close(); } // sequential file raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = fis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E fis.close(); } // sequential file raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( fis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = gzis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E gzis.close(); } // sequential file raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( fis, 4096/* buffsize in bytes */ ); // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = bis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E bis.close(); } // sequential file raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( fos ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // sequential file Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( fos, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( gzos ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // sequential file Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( fos ); // B U F F E R BufferedWriter bw = new BufferedWriter( osw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // sequential file Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( fis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // sequential file Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( fis, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader isr = new InputStreamReader( gzis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // sequential file Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( fis ); // B U F F E R BufferedReader br = new BufferedReader( isr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // sequential file Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( fos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // sequential file encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( fos, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( gzos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // sequential file encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( fos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedWriter bw = new BufferedWriter( eosw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // sequential file encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( fis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // sequential file encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( fis, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader eisr = new InputStreamReader( gzis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // sequential file encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( fis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedReader br = new BufferedReader( eisr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // sequential file encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( fos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // sequential file Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( fos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // sequential file Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( fos, 4096/* buffsize in bytes */ ); // F O R M A T DataOutputStream dos = new DataOutputStream( bos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // sequential file Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( fis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // sequential file Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( fis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // sequential file Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( fis, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // sequential file Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( fos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // sequential file big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( fos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // sequential file big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( fos, 4096/* buffsize in bytes */ ); // F O R M A T DataOutputStream dos = new DataOutputStream( bos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // sequential file big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( fis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // sequential file big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( fis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // sequential file big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( fis, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // sequential file big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( fos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // sequential file little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( fos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( gzos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // sequential file little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( fos, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( bos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // sequential file little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( fis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // sequential file little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( fis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( gzis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // sequential file little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( fis, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( bis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // sequential file little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( fos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // sequential file serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( fos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( gzos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // sequential file serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N FileOutputStream fos = new FileOutputStream( "C:/temp/temp.out", false/* append */ ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( fos, 4096/* buffsize in bytes */ ); // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( bos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // sequential file serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed sequential file. // Compression has built-in buffering. } // sequential file serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( fis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // sequential file serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( fis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( gzis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // sequential file serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered sequential file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N FileInputStream fis = new FileInputStream( "C:/temp/temp.in" ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( fis, 4096/* buffsize in bytes */ ); // F O R M A T ObjectInputStream ois = new ObjectInputStream( bis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // sequential file serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed sequential file. // Compression has built-in buffering. } // random access file raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N RandomAccessFile raf = new RandomAccessFile( "C:/temp/rand.data", "rw"/* read/write */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E raf.seek( 0/* byte offset in file*/ ); byte[] ba = new byte[ 10000 ]; raf.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H // C L O S E raf.close(); } // random access file raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed random access file. // Compression requires a sequential stream. } // random access file raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered random access file. // Buffering is incompatible with random access. } // random access file raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N RandomAccessFile raf = new RandomAccessFile( "C:/temp/rand.data", "r"/* read only */ ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D raf.seek( 0/* byte offset in file */ ); byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = raf.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E raf.close(); } // random access file raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed random access file. // Compression requires a sequential stream. } // random access file raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered random access file. // Buffering is incompatible with random access. } // random access file raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N RandomAccessFile raf = new RandomAccessFile( "C:/temp/rand.data", "rw"/* read/write */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E raf.seek( 0/* byte offset in file*/ ); raf.writeByte( ( byte ) 44 ); raf.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); // F L U S H // C L O S E raf.close(); } // random access file Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed random access file. // Compression requires a sequential stream. } // random access file Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered random access file. // Buffering is incompatible with random access. } // random access file Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N RandomAccessFile raf = new RandomAccessFile( "C:/temp/rand.data", "r"/* read only */ ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D raf.seek( 0/* byte offset in file */ ); byte b = raf.readByte(); String line; // File being read had better have a terminal \n! line = raf.readLine(); // C L O S E raf.close(); } // random access file Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed random access file. // Compression requires a sequential stream. } // random access file Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered random access file. // Buffering is incompatible with random access. } // random access file Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a random access file. // locale-encoding requires a sequential stream. } // random access file encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed random access file. // Compression requires a sequential stream. } // random access file encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered random access file. // Buffering is incompatible with random access. } // random access file encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a random access file. // locale-encoding requires a sequential stream. } // random access file encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed random access file. // Compression requires a sequential stream. } // random access file encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered random access file. // Buffering is incompatible with random access. } // random access file encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N RandomAccessFile raf = new RandomAccessFile( "C:/temp/rand.data", "rw"/* read/write */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E raf.seek( 0/* byte offset in file*/ ); raf.writeChar( 'x' ); raf.writeChars( "dolphin" ); // F L U S H // C L O S E raf.close(); } // random access file Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed random access file. // Compression requires a sequential stream. } // random access file Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered random access file. // Buffering is incompatible with random access. } // random access file Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N RandomAccessFile raf = new RandomAccessFile( "C:/temp/rand.data", "r"/* read only */ ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D raf.seek( 0/* byte offset in file */ ); char c = raf.readChar(); /* there is no readChars method */ // C L O S E raf.close(); } // random access file Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed random access file. // Compression requires a sequential stream. } // random access file Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered random access file. // Buffering is incompatible with random access. } // random access file Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // I M P O R T // import java.io.*; // O P E N RandomAccessFile raf = new RandomAccessFile( "C:/temp/rand.data", "rw"/* read/write */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E raf.seek( 0/* byte offset in file*/ ); raf.writeBoolean( true ); raf.writeByte( ( byte ) 44 ); raf.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); raf.writeChar( 'a' ); raf.writeChars( "dingo"/* string 16-bit Unicode */ ); raf.writeDouble( 3.14D ); raf.writeFloat( 3.14F ); raf.writeInt( 149 ); raf.writeLong( 149L ); raf.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. raf.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H // C L O S E raf.close(); } // random access file big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed random access file. // Compression requires a sequential stream. } // random access file big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered random access file. // Buffering is incompatible with random access. } // random access file big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // I M P O R T // import java.io.*; // O P E N RandomAccessFile raf = new RandomAccessFile( "C:/temp/rand.data", "r"/* read only */ ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D raf.seek( 0/* byte offset in file */ ); boolean q = raf.readBoolean(); byte b = raf.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = raf.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = raf.readChar(); // There is no readChars method double d = raf.readDouble(); float f = raf.readFloat(); int j = raf.readInt(); long l = raf.readLong(); short s = raf.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = raf.readUTF(); byte ub = ( byte ) raf.readUnsignedByte(); short us = ( short ) raf.readUnsignedShort(); // File being read had better have a terminal \n! String line = raf.readLine(); // line == null means EOF // C L O S E raf.close(); } // random access file big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed random access file. // Compression requires a sequential stream. } // random access file big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered random access file. // Buffering is incompatible with random access. } // random access file big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not write to the local hard disk. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N // NOTE: LERandomAccessFile is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LERandomAccessFile leraf = new LERandomAccessFile( "C:/temp/rand.data", "rw"/* read/write */ ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E leraf.seek( 0/* byte offset in file*/ ); leraf.writeBoolean( true ); leraf.writeByte( ( byte ) 44 ); leraf.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); leraf.writeChar( 'a' ); leraf.writeChars( "dingo"/* string 16-bit Unicode */ ); leraf.writeDouble( 3.14D ); leraf.writeFloat( 3.14F ); leraf.writeInt( 149 ); leraf.writeLong( 149L ); leraf.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. leraf.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H // C L O S E leraf.close(); } // random access file little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed random access file. // Compression requires a sequential stream. } // random access file little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered random access file. // Buffering is incompatible with random access. } // random access file little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a random access file. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may not read the local hard disk. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N // NOTE: LERandomAccessFile is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDASTREAM. LERandomAccessFile leraf = new LERandomAccessFile( "C:/temp/rand.data", "r"/* read only */ ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D leraf.seek( 0/* byte offset in file */ ); boolean q = leraf.readBoolean(); byte b = leraf.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = leraf.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = leraf.readChar(); // There is no readChars method double d = leraf.readDouble(); float f = leraf.readFloat(); int j = leraf.readInt(); long l = leraf.readLong(); short s = leraf.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = leraf.readUTF(); byte ub = ( byte ) leraf.readUnsignedByte(); short us = ( short ) leraf.readUnsignedShort(); // File being read had better have a terminal \n! String line = leraf.readLine(); // line == null means EOF // C L O S E leraf.close(); } // random access file little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed random access file. // Compression requires a sequential stream. } // random access file little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered random access file. // Buffering is incompatible with random access. } // random access file little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a random access file. // Serialized objects require a sequential stream. } // random access file serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed random access file. // Compression requires a sequential stream. } // random access file serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered random access file. // Buffering is incompatible with random access. } // random access file serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed random access file. // Buffering is incompatible with random access. } // random access file serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a random access file. // Serialized objects require a sequential stream. } // random access file serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed random access file. // Compression requires a sequential stream. } // random access file serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered random access file. // Buffering is incompatible with random access. } // random access file serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed random access file. // Buffering is incompatible with random access. } // String raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a String. // Strings can only contain Unicode data. } // String raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed String. // Compressed data cannot be stored in a String. } // String raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered String. // Strings can only contain Unicode data. } // String raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed String. // Compressed data cannot be stored in a String. } // String raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a String. // Strings can only contain Unicode data. } // String raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed String. // Compressed data cannot be stored in a String. } // String raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered String. // Strings can only contain Unicode data. } // String raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed String. // Compressed data cannot be stored in a String. } // String Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a String. // Strings can only contain Unicode data. } // String Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed String. // Compressed data cannot be stored in a String. } // String Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered String. // Strings can only contain Unicode data. } // String Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed String. // Compressed data cannot be stored in a String. } // String Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a String. // Strings can only contain Unicode data. } // String Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed String. // Compressed data cannot be stored in a String. } // String Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered String. // Strings can only contain Unicode data. } // String Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed String. // Compressed data cannot be stored in a String. } // String encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a String. // Strings can only contain Unicode data. } // String encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed String. // Compressed data cannot be stored in a String. } // String encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered String. // Strings can only contain Unicode data. } // String encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed String. // Compressed data cannot be stored in a String. } // String encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a String. // Strings can only contain Unicode data. } // String encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed String. // Compressed data cannot be stored in a String. } // String encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered String. // Strings can only contain Unicode data. } // String encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed String. // Compressed data cannot be stored in a String. } // String Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a String. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // E N C O D E StringWriter sw = new StringWriter(); // B U F F E R // F O R M A T // W R I T E sw.write( 'x' ); sw.write( "Tasmanian devil" ); // F L U S H sw.flush(); String result = sw.toString(); // C L O S E sw.close(); } // String Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed String. // Compressed data cannot be stored in a String. } // String Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered String. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // E N C O D E StringWriter sw = new StringWriter(); // B U F F E R BufferedWriter bw = new BufferedWriter( sw, 4096/* buffsize in chars */ ); // F O R M A T // W R I T E bw.write( 'x' ); bw.write( "Tasmanian devil" ); // F L U S H bw.flush(); String result = bw.toString(); // C L O S E bw.close(); } // String Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed String. // Compressed data cannot be stored in a String. } // String Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a String. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // D E C O D E String ids = "data to be read as if from a file"; StringReader sr = new StringReader( ids ); // B U F F E R // F O R M A T // R E A D char c = ( char ) sr.read(); /* there is no readChars method!*/ // C L O S E sr.close(); } // String Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed String. // Compressed data cannot be stored in a String. } // String Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered String. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // D E C O D E String ids = "data to be read as if from a file"; StringReader sr = new StringReader( ids ); // B U F F E R BufferedReader br = new BufferedReader( sr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char c = ( char ) br.read(); /* there is no readChars method!*/ // C L O S E br.close(); } // String Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed String. // Compressed data cannot be stored in a String. } // String big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a String. // Strings can only contain Unicode data. } // String big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed String. // Compressed data cannot be stored in a String. } // String big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered String. // Strings can only contain Unicode data. } // String big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed String. // Compressed data cannot be stored in a String. } // String big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a String. // Strings can only contain Unicode data. } // String big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed String. // Compressed data cannot be stored in a String. } // String big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered String. // Strings can only contain Unicode data. } // String big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed String. // Compressed data cannot be stored in a String. } // String little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a String. // Strings can only contain Unicode data. } // String little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed String. // Compressed data cannot be stored in a String. } // String little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered String. // Strings can only contain Unicode data. } // String little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed String. // Compressed data cannot be stored in a String. } // String little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a String. // Strings can only contain Unicode data. } // String little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed String. // Compressed data cannot be stored in a String. } // String little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered String. // Strings can only contain Unicode data. } // String little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed String. // Compressed data cannot be stored in a String. } // String serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a String. // Strings can only contain Unicode data. } // String serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed String. // Compressed data cannot be stored in a String. } // String serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered String. // Strings can only contain Unicode data. } // String serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed String. // Compressed data cannot be stored in a String. } // String serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a String. // Strings can only contain Unicode data. } // String serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed String. // Compressed data cannot be stored in a String. } // String serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered String. // Strings can only contain Unicode data. } // String serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed String. // Compressed data cannot be stored in a String. } // char[] raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a char[]. // Char arrays can only contain Unicode data. } // char[] raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered char[]. // Buffering would lose the ability to do a to toCharArray to get the results. } // char[] raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a char[]. // Char arrays can only contain Unicode data. } // char[] raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered char[]. // Char arrays can only contain Unicode data. } // char[] raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a char[]. // Char arrays can only contain Unicode data. } // char[] Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered char[]. // Buffering would lose the ability to do a to toCharArray to get the results. } // char[] Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a char[]. // Char arrays can only contain Unicode data. } // char[] Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered char[]. // Char arrays can only contain Unicode data. } // char[] Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a char[]. // Char arrays can only contain Unicode data. } // char[] encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered char[]. // Buffering would lose the ability to do a to toCharArray to get the results. } // char[] encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a char[]. // Char arrays can only contain Unicode data. } // char[] encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered char[]. // Char arrays can only contain Unicode data. } // char[] encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a char[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // E N C O D E CharArrayWriter caw = new CharArrayWriter( 1000 ); // B U F F E R // F O R M A T // W R I T E caw.write( 'x' ); caw.write( "wombat" ); // F L U S H caw.flush(); char[] result = caw.toCharArray(); // C L O S E caw.close(); } // char[] Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered char[]. // Buffering would lose the ability to do a to toCharArray to get the results. } // char[] Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a char[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // D E C O D E char[] ica = new char[ 1000 ]; // ... code to fill ica with data to be read ... CharArrayReader car = new CharArrayReader( ica ); // B U F F E R // F O R M A T // R E A D char c = ( char ) car.read(); /* there is no readChars method!*/ // C L O S E car.close(); } // char[] Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered char[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N // C O M P R E S S // D E C O D E char[] ica = new char[ 1000 ]; // ... code to fill ica with data to be read ... CharArrayReader car = new CharArrayReader( ica ); // B U F F E R BufferedReader br = new BufferedReader( car, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char c = ( char ) br.read(); /* there is no readChars method!*/ // C L O S E br.close(); } // char[] Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a char[]. // Char arrays can only contain Unicode data. } // char[] big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered char[]. // Buffering would lose the ability to do a to toCharArray to get the results. } // char[] big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a char[]. // Char arrays can only contain Unicode data. } // char[] big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered char[]. // Char arrays can only contain Unicode data. } // char[] big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a char[]. // Char arrays can only contain Unicode data. } // char[] little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered char[]. // Buffering would lose the ability to do a to toCharArray to get the results. } // char[] little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a char[]. // Char arrays can only contain Unicode data. } // char[] little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered char[]. // Char arrays can only contain Unicode data. } // char[] little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a char[]. // Char arrays can only contain Unicode data. } // char[] serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered char[]. // Buffering would lose the ability to do a to toCharArray to get the results. } // char[] serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // char[] serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a char[]. // Char arrays can only contain Unicode data. } // char[] serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed char[]. // Compressed data cannot be stored in char[]. } // char[] serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered char[]. // Char arrays can only contain Unicode data. } // char[] serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed char[]. // Compressed data cannot be stored in char[]. } // byte[] raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; baos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H baos.flush(); byte[] result = baos.toByteArray(); // C L O S E baos.close(); } // byte[] raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( baos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; gzos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H gzos.flush(); gzos.finish(); byte[] result = baos.toByteArray(); // C L O S E gzos.close(); } // byte[] raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( baos, 4096/* buffsize in bytes */ ); // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; bos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H bos.flush(); byte[] result = baos.toByteArray(); // C L O S E bos.close(); } // byte[] raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = bais.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E bais.close(); } // byte[] raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( bais, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = gzis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E gzis.close(); } // byte[] raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( bais, 4096/* buffsize in bytes */ ); // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = bis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E bis.close(); } // byte[] raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( baos ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); byte[] result = baos.toByteArray(); // C L O S E prw.close(); } // byte[] Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( baos, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( gzos ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); gzos.finish(); byte[] result = baos.toByteArray(); // C L O S E prw.close(); } // byte[] Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( baos ); // B U F F E R BufferedWriter bw = new BufferedWriter( osw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); byte[] result = baos.toByteArray(); // C L O S E prw.close(); } // byte[] Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( bais ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // byte[] Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( bais, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader isr = new InputStreamReader( gzis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // byte[] Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( bais ); // B U F F E R BufferedReader br = new BufferedReader( isr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // byte[] Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( baos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); byte[] result = baos.toByteArray(); // C L O S E prw.close(); } // byte[] encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( baos, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( gzos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); gzos.finish(); byte[] result = baos.toByteArray(); // C L O S E prw.close(); } // byte[] encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( baos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedWriter bw = new BufferedWriter( eosw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); byte[] result = baos.toByteArray(); // C L O S E prw.close(); } // byte[] encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( bais, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // byte[] encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( bais, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader eisr = new InputStreamReader( gzis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // byte[] encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( bais, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedReader br = new BufferedReader( eisr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // byte[] encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( baos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "cassowary" ); // F L U S H dos.flush(); byte[] result = baos.toByteArray(); // C L O S E dos.close(); } // byte[] Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( baos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "cassowary" ); // F L U S H dos.flush(); gzos.finish(); byte[] result = baos.toByteArray(); // C L O S E dos.close(); } // byte[] Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( baos, 4096/* buffsize in bytes */ ); // F O R M A T DataOutputStream dos = new DataOutputStream( bos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "cassowary" ); // F L U S H dos.flush(); byte[] result = baos.toByteArray(); // C L O S E dos.close(); } // byte[] Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( bais ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // byte[] Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( bais, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // byte[] Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( bais, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // byte[] Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( baos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); byte[] result = baos.toByteArray(); // C L O S E dos.close(); } // byte[] big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( baos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); gzos.finish(); byte[] result = baos.toByteArray(); // C L O S E dos.close(); } // byte[] big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( baos, 4096/* buffsize in bytes */ ); // F O R M A T DataOutputStream dos = new DataOutputStream( bos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); byte[] result = baos.toByteArray(); // C L O S E dos.close(); } // byte[] big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( bais ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // byte[] big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( bais, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // byte[] big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( bais, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // byte[] big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( baos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); byte[] result = baos.toByteArray(); // C L O S E ledos.close(); } // byte[] little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( baos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( gzos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); gzos.finish(); byte[] result = baos.toByteArray(); // C L O S E ledos.close(); } // byte[] little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( baos, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( bos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); byte[] result = baos.toByteArray(); // C L O S E ledos.close(); } // byte[] little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( bais ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // byte[] little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( bais, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( gzis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // byte[] little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( bais, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( bis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // byte[] little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( baos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); byte[] result = baos.toByteArray(); // C L O S E oos.close(); } // byte[] serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( baos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( gzos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); gzos.finish(); byte[] result = baos.toByteArray(); // C L O S E oos.close(); } // byte[] serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N ByteArrayOutputStream baos = new ByteArrayOutputStream( 1000 ); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( baos, 4096/* buffsize in bytes */ ); // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( bos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); byte[] result = baos.toByteArray(); // C L O S E oos.close(); } // byte[] serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed byte[]. // Compression has built-in buffering. } // byte[] serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( bais ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // byte[] serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( bais, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( gzis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // byte[] serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered byte[]. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // O P E N byte[] bai = new byte[ 1000 ]; // ... code to fill bai with data to be read ... ByteArrayInputStream bais = new ByteArrayInputStream( bai ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( bais, 4096/* buffsize in bytes */ ); // F O R M A T ObjectInputStream ois = new ObjectInputStream( bis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // byte[] serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed byte[]. // Compression has built-in buffering. } // URL raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; os.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H os.flush(); // C L O S E os.close(); } // URL raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; gzos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H gzos.flush(); // C L O S E gzos.close(); } // URL raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; bos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H bos.flush(); // C L O S E bos.close(); } // URL raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed URL. // Compression has built-in buffering. } // URL raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = is.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E is.close(); } // URL raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = gzis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E gzis.close(); } // URL raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = bis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E bis.close(); } // URL raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed URL. // Compression has built-in buffering. } // URL Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( os ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // URL Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( gzos ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // URL Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( os ); // B U F F E R BufferedWriter bw = new BufferedWriter( osw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // URL Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed URL. // Compression has built-in buffering. } // URL Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( is ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // URL Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader isr = new InputStreamReader( gzis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // URL Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( is ); // B U F F E R BufferedReader br = new BufferedReader( isr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // URL Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed URL. // Compression has built-in buffering. } // URL encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( os, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // URL encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( gzos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // URL encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( os, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedWriter bw = new BufferedWriter( eosw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, false/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // URL encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed URL. // Compression has built-in buffering. } // URL encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( is, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // URL encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader eisr = new InputStreamReader( gzis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // URL encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( is, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedReader br = new BufferedReader( eisr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // URL encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed URL. // Compression has built-in buffering. } // URL Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( os ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // URL Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // URL Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T DataOutputStream dos = new DataOutputStream( bos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // URL Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed URL. // Compression has built-in buffering. } // URL Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( is ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // URL Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // URL Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // URL Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed URL. // Compression has built-in buffering. } // URL big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( os ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // URL big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // URL big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T DataOutputStream dos = new DataOutputStream( bos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // URL big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed URL. // Compression has built-in buffering. } // URL big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( is ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // URL big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // URL big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // URL big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed URL. // Compression has built-in buffering. } // URL little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( os ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // URL little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( gzos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // URL little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( bos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // URL little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed URL. // Compression has built-in buffering. } // URL little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( is ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // URL little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( gzis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // URL little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( bis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // URL little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed URL. // Compression has built-in buffering. } // URL serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( os ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // URL serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( gzos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // URL serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! This code writes data to a URL, which may not be a successful way // to write or upload a file. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); URLConnection urlc = url.openConnection(); urlc.setAllowUserInteraction( false ); urlc.setDoInput( false );// if true, could read back results urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only // Make the physical connection. urlc.connect(); OutputStream os = urlc.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( bos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // URL serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed URL. // Compression has built-in buffering. } // URL serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( is ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // URL serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( gzis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // URL serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered URL. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate an URL read Command URL url = new URL( "http://www.billabong.com:80/songs/lyrics.txt" ); // if you want to analyse the URL String protocol = url.getProtocol();// http String host = url.getHost();// www.billabong.com String file = url.getFile();// lyrics.txt int port = url.getPort();// 80 URLConnection urlc = url.openConnection(); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif, application/x-java-serialized-object, " + "text/x-java-source, text/xml, application/xml, " + "text/css, application/x-java-jnlp-file," + " text/plain, application/zip," + " application/octet-stream, " + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); int length = urlc.getContentLength();// -1 if not available InputStream is = urlc.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T ObjectInputStream ois = new ObjectInputStream( bis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // URL serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed URL. // Compression has built-in buffering. } // HTTP GET/POST raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E // getResponseCode will block until the server responds. // save responseCode for later retrieval urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = is.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E is.close(); urlc.disconnect(); } // HTTP GET/POST raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = gzis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E gzis.close(); urlc.disconnect(); } // HTTP GET/POST raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = bis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E bis.close(); urlc.disconnect(); } // HTTP GET/POST raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = is.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E is.close(); urlc.disconnect(); } // HTTP GET/POST raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = gzis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E gzis.close(); urlc.disconnect(); } // HTTP GET/POST raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = bis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E bis.close(); urlc.disconnect(); } // HTTP GET/POST raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( is ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); urlc.disconnect(); } // HTTP GET/POST Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader isr = new InputStreamReader( gzis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); urlc.disconnect(); } // HTTP GET/POST Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( is ); // B U F F E R BufferedReader br = new BufferedReader( isr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); urlc.disconnect(); } // HTTP GET/POST Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( is ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); urlc.disconnect(); } // HTTP GET/POST Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader isr = new InputStreamReader( gzis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); urlc.disconnect(); } // HTTP GET/POST Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( is ); // B U F F E R BufferedReader br = new BufferedReader( isr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); urlc.disconnect(); } // HTTP GET/POST Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( is, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); urlc.disconnect(); } // HTTP GET/POST encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader eisr = new InputStreamReader( gzis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); urlc.disconnect(); } // HTTP GET/POST encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( is, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedReader br = new BufferedReader( eisr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); urlc.disconnect(); } // HTTP GET/POST encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( is, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); urlc.disconnect(); } // HTTP GET/POST encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader eisr = new InputStreamReader( gzis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); urlc.disconnect(); } // HTTP GET/POST encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( is, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedReader br = new BufferedReader( eisr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); urlc.disconnect(); } // HTTP GET/POST encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T /* Oops 3 */ // R E A D /* Oops 7 */ // C L O S E is.close(); urlc.disconnect(); } // HTTP GET/POST Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T /* Oops 3 */ // R E A D /* Oops 7 */ // C L O S E gzis.close(); urlc.disconnect(); } // HTTP GET/POST Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T /* Oops 3 */ // R E A D /* Oops 7 */ // C L O S E bis.close(); urlc.disconnect(); } // HTTP GET/POST Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T /* Oops 3 */ // R E A D /* Oops 7 */ // C L O S E is.close(); urlc.disconnect(); } // HTTP GET/POST Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T /* Oops 3 */ // R E A D /* Oops 7 */ // C L O S E gzis.close(); urlc.disconnect(); } // HTTP GET/POST Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T /* Oops 3 */ // R E A D /* Oops 7 */ // C L O S E bis.close(); urlc.disconnect(); } // HTTP GET/POST Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( is ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); urlc.disconnect(); } // HTTP GET/POST big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); urlc.disconnect(); } // HTTP GET/POST big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); urlc.disconnect(); } // HTTP GET/POST big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( is ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); urlc.disconnect(); } // HTTP GET/POST big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); urlc.disconnect(); } // HTTP GET/POST big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); urlc.disconnect(); } // HTTP GET/POST big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( is ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); urlc.disconnect(); } // HTTP GET/POST little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( gzis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); urlc.disconnect(); } // HTTP GET/POST little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( bis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); urlc.disconnect(); } // HTTP GET/POST little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( is ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); urlc.disconnect(); } // HTTP GET/POST little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( gzis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); urlc.disconnect(); } // HTTP GET/POST little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( bis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); urlc.disconnect(); } // HTTP GET/POST little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( is ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); urlc.disconnect(); } // HTTP GET/POST serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( gzis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); urlc.disconnect(); } // HTTP GET/POST serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // Writing to a server over the Internet is usually done indirectly with sockets, // RMI, servlets, CGI, HTTP PUT or JDBC. // Further, unsigned Applets may only communicate with the server they were loaded from. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI POST command URL url = new URL( "http://mindprod.com/cgi-bin/post-query" ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.urlc.setRequestMethod( "POST" ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( true ); urlc.setUseCaches( false ); urlc.setRequestProperty( "Content-type", "application/x-www-form-urlencoded" ); // prepare parm=value pairs data for POST. String postString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); urlc.setRequestProperty( "Content-length", ( Integer.toString( postString.length() ) ) ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for write OutputStream os = urlc.getOutputStream(); // W R I T E urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } // O P E N for read InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T ObjectInputStream ois = new ObjectInputStream( bis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); urlc.disconnect(); } // HTTP GET/POST serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // HTTP GET/POST serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( is ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); urlc.disconnect(); } // HTTP GET/POST serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( gzis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); urlc.disconnect(); } // HTTP GET/POST serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered HTTP GET/POST. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // I M P O R T // import java.io.*; // import java.net.*; // O P E N // Generate a HTTP CGI GET command // prepare parm=value pairs data for GET. String parmString = "flavour=" + URLEncoder.encode( "peaches & cream", "UTF-8" ) + "&" + "scoops=" + URLEncoder.encode( "2", "UTF-8" ); // For GET, glue parms on tail end of URL URL url = new URL( "http://mindprod.com/cgi-bin/query" + "?" + parmString ); // Beware! Netscape 4 creates a netscape.net.URLConnection instead of a HttpURLConnection. // This code will not work under Netscape 4 without the Java plug-in. HttpURLConnection urlc = ( HttpURLConnection ) url.openConnection(); // Beware! HttpURLConnection is implemented with browser-specific code, often buggy. // Consider using Apache HTTPClient instead.// GET or HEAD (like GET but just headers). urlc.setRequestMethod( "GET" ); // Identify as Firefox 1.5 String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.7) Gecko/20070914 Firefox/2.0.0.7"; urlc.setRequestProperty( "User-Agent", userAgent ); // optionally turn off keep-alive urlc.setRequestProperty( "Connection", "close" ); urlc.setAllowUserInteraction( false ); urlc.setDoInput( true ); urlc.setDoOutput( false ); urlc.setUseCaches( false ); urlc.setConnectTimeout( 50 * 1000/* ms */ );// JDK 1.5+ only urlc.setReadTimeout( 40 * 1000/* ms */ );// JDK 1.5+ only // MIME types desired in response. urlc.setRequestProperty( "Accept", "text/html, image/png, image/jpeg, " + "image/gif," + " text/plain," + "*; q=.2, */*; q=.2" ); // Make the physical connection. urlc.connect(); // O P E N for read int statusCode = urlc.getResponseCode(); int length = urlc.getContentLength();// -1 if not available if ( length < 0 ) { length = 32 * 1024; } InputStream is = urlc.getInputStream(); // Reads from a communications channel may fail to give you as much data as you asked for. // You get what is available. readBytesBlocking waits until all the data you requested (or eof). // See readBytesBlocking in the Java glossary http;//mindprod.com/jgloss/http.html for the source byte all[] = new byte[ 1000 ]; int byteCount = readBytesBlocking( is, all, 0/* offset */, all.length ); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T ObjectInputStream ois = new ObjectInputStream( bis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); urlc.disconnect(); } // HTTP GET/POST serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed HTTP GET/POST. // Compression has built-in buffering. } // socket raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; os.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H os.flush(); // C L O S E os.close(); } // socket raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; gzos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H gzos.flush(); // C L O S E gzos.close(); } // socket raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; bos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H bos.flush(); // C L O S E bos.close(); } // socket raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed socket. // Compression has built-in buffering. } // socket raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = is.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E is.close(); } // socket raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = gzis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E gzis.close(); } // socket raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = bis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E bis.close(); } // socket raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed socket. // Compression has built-in buffering. } // socket Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( os ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // socket Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( gzos ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // socket Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( os ); // B U F F E R BufferedWriter bw = new BufferedWriter( osw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // socket Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed socket. // Compression has built-in buffering. } // socket Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( is ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // socket Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader isr = new InputStreamReader( gzis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // socket Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( is ); // B U F F E R BufferedReader br = new BufferedReader( isr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // socket Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed socket. // Compression has built-in buffering. } // socket encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( os, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // socket encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( gzos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // socket encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( os, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedWriter bw = new BufferedWriter( eosw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // socket encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed socket. // Compression has built-in buffering. } // socket encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( is, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // socket encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader eisr = new InputStreamReader( gzis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // socket encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( is, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedReader br = new BufferedReader( eisr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // socket encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed socket. // Compression has built-in buffering. } // socket Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( os ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // socket Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // socket Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T DataOutputStream dos = new DataOutputStream( bos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // socket Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed socket. // Compression has built-in buffering. } // socket Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( is ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // socket Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // socket Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // socket Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed socket. // Compression has built-in buffering. } // socket big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( os ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // socket big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // socket big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T DataOutputStream dos = new DataOutputStream( bos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // socket big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed socket. // Compression has built-in buffering. } // socket big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( is ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // socket big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // socket big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // socket big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed socket. // Compression has built-in buffering. } // socket little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( os ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // socket little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( gzos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // socket little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( bos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't write raw objects, they need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // socket little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed socket. // Compression has built-in buffering. } // socket little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( is ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // socket little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( gzis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // socket little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.net.*; // import com.mindprod.ledatastream.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( bis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // socket little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed socket. // Compression has built-in buffering. } // socket serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( os ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // socket serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( os, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( gzos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // socket serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only write to the server they were loaded from. // Sockets are a low level tool. If you implement HTTP via low level sockets, you will have to create all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); OutputStream os = socket.getOutputStream(); // C O M P R E S S // E N C O D E // B U F F E R BufferedOutputStream bos = new BufferedOutputStream( os, 4096/* buffsize in bytes */ ); // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( bos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // socket serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed socket. // Compression has built-in buffering. } // socket serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // Input data had better be available at open time to check the header ObjectInputStream ois = new ObjectInputStream( is ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // socket serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // import java.util.zip.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( is, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // Input data had better be available at open time to check the header ObjectInputStream ois = new ObjectInputStream( gzis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // socket serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered socket. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! unsigned Applets may only read from the server they were loaded from. // Sockets are a low level tool. // If you implement HTTP via low level sockets, you will see all the headers and length bytes. // For HTTP normally you want HttpURLConnection rather than Socket. // To copy/download files see http://mindprod.com/products.html#FILETRANSFER. // To GET/PUT to a server see http://mindprod.com/products.html#HTTP for a library of useful methods. // I M P O R T // import java.io.*; // import java.net.*; // O P E N Socket socket = new Socket( "mail.abc.com"/* server */, 110/* socket # */ ); InputStream is = socket.getInputStream(); // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( is, 4096/* buffsize in bytes */ ); // F O R M A T // Input data had better be available at open time to check the header ObjectInputStream ois = new ObjectInputStream( bis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // socket serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed socket. // Compression has built-in buffering. } // pipe raw untranslated bytes read unbuffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; pos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H pos.flush(); // C L O S E pos.close(); } // pipe raw untranslated bytes read unbuffered compressed { // C O M M E N T // Write raw untranslated bytes into a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( pos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // W R I T E byte[] ba = new byte[ 10000 ]; gzos.write( ba, 0/* offset in ba */, ba.length/* bytes to write */ ); // F L U S H gzos.flush(); // C L O S E gzos.close(); } // pipe raw untranslated bytes read buffered uncompressed { // C O M M E N T // Write raw untranslated bytes into a buffered pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe raw untranslated bytes read buffered compressed { // C O M M E N T // Write raw untranslated bytes into a buffered compressed pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe raw untranslated bytes write unbuffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = pis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E pis.close(); } // pipe raw untranslated bytes write unbuffered compressed { // C O M M E N T // Read raw untranslated bytes from a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( pis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = gzis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E gzis.close(); } // pipe raw untranslated bytes write buffered uncompressed { // C O M M E N T // Read raw untranslated bytes from a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( pis, 4096/* buffsize in bytes */ ); // F O R M A T // R E A D byte[] ba = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = bis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); // C L O S E bis.close(); } // pipe raw untranslated bytes write buffered compressed { // C O M M E N T // Read raw untranslated bytes from a buffered compressed pipe. // Compression has built-in buffering. } // pipe Locale-encoded chars ( usually 8 bit ) read unbuffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( pos ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // pipe Locale-encoded chars ( usually 8 bit ) read unbuffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( pos, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( gzos ); // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( osw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // pipe Locale-encoded chars ( usually 8 bit ) read buffered uncompressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E OutputStreamWriter osw = new OutputStreamWriter( pos ); // B U F F E R BufferedWriter bw = new BufferedWriter( osw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // pipe Locale-encoded chars ( usually 8 bit ) read buffered compressed { // C O M M E N T // Write Locale-encoded chars ( usually 8 bit ) into a buffered compressed pipe. // Compression has built-in buffering. } // pipe Locale-encoded chars ( usually 8 bit ) write unbuffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( pis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // pipe Locale-encoded chars ( usually 8 bit ) write unbuffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( pis, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader isr = new InputStreamReader( gzis ); // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = isr.read( ca ); // isr.readLine() not available without BufferedReader. // C L O S E isr.close(); } // pipe Locale-encoded chars ( usually 8 bit ) write buffered uncompressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E InputStreamReader isr = new InputStreamReader( pis ); // B U F F E R BufferedReader br = new BufferedReader( isr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // pipe Locale-encoded chars ( usually 8 bit ) write buffered compressed { // C O M M E N T // Read Locale-encoded chars ( usually 8 bit ) from a buffered compressed pipe. // Compression has built-in buffering. } // pipe encoded chars read unbuffered uncompressed { // C O M M E N T // Write encoded chars into a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( pos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // pipe encoded chars read unbuffered compressed { // C O M M E N T // Write encoded chars into a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( pos, 4096/* buffsize in bytes */ ); // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( gzos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T PrintWriter prw = new PrintWriter( eosw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // pipe encoded chars read buffered uncompressed { // C O M M E N T // Write encoded chars into a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E OutputStreamWriter eosw = new OutputStreamWriter( pos, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedWriter bw = new BufferedWriter( eosw, 4096/* buffsize in chars */ ); // F O R M A T PrintWriter prw = new PrintWriter( bw, true/* auto flush on println */ ); // W R I T E prw.write( "platypus" ); prw.println( 149 ); // F L U S H prw.flush(); // C L O S E prw.close(); } // pipe encoded chars read buffered compressed { // C O M M E N T // Write encoded chars into a buffered compressed pipe. // Compression has built-in buffering. } // pipe encoded chars write unbuffered uncompressed { // C O M M E N T // Read encoded chars from a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( pis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // pipe encoded chars write unbuffered compressed { // C O M M E N T // Read encoded chars from a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( pis, 4096/* buffsize in bytes */ ); // D E C O D E InputStreamReader eisr = new InputStreamReader( gzis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = eisr.read( ca ); // eisr.readLine() not available without BufferedReader. // C L O S E eisr.close(); } // pipe encoded chars write buffered uncompressed { // C O M M E N T // Read encoded chars from a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E InputStreamReader eisr = new InputStreamReader( pis, "UTF-8" ); // usually UTF-8 for Unicode 8-bit giving the full Unicode set, no BOMs. // Cp437 is IBM OEM. // See "encoding" in the Java glossary for alternatives // such as UTF-16BE for 16-bit, big-endian Unicode characters. // B U F F E R BufferedReader br = new BufferedReader( eisr, 4096/* buffsize in chars */ ); // F O R M A T // R E A D char[] ca = new char[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int charsRead = br.read( ca ); String line; // File being read need not have have a terminal \n. // File being read may safely use any mixture of \r\n, \r or \n line terminators. line = br.readLine(); // line == null means EOF int aChar; aChar = br.read(); // aChar == -1 means EOF // C L O S E br.close(); } // pipe encoded chars write buffered compressed { // C O M M E N T // Read encoded chars from a buffered compressed pipe. // Compression has built-in buffering. } // pipe Unicode 16-bit chars read unbuffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( pos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // pipe Unicode 16-bit chars read unbuffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( pos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeChar( 'x' ); dos.writeChars( "dolphin" ); // F L U S H dos.flush(); // C L O S E dos.close(); } // pipe Unicode 16-bit chars read buffered uncompressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe Unicode 16-bit chars read buffered compressed { // C O M M E N T // Write Unicode 16-bit chars into a buffered compressed pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe Unicode 16-bit chars write unbuffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( pis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // pipe Unicode 16-bit chars write unbuffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( pis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // pipe Unicode 16-bit chars write buffered uncompressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( pis, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D char c = dis.readChar(); /* there is no readChars method */ // C L O S E dis.close(); } // pipe Unicode 16-bit chars write buffered compressed { // C O M M E N T // Read Unicode 16-bit chars from a buffered compressed pipe. // Compression has built-in buffering. } // pipe big-endian ( Java ) binary read unbuffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( pos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't send raw objects down a pipe. They need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // pipe big-endian ( Java ) binary read unbuffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( pos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T DataOutputStream dos = new DataOutputStream( gzos ); // W R I T E dos.writeBoolean( true ); dos.writeByte( ( byte ) 44 ); dos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); dos.writeChar( 'a' ); dos.writeChars( "dingo"/* string 16-bit Unicode */ ); dos.writeDouble( 3.14D ); dos.writeFloat( 3.14F ); dos.writeInt( 149 ); dos.writeLong( 149L ); dos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. dos.writeUTF( "echidna" ); // Note: You can't send raw objects down a pipe. They need to be serialised. // F L U S H dos.flush(); // C L O S E dos.close(); } // pipe big-endian ( Java ) binary read buffered uncompressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe big-endian ( Java ) binary read buffered compressed { // C O M M E N T // Write big-endian ( Java ) binary into a buffered compressed pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe big-endian ( Java ) binary write unbuffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( pis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // pipe big-endian ( Java ) binary write unbuffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( pis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T DataInputStream dis = new DataInputStream( gzis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // pipe big-endian ( Java ) binary write buffered uncompressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( pis, 4096/* buffsize in bytes */ ); // F O R M A T DataInputStream dis = new DataInputStream( bis ); // R E A D boolean q = dis.readBoolean(); byte b = dis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = dis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = dis.readChar(); // There is no readChars method double d = dis.readDouble(); float f = dis.readFloat(); int j = dis.readInt(); long l = dis.readLong(); short s = dis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = dis.readUTF(); byte ub = ( byte ) dis.readUnsignedByte(); short us = ( short ) dis.readUnsignedShort(); // C L O S E dis.close(); } // pipe big-endian ( Java ) binary write buffered compressed { // C O M M E N T // Read big-endian ( Java ) binary from a buffered compressed pipe. // Compression has built-in buffering. } // pipe little-endian ( Intel ) binary read unbuffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( pos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't send raw objects down a pipe. They need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // pipe little-endian ( Intel ) binary read unbuffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( pos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T // NOTE: LEDataOutputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataOutputStream ledos = new LEDataOutputStream( gzos ); // W R I T E ledos.writeBoolean( true ); ledos.writeByte( ( byte ) 44 ); ledos.writeBytes( "kangaroo"/* string -> LSB 8-bit */ ); ledos.writeChar( 'a' ); ledos.writeChars( "dingo"/* string 16-bit Unicode */ ); ledos.writeDouble( 3.14D ); ledos.writeFloat( 3.14F ); ledos.writeInt( 149 ); ledos.writeLong( 149L ); ledos.writeShort( ( short ) 149 ); // Do not use writeUTF to create Unicode files. // writeUTF creates binary files with counted Strings. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Writer.write with explicit UTF-16 or UTF-8 encoding instead. ledos.writeUTF( "echidna" ); // Note: You can't send raw objects down a pipe. They need to be serialised. // F L U S H ledos.flush(); // C L O S E ledos.close(); } // pipe little-endian ( Intel ) binary read buffered uncompressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe little-endian ( Intel ) binary read buffered compressed { // C O M M E N T // Write little-endian ( Intel ) binary into a buffered compressed pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe little-endian ( Intel ) binary write unbuffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( pis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // pipe little-endian ( Intel ) binary write unbuffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import java.util.zip.*; // import com.mindprod.ledatastream.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( pis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( gzis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // pipe little-endian ( Intel ) binary write buffered uncompressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // WARNING! Little endian is the native binary format on Windows/Intel machines. // The Java standard is big endian binary, with the most significant byte first. // I M P O R T // import java.io.*; // import com.mindprod.ledatastream.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( pis, 4096/* buffsize in bytes */ ); // F O R M A T // NOTE: LEDataInputStream is available free from Canadian Mind Products. // See http://mindprod.com/products.html#LEDATASTREAM. LEDataInputStream ledis = new LEDataInputStream( bis ); // R E A D boolean q = ledis.readBoolean(); byte b = ledis.readByte(); byte ba[] = new byte[ 1024 ]; // -1 means eof. // You don't necessarily get all you ask for in one read. // You get what's immediately available. int bytesRead = ledis.read( ba, 0/* offset in ba */, ba.length/* bytes to read */ ); char c = ledis.readChar(); // There is no readChars method double d = ledis.readDouble(); float f = ledis.readFloat(); int j = ledis.readInt(); long l = ledis.readLong(); short s = ledis.readShort(); // Do not use readUTF to read Unicode files. // readUTF reads binary counted Strings created by writeUTF. // The lead 16-bit byte count and UTF-8 encoding means Strings must be <= 10,922 chars!! // See http://mindprod.com/jgloss/utf.html#WRITEUTF for details.// Use Reader.read with an explicit UTF-8 or UTF-16 encoding instead. String u = ledis.readUTF(); byte ub = ( byte ) ledis.readUnsignedByte(); short us = ( short ) ledis.readUnsignedShort(); // C L O S E ledis.close(); } // pipe little-endian ( Intel ) binary write buffered compressed { // C O M M E N T // Read little-endian ( Intel ) binary from a buffered compressed pipe. // Compression has built-in buffering. } // pipe serialised binary objects read unbuffered uncompressed { // C O M M E N T // Write serialised binary objects into a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( pos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // pipe serialised binary objects read unbuffered compressed { // C O M M E N T // Write serialised binary objects into a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! producer/output/sender pipes can connect to at most one consumer/input/receiver pipe. // To allow multiple consumers, you need message-level synchronisation to a single consumer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedOutputStream pos = new PipedOutputStream(); // pos.connect( /* PipedInputStream */ receiver ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPOutputStream gzos = new GZIPOutputStream( pos, 4096/* buffsize in bytes */ ); // E N C O D E // B U F F E R // F O R M A T ObjectOutputStream oos = new ObjectOutputStream( gzos ); // W R I T E // object to write, and objects it points to must implement java.io.Serializable // Ideally such objects should also have have a field: // static final long serialVersionUID = 1L; // to prevent spurious InvalidClassExceptions. Object o = this; oos.writeObject( o ); // F L U S H oos.flush(); // C L O S E oos.close(); } // pipe serialised binary objects read buffered uncompressed { // C O M M E N T // Write serialised binary objects into a buffered pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe serialised binary objects read buffered compressed { // C O M M E N T // Write serialised binary objects into a buffered compressed pipe. // There is no point in buffering because pipe queues are stored in RAM. } // pipe serialised binary objects write unbuffered uncompressed { // C O M M E N T // Read serialised binary objects from a pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( pis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // pipe serialised binary objects write unbuffered compressed { // C O M M E N T // Read serialised binary objects from a compressed pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // import java.util.zip.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S GZIPInputStream gzis = new GZIPInputStream( pis, 4096/* buffsize in bytes */ ); // D E C O D E // B U F F E R // F O R M A T ObjectInputStream ois = new ObjectInputStream( gzis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // pipe serialised binary objects write buffered uncompressed { // C O M M E N T // Read serialised binary objects from a buffered pipe. // C A V E A T S // WARNING! Code to handle IOExceptions is not shown. // WARNING! This code is intended for teaching. In practice you would telescope some of the steps. // WARNING! consumer/input/receiver pipes can connect to at most one producer/output/sender pipe. // To allow multiple producers, you need message-level synchronisation to a single producer pipe. // WARNING! Pipes are for inter-thread communication only // not communication within a thread or between processes. // I M P O R T // import java.io.*; // O P E N PipedInputStream pis = new PipedInputStream(); // pis.connect( /* PipedOutputStream */ sender ); // connect() must be done by either sender or receiver but not both. // C O M P R E S S // D E C O D E // B U F F E R BufferedInputStream bis = new BufferedInputStream( pis, 4096/* buffsize in bytes */ ); // F O R M A T ObjectInputStream ois = new ObjectInputStream( bis ); // R E A D Object o = ois.readObject(); // C L O S E ois.close(); } // pipe serialised binary objects write buffered compressed { // C O M M E N T // Read serialised binary objects from a buffered compressed pipe. // Compression has built-in buffering. } } catch ( Exception e ) { } } }