Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: b0175744efe1b705cf7f2bfa8164c8249adc5096 (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
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
/*******************************************************************************
 * Copyright (c) 2011 Wind River Systems, Inc. and others. 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:
 * William Chen (Wind River) - [345552] Edit the remote files with a proper editor
 *******************************************************************************/
package org.eclipse.tm.te.tcf.filesystem.internal.url;

import java.io.IOException;
import java.io.OutputStream;

import org.eclipse.tm.tcf.protocol.IToken;
import org.eclipse.tm.tcf.services.IFileSystem;
import org.eclipse.tm.tcf.services.IFileSystem.DoneWrite;
import org.eclipse.tm.tcf.services.IFileSystem.FileSystemException;
import org.eclipse.tm.te.tcf.filesystem.internal.nls.Messages;

/**
 * The TCF output stream returned by {@link TcfURLConnection#getOutputStream()}.
 *
 */
public class TcfOutputStream extends OutputStream {
	// Default chunk size while pumping the data.
	private static final int DEFAULT_CHUNK_SIZE = 5 * 1024;

	// Current writing position
	long position;
	// The byte array used to buffer data.
	byte[] buffer;
	// The offset being written in the buffer.
	int offset;

	// If the stream has been closed.
	boolean closed;
	// The current error during writing.
	Exception ERROR;

	// The chunk size of the writing buffer.
	int chunk_size = 0;

	// The URL Connection
	TcfURLConnection connection;

	// The timeout for writing data.
	int timeout;
	/**
	 * Create a TCF output stream connected the specified peer with specified
	 * path to the remote resource.
	 *
	 * @param peer
	 *            The TCF agent peer.
	 * @param path
	 *            The path to the remote resource.
	 */
	public TcfOutputStream(TcfURLConnection connection) {
		this(connection, DEFAULT_CHUNK_SIZE);
	}

	/**
	 * Create a TCF output stream connected the specified peer with specified
	 * path to the remote resource using the specified buffer size.
	 *
	 * @param peer
	 *            The TCF agent peer.
	 * @param path
	 *            The path to the remote resource.
	 * @param chunk_size
	 *            The buffer size.
	 */
	public TcfOutputStream(TcfURLConnection connection, int chunk_size) {
		this.connection = connection;
		this.chunk_size = chunk_size;
		buffer = new byte[chunk_size];
		offset = 0;
	}

	/**
	 * Set the timeout for writing a file.
	 *
	 * @param timeout The timeout for writing a file.
	 */
	void setTimeout(int timeout){
		this.timeout = timeout;
	}

	/* (non-Javadoc)
	 * @see java.io.OutputStream#write(int)
	 */
	@Override
	public void write(int b) throws IOException {
		if (closed)
			throw new IOException(Messages.TcfOutputStream_StreamClosed);
		if (ERROR != null) {
			IOException exception = new IOException(ERROR.toString());
			exception.initCause(ERROR);
			throw exception;
		}
		if (offset < buffer.length) {
			buffer[offset++] = (byte) b;
		}
		if (offset == buffer.length)
			flush();
	}

	/* (non-Javadoc)
	 * @see java.io.OutputStream#flush()
	 */
	@Override
	public void flush() throws IOException {
		if (offset > 0) {
			final Rendezvous rendezvous = new Rendezvous();
			IFileSystem service = connection.handle.getService();
			service.write(connection.handle, position, buffer, 0, offset, new DoneWrite() {
				@Override
				public void doneWrite(IToken token, FileSystemException error) {
					if (error != null) {
						ERROR = error;
					}
					position += offset;
					offset = 0;
					rendezvous.arrive();
				}
			});
			// Waiting for writing.
			try {
				rendezvous.waiting(timeout);
			} catch (InterruptedException e) {
				ERROR = new IOException(Messages.TcfOutputStream_WriteTimeout);
			}
		}
	}

	/* (non-Javadoc)
	 * @see java.io.OutputStream#close()
	 */
	@Override
	public void close() throws IOException {
		if (!closed) {
			connection.closeStream(this);
			closed = true;
		}
	}
}

Back to the top