File: java.io.SequenceInputStream

package info (click to toggle)
bock 0.20.2.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,228 kB
  • ctags: 1,370
  • sloc: ansic: 7,367; java: 5,553; yacc: 963; lex: 392; makefile: 243; sh: 90; perl: 42
file content (78 lines) | stat: -rw-r--r-- 1,620 bytes parent folder | download | duplicates (3)
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
// java.io.SequenceInputStream
// An implementation of the Java Language Specification section 22.8
// Written by Charles Briscoe-Smith; refer to the file LEGAL for details.

package java.io;

import java.util.Enumeration;

class StreamPair implements Enumeration {
	private InputStream s1, s2;
	StreamPair(InputStream s1, InputStream s2) { this.s1=s1; this.s2=s2; }
	public boolean hasMoreElements() { return s1!=null || s2!=null; }

	public Object nextElement()
	{
		Object rv=s1; s1=null;
		if (rv==null) { rv=s2; s2=null; }
		return rv;
	}
}

public class SequenceInputStream extends InputStream {

	private Enumeration source;
	private InputStream cur;

	public SequenceInputStream(Enumeration e)
	{
		source=e;
	}

	public SequenceInputStream(InputStream s1, InputStream s2)
	{
		source=new StreamPair(s1, s2);
	}

	public int read() throws IOException
	{
		if (cur==null)
			if (source.hasMoreElements())
				cur=(InputStream) source.nextElement();
			else
				return -1;
		int rv=cur.read();
		if (rv==-1) {
			cur.close();
			cur=null;
			return read();
		}
		return rv;
	}

	public int read(byte[] b, int off, int len)
			throws IOException, NullPointerException,
			       IndexOutOfBoundsException
	{
		if (cur==null)
			if (source.hasMoreElements())
				cur=(InputStream) source.nextElement();
			else
				return -1;
		int rv=cur.read(b, off, len);
		if (rv==-1) {
			cur.close();
			cur=null;
			return read(b, off, len);
		}
		return rv;
	}

	public void close() throws IOException
	{
		if (cur!=null) cur.close();
		while (source.hasMoreElements())
			((InputStream) source.nextElement()).close();
	}

}