~ubuntu-branches/ubuntu/utopic/eclipse-eclox/utopic

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
/*******************************************************************************
 * Copyright (C) 2003-2004, 2013, Guillaume Brocker
 * 
 * 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:
 *     Guillaume Brocker - Initial API and implementation
 *
 ******************************************************************************/ 

package eclox.core.doxyfiles.io;

import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import eclox.core.doxyfiles.Chunk;
import eclox.core.doxyfiles.Doxyfile;

/**
 * Implements a doxyfile content serializer.
 * 
 * @author willy
 */
public class Serializer extends InputStream {
	
	/**
	 * an iterator on the doxyfile chunks
	 */
	private Iterator chunkIterator;
	
	/**
	 * a string buffer containing the next character to red
	 */
	private StringBuffer stringBuffer;
	
	/**
	 * Constructor
	 * 
	 * @param	doxyfile	a doxyfile to serialize
	 */
	public Serializer( Doxyfile doxyfile ) {
		this.chunkIterator = doxyfile.iterator();
		this.stringBuffer = getNextStringBuffer();
	}

	public int read() throws IOException {
		int	result;
		if( stringBuffer != null ) {
			// Retrieves the next character from the current string buffer.
			result = stringBuffer.charAt( 0 );
			stringBuffer.deleteCharAt( 0 );
			
			// If the current string buffer has been entierly read, gets the next string buffer.
			if( stringBuffer.length() == 0 ) {
				stringBuffer = getNextStringBuffer();
			}
		}
		else {
			result = -1;
		}
		return result;
	}
	
	/**
	 * Retrieves the next string buffer to use for reading operations or null
	 * if no more chunk is left in the doxyfile.
	 * 
	 * @return	a string buffer or null of none
	 */
	private StringBuffer getNextStringBuffer() {
        // Pre-condition
        assert chunkIterator != null;
        
        // Retrieves the next string buffer.
		StringBuffer result = null;
		if( this.chunkIterator.hasNext() == true ) {
			Chunk	chunk = (Chunk) this.chunkIterator.next();
			result = new StringBuffer( chunk.toString() );
		}
		return result;
	}

}