KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jcifs > smb > Trans2FindFirst2Response


1 /* jcifs smb client library in Java
2  * Copyright (C) 2000 "Michael B. Allen" <jcifs at samba dot org>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package jcifs.smb;
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 import java.util.Date JavaDoc;
23
24 class Trans2FindFirst2Response extends SmbComTransactionResponse {
25
26     // information levels
27

28     static final int SMB_INFO_STANDARD = 1;
29     static final int SMB_INFO_QUERY_EA_SIZE = 2;
30     static final int SMB_INFO_QUERY_EAS_FROM_LIST = 3;
31     static final int SMB_FIND_FILE_DIRECTORY_INFO = 0x101;
32     static final int SMB_FIND_FILE_FULL_DIRECTORY_INFO = 0x102;
33     static final int SMB_FILE_NAMES_INFO = 0x103;
34     static final int SMB_FILE_BOTH_DIRECTORY_INFO = 0x104;
35
36     class SmbFindFileBothDirectoryInfo implements FileEntry {
37         int nextEntryOffset;
38         int fileIndex;
39         long creationTime;
40         long lastAccessTime;
41         long lastWriteTime;
42         long changeTime;
43         long endOfFile;
44         long allocationSize;
45         int extFileAttributes;
46         int fileNameLength;
47         int eaSize;
48         int shortNameLength;
49         String JavaDoc shortName;
50         String JavaDoc filename;
51
52         public String JavaDoc getName() {
53             return filename;
54         }
55         public int getType() {
56             return SmbFile.TYPE_FILESYSTEM;
57         }
58         public int getAttributes() {
59             return extFileAttributes;
60         }
61         public long createTime() {
62             return creationTime;
63         }
64         public long lastModified() {
65             return lastWriteTime;
66         }
67         public long length() {
68             return endOfFile;
69         }
70
71         public String JavaDoc toString() {
72             return new String JavaDoc( "SmbFindFileBothDirectoryInfo[" +
73                 "nextEntryOffset=" + nextEntryOffset +
74                 ",fileIndex=" + fileIndex +
75                 ",creationTime=" + new Date JavaDoc( creationTime ) +
76                 ",lastAccessTime=" + new Date JavaDoc( lastAccessTime ) +
77                 ",lastWriteTime=" + new Date JavaDoc( lastWriteTime ) +
78                 ",changeTime=" + new Date JavaDoc( changeTime ) +
79                 ",endOfFile=" + endOfFile +
80                 ",allocationSize=" + allocationSize +
81                 ",extFileAttributes=" + extFileAttributes +
82                 ",fileNameLength=" + fileNameLength +
83                 ",eaSize=" + eaSize +
84                 ",shortNameLength=" + shortNameLength +
85                 ",shortName=" + shortName +
86                 ",filename=" + filename + "]" );
87         }
88     }
89
90     int sid;
91     boolean isEndOfSearch;
92     int eaErrorOffset;
93     int lastNameOffset, lastNameBufferIndex;
94     String JavaDoc lastName;
95     int resumeKey;
96
97
98     Trans2FindFirst2Response() {
99         command = SMB_COM_TRANSACTION2;
100         subCommand = SmbComTransaction.TRANS2_FIND_FIRST2;
101     }
102
103     String JavaDoc readString( byte[] src, int srcIndex, int len ) {
104         String JavaDoc str = null;
105         try {
106             if( useUnicode ) {
107                 // should Unicode alignment be corrected for here?
108
str = new String JavaDoc( src, srcIndex, len, "UnicodeLittle" );
109             } else {
110     
111                 /* On NT without Unicode the fileNameLength
112                  * includes the '\0' whereas on win98 it doesn't. I
113                  * guess most clients only support non-unicode so
114                  * they don't run into this.
115                  */

116     
117     /* UPDATE: Maybe not! Could this be a Unicode alignment issue. I hope
118      * so. We cannot just comment out this method and use readString of
119      * ServerMessageBlock.java because the arguments are different, however
120      * one might be able to reduce this.
121      */

122     
123                 if( len > 0 && src[srcIndex + len - 1] == '\0' ) {
124                     len--;
125                 }
126                 str = new String JavaDoc( src, srcIndex, len, ServerMessageBlock.OEM_ENCODING );
127             }
128         } catch( UnsupportedEncodingException JavaDoc uee ) {
129             if( log.level > 1 )
130                 uee.printStackTrace( log );
131         }
132         return str;
133     }
134     int writeSetupWireFormat( byte[] dst, int dstIndex ) {
135         return 0;
136     }
137     int writeParametersWireFormat( byte[] dst, int dstIndex ) {
138         return 0;
139     }
140     int writeDataWireFormat( byte[] dst, int dstIndex ) {
141         return 0;
142     }
143     int readSetupWireFormat( byte[] buffer, int bufferIndex, int len ) {
144         return 0;
145     }
146     int readParametersWireFormat( byte[] buffer, int bufferIndex, int len ) {
147         int start = bufferIndex;
148
149         if( subCommand == SmbComTransaction.TRANS2_FIND_FIRST2 ) {
150             sid = readInt2( buffer, bufferIndex );
151             bufferIndex += 2;
152         }
153         numEntries = readInt2( buffer, bufferIndex );
154         bufferIndex += 2;
155         isEndOfSearch = ( buffer[bufferIndex] & 0x01 ) == 0x01 ? true : false;
156         bufferIndex += 2;
157         eaErrorOffset = readInt2( buffer, bufferIndex );
158         bufferIndex += 2;
159         lastNameOffset = readInt2( buffer, bufferIndex );
160         bufferIndex += 2;
161
162         return bufferIndex - start;
163     }
164     int readDataWireFormat( byte[] buffer, int bufferIndex, int len ) {
165         int start = bufferIndex;
166         SmbFindFileBothDirectoryInfo e;
167
168         lastNameBufferIndex = bufferIndex + lastNameOffset;
169
170         results = new SmbFindFileBothDirectoryInfo[numEntries];
171         for( int i = 0; i < numEntries; i++ ) {
172             results[i] = e = new SmbFindFileBothDirectoryInfo();
173
174             e.nextEntryOffset = readInt4( buffer, bufferIndex );
175             e.fileIndex = readInt4( buffer, bufferIndex + 4 );
176             e.creationTime = readTime( buffer, bufferIndex + 8 );
177     // e.lastAccessTime = readTime( buffer, bufferIndex + 16 );
178
e.lastWriteTime = readTime( buffer, bufferIndex + 24 );
179     // e.changeTime = readTime( buffer, bufferIndex + 32 );
180
e.endOfFile = readInt8( buffer, bufferIndex + 40 );
181     // e.allocationSize = readInt8( buffer, bufferIndex + 48 );
182
e.extFileAttributes = readInt4( buffer, bufferIndex + 56 );
183             e.fileNameLength = readInt4( buffer, bufferIndex + 60 );
184     // e.eaSize = readInt4( buffer, bufferIndex + 64 );
185
// e.shortNameLength = buffer[bufferIndex + 68] & 0xFF;
186

187             /* With NT, the shortName is in Unicode regardless of what is negotiated.
188              */

189
190     // e.shortName = readString( buffer, bufferIndex + 70, e.shortNameLength );
191
e.filename = readString( buffer, bufferIndex + 94, e.fileNameLength );
192
193             /* lastNameOffset ends up pointing to either to
194              * the exact location of the filename(e.g. Win98)
195              * or to the start of the entry containing the
196              * filename(e.g. NT). Ahhrg! In either case the
197              * lastNameOffset falls between the start of the
198              * entry and the next entry.
199              */

200
201             if( lastNameBufferIndex >= bufferIndex && ( e.nextEntryOffset == 0 ||
202                         lastNameBufferIndex < ( bufferIndex + e.nextEntryOffset ))) {
203                 lastName = e.filename;
204                 resumeKey = e.fileIndex;
205             }
206
207             bufferIndex += e.nextEntryOffset;
208         }
209
210         /* last nextEntryOffset for NT 4(but not 98) is 0 so we must
211          * use dataCount or our accounting will report an error for NT :~(
212          */

213
214         //return bufferIndex - start;
215

216         return dataCount;
217     }
218     public String JavaDoc toString() {
219         String JavaDoc c;
220         if( subCommand == SmbComTransaction.TRANS2_FIND_FIRST2 ) {
221             c = "Trans2FindFirst2Response[";
222         } else {
223             c = "Trans2FindNext2Response[";
224         }
225         return new String JavaDoc( c + super.toString() +
226             ",sid=" + sid +
227             ",searchCount=" + numEntries +
228             ",isEndOfSearch=" + isEndOfSearch +
229             ",eaErrorOffset=" + eaErrorOffset +
230             ",lastNameOffset=" + lastNameOffset +
231             ",lastName=" + lastName + "]" );
232     }
233 }
234
Popular Tags