summaryrefslogtreecommitdiff
path: root/plugins/cvs/javacvs-src/org/netbeans/lib/cvsclient/ConnectionStreams.java
blob: 5484909b35f0306dce7549eba0069c2f482af492 (plain)
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.lib.cvsclient;

import org.netbeans.lib.cvsclient.connection.IConnection;
import org.netbeans.lib.cvsclient.file.IReaderFactory;
import org.netbeans.lib.cvsclient.file.IWriterFactory;
import org.netbeans.lib.cvsclient.io.IStreamLogger;
import org.netbeans.lib.cvsclient.util.BugLog;

import java.io.*;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.InflaterInputStream;

/**
 * @author  Thomas Singer
 */
public final class ConnectionStreams
        implements IConnectionStreams, IReaderFactory, IWriterFactory {

	// Fields =================================================================

	private final IConnection connection;
	private final IStreamLogger streamLogger;

	private InputStream loggedInputStream;
	private OutputStream loggedOutputStream;
	private Reader loggedReader;
	private Writer loggedWriter;
	private InputStream inputStream;
	private OutputStream outputStream;
	private DeflaterOutputStream deflaterOutputStream;
	private final String myCharset;

  // Setup ==================================================================

	public ConnectionStreams(IConnection connection, IStreamLogger streamLogger, String charset) {
		BugLog.getInstance().assertNotNull(connection);
		BugLog.getInstance().assertNotNull(streamLogger);

		this.connection = connection;
		this.streamLogger = streamLogger;
		this.myCharset = charset;

		setInputStream(connection.getInputStream());
		setOutputStream(connection.getOutputStream());
	}

	public Reader createReader(InputStream inputStream) {
		return new InputStreamReader(inputStream);
	}

	public Writer createWriter(OutputStream outputStream) {
		try {
			return new OutputStreamWriter(outputStream, myCharset);
		}
		catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	// Accessing ==============================================================

	public IReaderFactory getReaderFactory() {
		return this;
	}

	public IWriterFactory getWriterFactory() {
		return this;
	}

	public InputStream getLoggedInputStream() {
		return loggedInputStream;
	}

	public OutputStream getLoggedOutputStream() {
		return loggedOutputStream;
	}

	public Reader getLoggedReader() {
		return loggedReader;
	}

	public Writer getLoggedWriter() {
		return loggedWriter;
	}

	public InputStream getInputStream() {
		return inputStream;
	}

	public OutputStream getOutputStream() {
		return outputStream;
	}

  public void flushForReading() throws IOException {
    loggedWriter.flush();
    if (deflaterOutputStream != null) {
      deflaterOutputStream.finish();
    }
    loggedOutputStream.flush();
  }

	public void close() {
		try {
			if (loggedOutputStream != null) {
				try {
					loggedOutputStream.close();
				}
				catch (IOException ex) {
					BugLog.getInstance().showException(ex);
				}
			}

			if (loggedInputStream != null) {
				try {
					loggedInputStream.close();
				}
				catch (IOException ex) {
					BugLog.getInstance().showException(ex);
				}
			}
		}
		finally {
			try {
				connection.close();
			}
			catch (IOException ex) {
				BugLog.getInstance().showException(ex);
			}
		}
	}

	// Actions ================================================================

	// Actions ================================================================

	public void setGzipped() throws IOException {
		loggedWriter.flush();
		loggedOutputStream.flush();

		deflaterOutputStream = new DeflaterOutputStream(connection.getOutputStream(), new Deflater(6));
		setOutputStream(deflaterOutputStream);

		setInputStream(new InflaterInputStream(connection.getInputStream()));
	}

	// Utils ==================================================================

	private void setInputStream(InputStream inputStream) {
		this.inputStream = inputStream;

		this.loggedInputStream = streamLogger.createLoggingInputStream(inputStream);
		this.loggedReader = createReader(this.loggedInputStream);
	}

	private void setOutputStream(OutputStream outputStream) {
		this.outputStream = outputStream;

		this.loggedOutputStream = streamLogger.createLoggingOutputStream(outputStream);
		this.loggedWriter = createWriter(this.loggedOutputStream);
	}
}