View Javadoc
1   // ////////////////////////////////////////////////////////////////////////////
2   // dexterIM - Instant Messaging Framework
3   // Copyright (C) 2003 Christoph Walcher
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or
8   // (at your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  // GNU General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  //////////////////////////////////////////////////////////////////////////////
19  package net.sf.dexterim.oscar;
20  
21  import java.util.ArrayList;
22  import java.util.Iterator;
23  import java.util.List;
24  
25  import net.sf.dexterim.oscar.entity.ByteBased;
26  import net.sf.dexterim.oscar.entity.DWord;
27  import net.sf.dexterim.oscar.entity.Word;
28  
29  
30  /***
31   * @author christoph
32   *
33   */
34  public class BufferArray extends OscarByteBuffer {
35  
36  	private List buffers;
37  	private int position;
38  	
39  	private BufferArray(List buffers) {
40  		this.buffers = buffers;
41  		this.position = 0;
42  	}
43  	
44  	/* (non-Javadoc)
45  	 * @see net.sf.dexterim.oscar.OscarByteBuffer#position(int)
46  	 */
47  	public int position(int position) {
48  		int current = 0;
49  		int oldPosition = this.position;
50  		
51  		for (Iterator iter = buffers.iterator(); iter.hasNext();) {
52  			OscarByteBuffer element = (OscarByteBuffer) iter.next();
53  			
54  			if (position < current) {
55  				current += element.fill();
56  			}
57  			else {
58  				this.position = position;
59  			}
60  		}
61  		
62  		return oldPosition;
63  	}
64  
65  	/* (non-Javadoc)
66  	 * @see net.sf.dexterim.oscar.OscarByteBuffer#position()
67  	 */
68  	public int position() {
69  		return position;
70  	}
71  
72  	/* (non-Javadoc)
73  	 * @see net.sf.dexterim.oscar.OscarByteBuffer#size()
74  	 */
75  	public int size() {
76  		int size = 0;
77  		
78  		for (Iterator iter = buffers.iterator(); iter.hasNext();) {
79  			OscarByteBuffer element = (OscarByteBuffer) iter.next();
80  		
81  			size += element.size();
82  		}
83  		
84  		return size;
85  	}
86  
87  	/* (non-Javadoc)
88  	 * @see net.sf.dexterim.oscar.OscarByteBuffer#fill()
89  	 */
90  	public int fill() {
91  		int fill = 0;
92  		
93  		for (Iterator iter = buffers.iterator(); iter.hasNext();) {
94  			OscarByteBuffer element = (OscarByteBuffer) iter.next();
95  		
96  			fill += element.fill();
97  		}
98  		
99  		return fill;
100 	}
101 
102 	/* (non-Javadoc)
103 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#strip()
104 	 */
105 	public int strip() {
106 		throw new UnsupportedOperationException("Method not implemented");
107 	}
108 
109 	/* (non-Javadoc)
110 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#getWord()
111 	 */
112 	public Word getWord() {
113 		throw new UnsupportedOperationException("Method not implemented");
114 	}
115 
116 	/* (non-Javadoc)
117 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#getDWord()
118 	 */
119 	public DWord getDWord() {
120 		throw new UnsupportedOperationException("Method not implemented");
121 	}
122 
123 	/* (non-Javadoc)
124 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#getByte()
125 	 */
126 	public int getByte() {
127 		throw new UnsupportedOperationException("Method not implemented");
128 	}
129 
130 	/* (non-Javadoc)
131 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#write(net.sf.dexterim.oscar.entity.ByteBased)
132 	 */
133 	public OscarByteBuffer write(ByteBased source) {
134 		throw new UnsupportedOperationException("Method not implemented");
135 	}
136 
137 	/* (non-Javadoc)
138 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#write(byte)
139 	 */
140 	public OscarByteBuffer write(byte source) {
141 		throw new UnsupportedOperationException("Method not implemented");
142 	}
143 
144 	/* (non-Javadoc)
145 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#write(net.sf.dexterim.oscar.OscarByteBuffer)
146 	 */
147 	public OscarByteBuffer write(OscarByteBuffer source) {
148 		throw new UnsupportedOperationException("Method not implemented");
149 	}
150 
151 	/* (non-Javadoc)
152 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#array()
153 	 */
154 	public byte[] array() {
155 		// TODO Auto-generated method stub
156 		return null;
157 	}
158 
159 	/* (non-Javadoc)
160 	 * @see net.sf.dexterim.oscar.OscarByteBuffer#slice()
161 	 */
162 	public byte[] slice() {
163 		byte[] slice = new byte[fill()];
164 		int index = 0;
165 		
166 		for (Iterator iter = buffers.iterator(); iter.hasNext();) {
167 			OscarByteBuffer buffer = (OscarByteBuffer) iter.next();
168 			
169 			byte[] bufferSlice = buffer.slice();
170 			
171 			System.arraycopy(bufferSlice, 0, slice, index, bufferSlice.length);
172 			index += bufferSlice.length;
173 		}
174 
175 		return slice;
176 	}
177 
178 	public static OscarByteBuffer createBufferArray(OscarByteBuffer first, OscarByteBuffer second) {
179 		List buffers = new ArrayList(2);
180 		buffers.add(first);
181 		buffers.add(second);
182 		
183 		return new BufferArray(buffers);
184 	}
185 	
186 	public static OscarByteBuffer createBufferArray(List buffers) {
187 		return new BufferArray(buffers);
188 	}
189 }