aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/util/io/TimeoutInputStream.java
blob: 4d9f83d233cefe7618cd88e3b6d73abfda9c336e (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
/*
 * Copyright (C) 2009, 2013 Google Inc. and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

package org.eclipse.jgit.util.io;

import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InterruptedIOException;
import java.text.MessageFormat;

import org.eclipse.jgit.internal.JGitText;

/**
 * InputStream with a configurable timeout.
 */
public class TimeoutInputStream extends FilterInputStream {
	private final InterruptTimer myTimer;

	private int timeout;

	/**
	 * Wrap an input stream with a timeout on all read operations.
	 *
	 * @param src
	 *            base input stream (to read from). The stream must be
	 *            interruptible (most socket streams are).
	 * @param timer
	 *            timer to manage the timeouts during reads.
	 */
	public TimeoutInputStream(final InputStream src,
			final InterruptTimer timer) {
		super(src);
		myTimer = timer;
	}

	/**
	 * Get number of milliseconds before aborting a read.
	 *
	 * @return number of milliseconds before aborting a read.
	 */
	public int getTimeout() {
		return timeout;
	}

	/**
	 * Set number of milliseconds before aborting a read.
	 *
	 * @param millis
	 *            number of milliseconds before aborting a read. Must be > 0.
	 */
	public void setTimeout(int millis) {
		if (millis < 0)
			throw new IllegalArgumentException(MessageFormat.format(
					JGitText.get().invalidTimeout, Integer.valueOf(millis)));
		timeout = millis;
	}

	@Override
	public int read() throws IOException {
		try {
			beginRead();
			return super.read();
		} catch (InterruptedIOException e) {
			throw readTimedOut(e);
		} finally {
			endRead();
		}
	}

	@Override
	public int read(byte[] buf) throws IOException {
		return read(buf, 0, buf.length);
	}

	@Override
	public int read(byte[] buf, int off, int cnt) throws IOException {
		try {
			beginRead();
			return super.read(buf, off, cnt);
		} catch (InterruptedIOException e) {
			throw readTimedOut(e);
		} finally {
			endRead();
		}
	}

	@Override
	public long skip(long cnt) throws IOException {
		try {
			beginRead();
			return super.skip(cnt);
		} catch (InterruptedIOException e) {
			throw readTimedOut(e);
		} finally {
			endRead();
		}
	}

	private void beginRead() {
		myTimer.begin(timeout);
	}

	private void endRead() {
		myTimer.end();
	}

	private InterruptedIOException readTimedOut(InterruptedIOException e) {
		InterruptedIOException interrupted = new InterruptedIOException(
				MessageFormat.format(JGitText.get().readTimedOut,
						Integer.valueOf(timeout)));
		interrupted.initCause(e);
		return interrupted;
	}
}