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.ToStringBuilder;
27  
28  /***
29   * @author christoph
30   */
31  public class Snac {
32  
33  	private int family;
34  	private int type;
35  	private int flags;
36  	private int request;
37  
38  	private byte data[];
39  
40  	public Snac() {
41  		this.request = 0x0000;
42  	}
43  
44  	public Snac(int family, int type, int flags) {
45  		this();
46  
47  		this.family = family;
48  		this.type = type;
49  		this.flags = flags;
50  	}
51  
52  	/***
53  	 * @return
54  	 */
55  	public int getFamily() {
56  		return family;
57  	}
58  
59  	/***
60  	 * @return
61  	 */
62  	public int getFlags() {
63  		return flags;
64  	}
65  
66  	/***
67  	 * @return
68  	 */
69  	public int getRequest() {
70  		return request;
71  	}
72  
73  	/***
74  	 * @param request
75  	 */
76  	public void setRequest(int request) {
77  		this.request = request;
78  	}
79  
80  	/***
81  	 * @return
82  	 */
83  	public int getType() {
84  		return type;
85  	}
86  
87  	/***
88  	 * @return
89  	 */
90  	public byte[] getData() {
91  		return data;
92  	}
93  
94  	/***
95  	 * @param data
96  	 */
97  	public void setData(byte[] data) {
98  		this.data = data;
99  	}
100 
101 	public String toString() {
102 		return new ToStringBuilder(this).append(
103 				"family", family).append("type", type).append("falgs", flags).append(
104 				"request id", request).append("data", HexPrinter.format(this.data))
105 				.toString();
106 	}
107 
108 	public byte[] toByteArray() {
109 		ByteArrayOutputStream out = new ByteArrayOutputStream();
110 
111 		try {
112 
113 			out.write(new Word(getFamily()).toByteArray());
114 			out.write(new Word(getType()).toByteArray());
115 			out.write(new Word(getFlags()).toByteArray());
116 			out.write(new DWord(request).toByteArray());
117 
118 			if (getData() != null) {
119 				out.write(getData());
120 			}
121 		} catch (IOException e) {
122 			e.printStackTrace();
123 		}
124 
125 		return out.toByteArray();
126 	}
127 }