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.entity;
20  
21  import java.io.ByteArrayOutputStream;
22  import java.io.IOException;
23  
24  import net.sf.dexterim.codec.HexPrinter;
25  
26  import org.apache.commons.lang.builder.EqualsBuilder;
27  import org.apache.commons.lang.builder.HashCodeBuilder;
28  import org.apache.commons.lang.builder.ToStringBuilder;
29  
30  /***
31   * @author christoph
32   */
33  public class Flap {
34  
35  	private int id;
36  	private int channel;
37  	private int sequence;
38  	private int length;
39  	private byte data[];
40  
41  	/***
42  	 *  
43  	 */
44  	public Flap() {
45  	}
46  
47  	public Flap(int id, int channel) {
48  		this.id = id;
49  		this.channel = channel;
50  	}
51  
52  	/***
53  	 * @return
54  	 */
55  	public int getChannel() {
56  		return channel;
57  	}
58  
59  	/***
60  	 * @param channel
61  	 */
62  	public void setChannel(int channel) {
63  		this.channel = channel;
64  	}
65  
66  	/***
67  	 * @return
68  	 */
69  	public int getId() {
70  		return id;
71  	}
72  
73  	/***
74  	 * @param id
75  	 */
76  	public void setId(int id) {
77  		this.id = id;
78  	}
79  
80  	/***
81  	 * @return
82  	 */
83  	public int getLength() {
84  		return length;
85  	}
86  
87  	/***
88  	 * @param length
89  	 */
90  	public void setLength(int length) {
91  		this.length = length;
92  	}
93  
94  	/***
95  	 * @return
96  	 */
97  	public int getSequence() {
98  		return sequence;
99  	}
100 
101 	/***
102 	 * @param sequence
103 	 */
104 	public void setSequence(int sequence) {
105 		this.sequence = sequence;
106 	}
107 
108 	/***
109 	 * @return
110 	 */
111 	public byte[] getData() {
112 		return data;
113 	}
114 
115 	/***
116 	 * @param data
117 	 */
118 	public void setData(byte[] data) {
119 		this.data = data;
120 	}
121 
122 	/*
123 	 * (non-Javadoc)
124 	 * 
125 	 * @see java.lang.Object#equals(java.lang.Object)
126 	 */
127 	public boolean equals(Object obj) {
128 		if (!(obj instanceof Flap)) {
129 			return false;
130 		}
131 
132 		Flap other = (Flap) obj;
133 
134 		return new EqualsBuilder().append(id, other.id).append(channel,
135 				other.channel).append(sequence, other.sequence)
136 				.append(data, other.data).isEquals();
137 	}
138 
139 	/***
140 	 * @see java.lang.Object#hashCode()
141 	 */
142 	public int hashCode() {
143 		return new HashCodeBuilder().append(this.id).append(this.channel).append(
144 				this.sequence).toHashCode();
145 	}
146 
147 	/***
148 	 * @see java.lang.Object#toString()
149 	 */
150 	public String toString() {
151 		return new ToStringBuilder(this).append("flap id", id).append("channel",
152 				channel).append("sequence", sequence).append("data",
153 				HexPrinter.format(data)).toString();
154 	}
155 
156 	public byte[] toByteArray() {
157 		ByteArrayOutputStream out = new ByteArrayOutputStream();
158 
159 		try {
160 			out.write(getId());
161 			out.write(getChannel());
162 			out.write(new Word(getSequence()).toByteArray());
163 			byte data[] = getData();
164 
165 			if (data != null) {
166 				out.write(new Word(data.length).toByteArray());
167 				out.write(data);
168 			} else {
169 				out.write(new Word(0).toByteArray());
170 			}
171 		} catch (IOException e) {
172 			e.printStackTrace();
173 		}
174 
175 		return out.toByteArray();
176 	}
177 }