Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: c9fa04f263886f9d5bd7f608834da6f3c060ed7e (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
/*******************************************************************************
 * Copyright (c) 2009  Clark N. Hobbie
 * 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:
 *     Clark N. Hobbie - initial API and implementation
 *******************************************************************************/
package org.eclipse.ecf.ipc.fifo;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.ecf.ipc.IPCException;


public class FIFOInputStream extends InputStream
{
	private FIFO myPipe;
	private byte[] myBuffer;
	private int myIndex;
	private boolean myEndOfFile;
	private int myDataSize;
	private int myTimeout;
	
	public boolean isEndOfFile()
	{
		return myEndOfFile;
	}

	public void setEndOfFile(boolean endOfFile)
	{
		myEndOfFile = endOfFile;
	}

	public FIFOInputStream(FIFO pipe)
	{
		initialize(pipe, -1);
	}
	
	public FIFOInputStream(FIFO fifo, int timeoutMsec) 
	{
		initialize(fifo, timeoutMsec);
	}

	protected void initialize(FIFO pipe, int timeoutMsec)
	{
		myPipe = pipe;
		myBuffer = new byte[8192];
		myIndex = -1;
		setEndOfFile(false);
		myTimeout = timeoutMsec;
	}
	
	
	public int read() throws IOException
	{
		if (isEndOfFile())
			return -1;
		
		if (-1 == myIndex || myIndex >= myDataSize)
		{
			loadBuffer();
		}
		
		if (isEndOfFile())
			return -1;
		
		int bvalue = myBuffer[myIndex];
		myIndex++;
		return bvalue;
	}
	
	protected int bytesAvailable()
	{
		if (myIndex < 0)
			return 0;
		
		return myDataSize - myIndex;
	}
	
	public int read(byte[] buffer, int offset, int length) throws IOException
	{
		if (isEndOfFile())
			return -1;
		
		//
		// if we don't currently have any data, load some
		//
		if (bytesAvailable() < 1)
			loadBuffer();
		
		if (isEndOfFile())
			return -1;
		
		int count = bytesAvailable();
		int index = 0;
		while (index < count && index < length && index < buffer.length)
		{
			buffer[index + offset] = (byte) read();
			index++;
		}

		return count;
	}

	protected void loadBuffer() throws IOException
	{
		try
		{
			if (-1 == myTimeout)
			{
				myDataSize = myPipe.read(myBuffer);
			}
			else
			{
			}
			myIndex = 0;
			
			if (-1 == myDataSize)
			{
				setEndOfFile(true);
			}
		}
		catch (IPCException e)
		{
			throw new IOException("Error reading data from pipe", e);
		}
	}
}

Back to the top