1 package com.liviutudor.rmi.io.gzip;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.io.OutputStream;
6 import java.net.Socket;
7 import java.net.UnknownHostException;
8 import java.util.zip.GZIPInputStream;
9 import java.util.zip.GZIPOutputStream;
10
11
12
13
14
15
16 class GzipSocket extends Socket {
17
18 private GZIPInputStream inputStream;
19
20
21 private GZIPOutputStream outputStream;
22
23
24
25
26
27
28
29
30
31
32
33
34
35 public GzipSocket(String host, int port) throws UnknownHostException, IOException {
36 super(host, port);
37 }
38
39 @Override
40 public synchronized void close() throws IOException {
41 if (inputStream != null) {
42 inputStream.close();
43 inputStream = null;
44 }
45 if (outputStream != null) {
46 outputStream.flush();
47 outputStream.close();
48 outputStream = null;
49 }
50 super.close();
51 }
52
53
54
55
56
57
58
59
60
61
62
63
64
65 @Override
66 public synchronized InputStream getInputStream() throws IOException {
67 if (inputStream == null) {
68 inputStream = new GZIPInputStream(super.getInputStream());
69 }
70 return inputStream;
71 }
72
73
74
75
76
77
78
79
80
81
82 @Override
83 public synchronized OutputStream getOutputStream() throws IOException {
84 if (outputStream == null) {
85 outputStream = new GZIPOutputStream(super.getOutputStream());
86 }
87 return outputStream;
88 }
89
90 @Override
91 public void shutdownInput() throws IOException {
92
93 super.shutdownInput();
94 }
95
96 @Override
97 public void shutdownOutput() throws IOException {
98
99 super.shutdownOutput();
100 }
101 }