jdk/test/java/util/zip/InflateIn_DeflateOut.java
changeset 5607 f01eda72e178
parent 4162 425328b81201
child 5627 e636ac7a63a4
equal deleted inserted replaced
5606:63aa4b61c0a8 5607:f01eda72e178
    21  * have any questions.
    21  * have any questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 4206909
    26  * @bug 4206909 4813885
    27  * @summary Test basic functionality of DeflaterOutputStream and InflaterInputStream including flush
    27  * @summary Test basic functionality of DeflaterOutputStream/InflaterInputStream and GZIPOutputStream/GZIPInputStream, including flush
    28  */
    28  */
    29 
    29 
    30 import java.io.*;
    30 import java.io.*;
    31 import java.util.*;
    31 import java.util.*;
    32 import java.util.zip.*;
    32 import java.util.zip.*;
    77             buf = newbuf;
    77             buf = newbuf;
    78         }
    78         }
    79     }
    79     }
    80 
    80 
    81     private static class PairedOutputStream extends ByteArrayOutputStream {
    81     private static class PairedOutputStream extends ByteArrayOutputStream {
    82       private PairedInputStream pairedStream = null;
    82         private PairedInputStream pairedStream = null;
    83 
    83 
    84       public PairedOutputStream(PairedInputStream inputPair) {
    84         public PairedOutputStream(PairedInputStream inputPair) {
    85         super();
    85             super();
    86         this.pairedStream = inputPair;
    86             this.pairedStream = inputPair;
    87       }
    87         }
    88 
    88 
    89       public void flush() {
    89         public void flush() {
    90         if (count > 0) {
    90             if (count > 0) {
    91           pairedStream.addBytes(buf, count);
    91                 pairedStream.addBytes(buf, count);
    92           reset();
    92                 reset();
    93         }
    93             }
    94       }
    94         }
    95 
    95 
    96       public void close() {
    96         public void close() {
    97         flush();
    97             flush();
    98       }
    98         }
    99     }
    99     }
   100 
   100 
   101     private static boolean readFully(InputStream in, byte[] buf, int length)
   101     private static boolean readFully(InputStream in, byte[] buf, int length)
   102             throws IOException {
   102             throws IOException {
   103         int pos = 0;
   103         int pos = 0;
   144         dos.close();
   144         dos.close();
   145         check(readFully(iis, buf, buf.length));
   145         check(readFully(iis, buf, buf.length));
   146         check(Arrays.equals(data, buf));
   146         check(Arrays.equals(data, buf));
   147     }
   147     }
   148 
   148 
   149     /** Check that written, flushed and read */
   149     private static void check(InputStream is, OutputStream os)
   150     private static void WriteFlushRead() throws Throwable {
   150         throws Throwable
       
   151     {
   151         Random random = new Random(new Date().getTime());
   152         Random random = new Random(new Date().getTime());
   152 
   153        // Large writes
   153         PairedInputStream pis = new PairedInputStream();
       
   154         InflaterInputStream iis = new InflaterInputStream(pis);
       
   155 
       
   156         PairedOutputStream pos = new PairedOutputStream(pis);
       
   157         pis.setPairedOutputStream(pos);
       
   158         DeflaterOutputStream dos = new DeflaterOutputStream(pos, true);
       
   159 
       
   160         // Large writes
       
   161         for (int x = 0; x < 200 ; x++) {
   154         for (int x = 0; x < 200 ; x++) {
   162             // byte[] data = new byte[random.nextInt(1024 * 1024)];
   155             // byte[] data = new byte[random.nextInt(1024 * 1024)];
   163             byte[] data = new byte[1024];
   156             byte[] data = new byte[1024];
   164             byte[] buf = new byte[data.length];
   157             byte[] buf = new byte[data.length];
   165             random.nextBytes(data);
   158             random.nextBytes(data);
   166 
   159 
   167             dos.write(data);
   160             os.write(data);
   168             dos.flush();
   161             os.flush();
   169             check(readFully(iis, buf, buf.length));
   162             check(readFully(is, buf, buf.length));
   170             check(Arrays.equals(data, buf));
   163             check(Arrays.equals(data, buf));
   171         }
   164         }
   172 
   165 
   173         // Small writes
   166         // Small writes
   174         for (int x = 0; x < 2000 ; x++) {
   167         for (int x = 0; x < 2000 ; x++) {
   175             byte[] data = new byte[random.nextInt(20) + 10];
   168             byte[] data = new byte[random.nextInt(20) + 10];
   176             byte[] buf = new byte[data.length];
   169             byte[] buf = new byte[data.length];
   177             random.nextBytes(data);
   170             random.nextBytes(data);
   178 
   171 
   179             dos.write(data);
   172             os.write(data);
   180             dos.flush();
   173             os.flush();
   181             if (!readFully(iis, buf, buf.length)) {
   174             if (!readFully(is, buf, buf.length)) {
   182                 fail("Didn't read full buffer of " + buf.length);
   175                 fail("Didn't read full buffer of " + buf.length);
   183             }
   176             }
   184             check(Arrays.equals(data, buf));
   177             check(Arrays.equals(data, buf));
   185         }
   178         }
   186 
   179 
   187         String quit = "QUIT\r\n";
   180         String quit = "QUIT\r\n";
   188 
   181 
   189         // Close it out
   182         // Close it out
   190         dos.write(quit.getBytes());
   183         os.write(quit.getBytes());
   191         dos.close();
   184         os.close();
   192 
   185 
   193         StringBuilder sb = new StringBuilder();
   186         StringBuilder sb = new StringBuilder();
   194         check(readLineIfAvailable(iis, sb));
   187         check(readLineIfAvailable(is, sb));
   195         equal(sb.toString(), quit);
   188         equal(sb.toString(), quit);
   196     }
   189     }
   197 
   190 
   198     /** Validate that we need to use flush at least once on a line
   191     /** Check that written, flushed and read */
   199      * oriented protocol */
   192     private static void WriteFlushRead() throws Throwable {
   200     private static void LineOrientedProtocol() throws Throwable {
       
   201         PairedInputStream pis = new PairedInputStream();
   193         PairedInputStream pis = new PairedInputStream();
   202         InflaterInputStream iis = new InflaterInputStream(pis);
   194         InflaterInputStream iis = new InflaterInputStream(pis);
   203 
   195 
   204         PairedOutputStream pos = new PairedOutputStream(pis);
   196         PairedOutputStream pos = new PairedOutputStream(pis);
   205         pis.setPairedOutputStream(pos);
   197         pis.setPairedOutputStream(pos);
   206         DeflaterOutputStream dos = new DeflaterOutputStream(pos, true);
   198         DeflaterOutputStream dos = new DeflaterOutputStream(pos, true);
   207 
   199 
       
   200         check(iis, dos);
       
   201     }
       
   202 
       
   203     private static void GZWriteFlushRead() throws Throwable {
       
   204         PairedInputStream pis = new PairedInputStream();
       
   205         PairedOutputStream pos = new PairedOutputStream(pis);
       
   206         pis.setPairedOutputStream(pos);
       
   207 
       
   208         GZIPOutputStream gos = new GZIPOutputStream(pos, true);
       
   209         gos.flush();  // flush the head out, so gis can read
       
   210         GZIPInputStream gis = new GZIPInputStream(pis);
       
   211 
       
   212         check(gis, gos);
       
   213     }
       
   214 
       
   215     private static void checkLOP(InputStream is, OutputStream os)
       
   216         throws Throwable
       
   217     {
   208         boolean flushed = false;
   218         boolean flushed = false;
   209         int count = 0;
   219         int count = 0;
   210 
   220 
   211         // Do at least a certain number of lines, but too many without a
   221         // Do at least a certain number of lines, but too many without a
   212         // flush means this test isn't testing anything
   222         // flush means this test isn't testing anything
   213         while ((count < 10 && flushed) || (count < 1000 && !flushed)) {
   223         while ((count < 10 && flushed) || (count < 1000 && !flushed)) {
   214             String command = "PING " + count + "\r\n";
   224             String command = "PING " + count + "\r\n";
   215             dos.write(command.getBytes());
   225             os.write(command.getBytes());
   216 
   226 
   217             StringBuilder buf = new StringBuilder();
   227             StringBuilder buf = new StringBuilder();
   218             if (!readLineIfAvailable(iis, buf)) {
   228             if (!readLineIfAvailable(is, buf)) {
   219                 flushed = true;
   229                 flushed = true;
   220                 dos.flush();
   230                 os.flush();
   221                 check(readLineIfAvailable(iis, buf));
   231                 check(readLineIfAvailable(is, buf));
   222             }
   232             }
   223             equal(buf.toString(), command);
   233             equal(buf.toString(), command);
   224             count++;
   234             count++;
   225         }
   235         }
   226         check(flushed);
   236         check(flushed);
   227     }
   237     }
   228 
   238 
       
   239     /** Validate that we need to use flush at least once on a line
       
   240      * oriented protocol */
       
   241     private static void LineOrientedProtocol() throws Throwable {
       
   242         PairedInputStream pis = new PairedInputStream();
       
   243         InflaterInputStream iis = new InflaterInputStream(pis);
       
   244 
       
   245         PairedOutputStream pos = new PairedOutputStream(pis);
       
   246         pis.setPairedOutputStream(pos);
       
   247         DeflaterOutputStream dos = new DeflaterOutputStream(pos, true);
       
   248 
       
   249         checkLOP(iis, dos);
       
   250     }
       
   251 
       
   252     private static void GZLineOrientedProtocol() throws Throwable {
       
   253         PairedInputStream pis = new PairedInputStream();
       
   254         PairedOutputStream pos = new PairedOutputStream(pis);
       
   255         pis.setPairedOutputStream(pos);
       
   256 
       
   257         GZIPOutputStream gos = new GZIPOutputStream(pos, true);
       
   258         gos.flush();  // flush the head out, so gis can read
       
   259         GZIPInputStream gis = new GZIPInputStream(pis);
       
   260 
       
   261         checkLOP(gis, gos);
       
   262     }
       
   263 
   229     public static void realMain(String[] args) throws Throwable {
   264     public static void realMain(String[] args) throws Throwable {
   230         WriteCloseRead();
   265         WriteCloseRead();
   231 
       
   232         WriteFlushRead();
   266         WriteFlushRead();
   233 
       
   234         LineOrientedProtocol();
   267         LineOrientedProtocol();
       
   268         GZWriteFlushRead();
       
   269         GZLineOrientedProtocol();
   235     }
   270     }
   236 
   271 
   237     //--------------------- Infrastructure ---------------------------
   272     //--------------------- Infrastructure ---------------------------
   238     static volatile int passed = 0, failed = 0;
   273     static volatile int passed = 0, failed = 0;
   239     static void pass() {passed++;}
   274     static void pass() {passed++;}