Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 981edda9ccee0bedb9c7110ea62bbde8e39b62e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/***************************************************************************
 * Copyright (c) 2004 - 2007 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.util.io;

import org.eclipse.net4j.util.HexUtil;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

/**
 * @author Eike Stepper
 */
public class GZIPStreamWrapper implements IStreamWrapper<GZIPInputStream, GZIPOutputStream>
{
  public GZIPStreamWrapper()
  {
  }

  public GZIPInputStream wrapInputStream(InputStream in) throws IOException
  {
    if (in instanceof GZIPInputStream)
    {
      return (GZIPInputStream)in;
    }

    return new GZIPInputStream(in);
  }

  public GZIPOutputStream wrapOutputStream(OutputStream out) throws IOException
  {
    if (out instanceof GZIPOutputStream)
    {
      return (GZIPOutputStream)out;
    }

    return new GZIPOutputStream(out);
  }

  public void finishInputStream(GZIPInputStream in) throws IOException
  {
  }

  public void finishOutputStream(GZIPOutputStream out) throws IOException
  {
    out.finish();
  }

  public static void main(String[] args) throws Exception
  {
    final PipedOutputStream pos = new PipedOutputStream();
    final PipedInputStream pis = new PipedInputStream(pos);

    final GZIPOutputStream gos = new GZIPOutputStream(pos);
    final byte[] out = "eike".getBytes();

    Thread thread = new Thread()
    {
      @Override
      public void run()
      {
        try
        {
          GZIPInputStream gis = new GZIPInputStream(pis);

          byte[] in = new byte[out.length];
          gis.read(in);
          gis.close();
          System.out.println("Read: " + HexUtil.bytesToHex(in));
        }
        catch (IOException ex)
        {
          throw new IORuntimeException(ex);
        }
      }
    };

    thread.start();
    Thread.sleep(1000);

    gos.write(out);
    gos.close();
    System.out.println("Written: " + HexUtil.bytesToHex(out));

    Thread.sleep(2000);
  }
}

Back to the top