KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > dcerpc > PolicyHandle


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb.dcerpc;
18
19 /**
20  * Policy Handle Class
21  */

22 public class PolicyHandle
23 {
24
25     // Length of a policy handle
26

27     public static final int POLICY_HANDLE_SIZE = 20;
28
29     // Policy handle bytes
30

31     private byte[] m_handle;
32
33     // Handle name
34

35     private String JavaDoc m_name;
36
37     /**
38      * Default constructor
39      */

40     public PolicyHandle()
41     {
42         setName("");
43     }
44
45     /**
46      * Class constructor
47      *
48      * @param buf byte[]
49      * @param off int
50      */

51     public PolicyHandle(byte[] buf, int off)
52     {
53         initialize(buf, off);
54         setName("");
55     }
56
57     /**
58      * Class constructor
59      *
60      * @param name String
61      * @param buf byte[]
62      * @param off int
63      */

64     public PolicyHandle(String JavaDoc name, byte[] buf, int off)
65     {
66         initialize(buf, off);
67         setName(name);
68     }
69
70     /**
71      * Determine if the policy handle is valid
72      *
73      * @return boolean
74      */

75     public final boolean isValid()
76     {
77         return m_handle != null ? true : false;
78     }
79
80     /**
81      * Return the policy handle bytes
82      *
83      * @return byte[]
84      */

85     public final byte[] getBytes()
86     {
87         return m_handle;
88     }
89
90     /**
91      * Return the policy handle name
92      *
93      * @return String
94      */

95     public final String JavaDoc getName()
96     {
97         return m_name;
98     }
99
100     /**
101      * Set the policy handle name
102      *
103      * @param name String
104      */

105     public final void setName(String JavaDoc name)
106     {
107         m_name = name;
108     }
109
110     /**
111      * Store the policy handle into the specified buffer
112      *
113      * @param buf byte[]
114      * @param off int
115      * @return int
116      */

117     public final int storePolicyHandle(byte[] buf, int off)
118     {
119
120         // Check if the policy handle is valid
121

122         if (isValid() == false)
123             return -1;
124
125         // Copy the policy handle bytes to the user buffer
126

127         for (int i = 0; i < POLICY_HANDLE_SIZE; i++)
128             buf[off + i] = m_handle[i];
129
130         // Return the new buffer position
131

132         return off + POLICY_HANDLE_SIZE;
133     }
134
135     /**
136      * Load the policy handle from the specified buffer
137      *
138      * @param buf byte[]
139      * @param off int
140      * @return int
141      */

142     public final int loadPolicyHandle(byte[] buf, int off)
143     {
144
145         // Load the policy handle from the buffer
146

147         initialize(buf, off);
148         return off + POLICY_HANDLE_SIZE;
149     }
150
151     /**
152      * Clear the handle
153      */

154     protected final void clearHandle()
155     {
156         m_handle = null;
157     }
158
159     /**
160      * Initialize the policy handle
161      *
162      * @param buf byte[]
163      * @param off int
164      */

165     private final void initialize(byte[] buf, int off)
166     {
167
168         // Copy the policy handle bytes
169

170         if ((off + POLICY_HANDLE_SIZE) <= buf.length)
171         {
172
173             // Allocate the policy handle buffer
174

175             m_handle = new byte[POLICY_HANDLE_SIZE];
176
177             // Copy the policy handle
178

179             for (int i = 0; i < POLICY_HANDLE_SIZE; i++)
180                 m_handle[i] = buf[off + i];
181         }
182     }
183
184     /**
185      * Return the policy handle as a string
186      *
187      * @return String
188      */

189     public String JavaDoc toString()
190     {
191         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
192
193         str.append("[");
194
195         if (getName() != null)
196             str.append(getName());
197         str.append(":");
198
199         if (isValid())
200         {
201             for (int i = 0; i < POLICY_HANDLE_SIZE; i++)
202             {
203                 int val = (int) (m_handle[i] & 0xFF);
204                 if (val <= 16)
205                     str.append("0");
206                 str.append(Integer.toHexString(val).toUpperCase());
207                 str.append("-");
208             }
209             str.setLength(str.length() - 1);
210             str.append("]");
211         }
212
213         return str.toString();
214     }
215 }
216
Popular Tags