KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > server > CoreResumeKey


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.server;
18
19 import java.io.PrintStream JavaDoc;
20
21 import org.alfresco.filesys.util.DataPacker;
22
23 /**
24  * Core protocol search resume key.
25  */

26 class CoreResumeKey
27 {
28     // Resume key offsets/lengths
29

30     private static final int RESBITS = 0;
31     private static final int FILENAME = 1;
32     private static final int RESSERVER = 12;
33     private static final int RESCONSUMER = 17;
34
35     private static final int FILENAMELEN = 11;
36     private static final int RESSRVLEN = 5;
37     private static final int RESCONSUMLEN = 4;
38
39     public static final int LENGTH = 21;
40
41     /**
42      * Dump the resume key to the specified output stream.
43      *
44      * @param out java.io.PrintStream
45      * @param buf byte[]
46      * @param pos int
47      */

48     public final static void DumpKey(PrintStream JavaDoc out, byte[] buf, int pos)
49     {
50
51         // Output the various resume key fields
52

53         out.print("[" + getReservedByte(buf, pos) + ", ");
54         out.print(getFileName(buf, pos, false) + "]");
55     }
56
57     /**
58      * Return the consumer area of the resume key.
59      *
60      * @return byte[]
61      */

62     public static final byte[] getConsumerArea(byte[] buf, int pos)
63     {
64         byte[] conArea = new byte[RESCONSUMLEN];
65         for (int i = 0; i < RESCONSUMLEN; i++)
66             conArea[i] = buf[pos + RESCONSUMER + i];
67         return conArea;
68     }
69
70     /**
71      * Return the file name from the resume key.
72      *
73      * @return java.lang.String
74      */

75     public static final String JavaDoc getFileName(byte[] buf, int pos, boolean dot)
76     {
77
78         // Check if we should return the file name in 8.3 format
79

80         if (dot)
81         {
82
83             // Build the 8.3 file name
84

85             StringBuffer JavaDoc name = new StringBuffer JavaDoc();
86             name.append(new String JavaDoc(buf, pos + FILENAMELEN, 8).trim());
87             name.append(".");
88             name.append(new String JavaDoc(buf, pos + FILENAMELEN + 8, 3).trim());
89
90             return name.toString();
91         }
92
93         // Return the raw string
94

95         return new String JavaDoc(buf, pos + FILENAME, FILENAMELEN).trim();
96     }
97
98     /**
99      * Return the reserved byte from the resume key.
100      *
101      * @return byte
102      */

103     public static final byte getReservedByte(byte[] buf, int pos)
104     {
105         return buf[pos];
106     }
107
108     /**
109      * Copy the resume key from the buffer to the user buffer.
110      *
111      * @param buf byte[]
112      * @param pos int
113      * @param key byte[]
114      */

115     public final static void getResumeKey(byte[] buf, int pos, byte[] key)
116     {
117
118         // Copy the resume key bytes
119

120         System.arraycopy(buf, pos, key, 0, LENGTH);
121     }
122
123     /**
124      * Return the server area resume key value. This is the search context index in our case.
125      *
126      * @return int Server resume key value ( search context index).
127      */

128     public static final int getServerArea(byte[] buf, int pos)
129     {
130         return DataPacker.getIntelInt(buf, pos + RESSERVER + 1);
131     }
132
133     /**
134      * Generate a resume key with the specified filename and search context id.
135      *
136      * @param buf byte[]
137      * @param pos
138      * @param fileName java.lang.String
139      * @param ctxId int
140      */

141     public final static void putResumeKey(byte[] buf, int pos, String JavaDoc fileName, int ctxId)
142     {
143
144         // Clear the reserved area
145

146         buf[pos + RESBITS] = 0x16;
147
148         // Put the file name in resume key format
149

150         setFileName(buf, pos, fileName);
151
152         // Put the server side reserved area
153

154         setServerArea(buf, pos, ctxId);
155         // setServerArea( buf, pos, 0);
156
}
157
158     /**
159      * Set the consumer reserved area value.
160      *
161      * @param conArea byte[]
162      */

163     public static final void setConsumerArea(byte[] buf, int pos, byte[] conArea)
164     {
165         for (int i = 0; i < RESCONSUMLEN; i++)
166             buf[pos + RESCONSUMER + i] = conArea[i];
167     }
168
169     /**
170      * Set the resume key file name string.
171      *
172      * @param name java.lang.String
173      */

174     public static final void setFileName(byte[] buf, int pos, String JavaDoc name)
175     {
176
177         // Split the file name string
178

179         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
180         int dot = name.indexOf(".");
181         if (dot != -1)
182         {
183             str.append(name.substring(0, dot));
184             while (str.length() < 8)
185                 str.append(" ");
186             str.append(name.substring(dot + 1, name.length()));
187         }
188         else
189             str.append(name);
190
191         // Space fill the file name to 11 characters
192

193         while (str.length() < FILENAMELEN)
194             str.append(" ");
195
196         // Pack the file name string into the resume key
197

198         DataPacker.putString(str.toString(), buf, pos + FILENAME, false);
199     }
200
201     /**
202      * Set the resume key reserved byte value.
203      *
204      * @param param byte
205      */

206     public static final void setReservedByte(byte[] buf, int pos, byte val)
207     {
208         buf[pos] = val;
209     }
210
211     /**
212      * Set the resume key server area value. This is the search context index in our case.
213      *
214      * @param srvVal int
215      */

216     public static final void setServerArea(byte[] buf, int pos, int srvVal)
217     {
218         buf[pos + RESSERVER] = 1;
219         DataPacker.putIntelInt(srvVal, buf, pos + RESSERVER + 1);
220     }
221 }
Popular Tags