Revision 19445
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/dbf/filedbf/DbaseFile.java | ||
---|---|---|
22 | 22 |
import java.util.Date; |
23 | 23 |
import java.util.Locale; |
24 | 24 |
|
25 |
import org.gvsig.data.datastores.vectorial.driver.exception.FileNotFoundDriverException; |
|
26 |
import org.gvsig.data.datastores.vectorial.driver.exception.UnsupportedEncodingDriverException; |
|
27 |
import org.gvsig.data.exception.CloseException; |
|
28 |
import org.gvsig.data.exception.UnsupportedVersionException; |
|
29 |
import org.gvsig.data.exception.WriteException; |
|
30 |
|
|
25 | 31 |
import com.iver.utiles.bigfile.BigByteBuffer2; |
26 | 32 |
|
27 | 33 |
/** |
... | ... | |
247 | 253 |
|
248 | 254 |
} |
249 | 255 |
|
250 |
public void setFieldValue(int rowIndex, int fieldId, Object obj) |
|
251 |
throws IOException {
|
|
256 |
public void setFieldValue(int rowIndex, int fieldId, Object obj) throws UnsupportedEncodingDriverException, WriteException {
|
|
257 |
try{
|
|
252 | 258 |
int fieldOffset = myHeader.getFieldDescription(fieldId).myFieldDataAddress; |
253 | 259 |
String str = fieldString(obj, fieldId); |
254 | 260 |
byte[] data = new byte[myHeader.getFieldLength(fieldId)]; |
... | ... | |
262 | 268 |
aux.flip(); |
263 | 269 |
int numBytesWritten = channel.write(aux, recordOffset + fieldOffset); |
264 | 270 |
//channel.force(true); |
271 |
}catch (UnsupportedEncodingException e) { |
|
272 |
throw new UnsupportedEncodingDriverException("DBF",e); |
|
273 |
}catch (IOException e) { |
|
274 |
throw new WriteException("DBF",e); |
|
275 |
} |
|
265 | 276 |
|
266 |
|
|
267 | 277 |
} |
268 | 278 |
|
269 | 279 |
|
... | ... | |
343 | 353 |
* |
344 | 354 |
* @param file |
345 | 355 |
* DOCUMENT ME! |
356 |
* @throws FileNotFoundDriverException |
|
357 |
* @throws UnsupportedVersionException |
|
346 | 358 |
* |
347 | 359 |
* @throws IOException |
348 | 360 |
* DOCUMENT ME! |
349 | 361 |
*/ |
350 |
public void open(File file) { |
|
362 |
public void open(File file) throws FileNotFoundDriverException, UnsupportedVersionException {
|
|
351 | 363 |
/* |
352 | 364 |
* 01h DOS USA code page 437 02h DOS Multilingual code page 850 03h |
353 | 365 |
* Windows ANSI code page 1252 04h Standard Macintosh 64h EE MS-DOS code |
... | ... | |
423 | 435 |
} |
424 | 436 |
bytesCachedRecord = new byte[myHeader.getRecordLength()]; |
425 | 437 |
}catch (IOException e) { |
426 |
// throw new FileNotFoundDriverException("DBF",e,file.getAbsolutePath());
|
|
438 |
throw new FileNotFoundDriverException("DBF",e,file.getAbsolutePath()); |
|
427 | 439 |
} |
428 | 440 |
} |
429 | 441 |
|
430 | 442 |
/** |
431 | 443 |
* Removes all data from the dataset |
444 |
* @throws CloseException |
|
432 | 445 |
* |
433 | 446 |
* @throws IOException |
434 | 447 |
* DOCUMENT ME! |
435 | 448 |
*/ |
436 |
public void close() throws IOException { |
|
449 |
public void close() throws CloseException { |
|
450 |
try{ |
|
437 | 451 |
raf.close(); |
438 | 452 |
channel.close(); |
439 | 453 |
buffer = null; |
440 | 454 |
posActual=-1; |
455 |
}catch (Exception e) { |
|
456 |
throw new CloseException("DBF",e); |
|
457 |
} |
|
441 | 458 |
} |
442 | 459 |
|
443 | 460 |
public FileChannel getWriteChannel() { |
... | ... | |
491 | 508 |
|
492 | 509 |
return o; |
493 | 510 |
} |
494 |
////////////////////////////7777 |
|
495 |
public void setNumRecords(int numRows) { |
|
496 |
myHeader.setNumRecords(numRows); |
|
497 | 511 |
|
498 |
} |
|
499 |
|
|
500 |
public DbaseFileHeader getHeader() { |
|
501 |
return myHeader; |
|
502 |
} |
|
503 |
/////////////////////////////7777 |
|
504 | 512 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/dbf/filedbf/DbaseFileHeader.java | ||
---|---|---|
13 | 13 |
import java.util.Calendar; |
14 | 14 |
import java.util.Date; |
15 | 15 |
|
16 |
import org.gvsig.data.datastores.vectorial.driver.exception.BadFieldDriverException; |
|
17 |
import org.gvsig.data.exception.UnsupportedVersionException; |
|
16 | 18 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
17 | 19 |
import org.gvsig.data.vectorial.IFeatureType; |
18 | 20 |
|
... | ... | |
73 | 75 |
* @param inFieldType DOCUMENT ME! |
74 | 76 |
* @param inFieldLength DOCUMENT ME! |
75 | 77 |
* @param inDecimalCount DOCUMENT ME! |
78 |
* @throws BadFieldDriverException |
|
76 | 79 |
* |
77 | 80 |
* @throws Exception DOCUMENT ME! |
78 | 81 |
*/ |
79 | 82 |
public void addColumn(String inFieldName, char inFieldType, |
80 |
int inFieldLength, int inDecimalCount) { |
|
83 |
int inFieldLength, int inDecimalCount) throws BadFieldDriverException {
|
|
81 | 84 |
if (inFieldLength <= 0) { |
82 | 85 |
inFieldLength = 1; |
83 | 86 |
} |
... | ... | |
189 | 192 |
|
190 | 193 |
tempFieldDescriptors[myFieldDescriptions.length].myFieldLength = 1; |
191 | 194 |
} else { |
192 |
// throw new Exception("Undefined field type " + inFieldType +
|
|
193 |
// " For column " + inFieldName);
|
|
195 |
throw new BadFieldDriverException("Undefined field type " + inFieldType +
|
|
196 |
" For column " + inFieldName,new Exception());
|
|
194 | 197 |
} |
195 | 198 |
|
196 | 199 |
// the length of a record |
... | ... | |
339 | 342 |
* Read the header data from the DBF file. |
340 | 343 |
* |
341 | 344 |
* @param in DOCUMENT ME! |
345 |
* @throws UnsupportedVersionException |
|
342 | 346 |
* |
343 | 347 |
* @throws IOException DOCUMENT ME! |
344 | 348 |
*/ |
345 |
public void readHeader(BigByteBuffer2 in) throws IOException {
|
|
349 |
public void readHeader(BigByteBuffer2 in) throws UnsupportedVersionException {
|
|
346 | 350 |
// type of file. |
347 | 351 |
myFileType = in.get(); |
348 | 352 |
|
349 | 353 |
if (myFileType != 0x03) { |
350 |
throw new IOException("Unsupported DBF file Type " +
|
|
351 |
Integer.toHexString(myFileType)); |
|
354 |
throw new UnsupportedVersionException("Unsupported DBF file Type " +
|
|
355 |
Integer.toHexString(myFileType),new Exception());
|
|
352 | 356 |
} |
353 | 357 |
|
354 | 358 |
// parse the update date information. |
... | ... | |
520 | 524 |
return myLanguageID; |
521 | 525 |
} |
522 | 526 |
|
523 |
public static DbaseFileHeader createDbaseHeader(IFeatureType featureType) { |
|
527 |
public static DbaseFileHeader createDbaseHeader(IFeatureType featureType) throws BadFieldDriverException {
|
|
524 | 528 |
DbaseFileHeader header = new DbaseFileHeader(); |
525 | 529 |
|
526 | 530 |
for (int i=0;i<featureType.size();i++) { |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/dbf/filedbf/DbaseFileWriter.java | ||
---|---|---|
63 | 63 |
package org.gvsig.data.datastores.vectorial.driver.dbf.filedbf; |
64 | 64 |
|
65 | 65 |
import java.io.IOException; |
66 |
import java.io.UnsupportedEncodingException; |
|
66 | 67 |
import java.nio.ByteBuffer; |
67 | 68 |
import java.nio.MappedByteBuffer; |
68 | 69 |
import java.nio.channels.FileChannel; |
... | ... | |
74 | 75 |
import java.util.Date; |
75 | 76 |
import java.util.Locale; |
76 | 77 |
|
78 |
import org.gvsig.data.datastores.vectorial.driver.exception.UnsupportedEncodingDriverException; |
|
79 |
import org.gvsig.data.exception.CloseException; |
|
80 |
import org.gvsig.data.exception.InitializeWriterException; |
|
81 |
import org.gvsig.data.exception.WriteException; |
|
77 | 82 |
import org.gvsig.data.vectorial.IFeature; |
78 | 83 |
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor; |
79 | 84 |
import org.gvsig.data.vectorial.IFeatureType; |
... | ... | |
111 | 116 |
* channel. |
112 | 117 |
* @param header The DbaseFileHeader to write. |
113 | 118 |
* @param out The Channel to write to. |
119 |
* @throws InitializeWriterException |
|
114 | 120 |
* @throws IOException If errors occur while initializing. |
115 | 121 |
*/ |
116 |
public DbaseFileWriter(DbaseFileHeader header,FileChannel out) throws IOException { |
|
117 |
header.writeHeader(out); |
|
122 |
public DbaseFileWriter(DbaseFileHeader header,FileChannel out) throws InitializeWriterException { |
|
123 |
try { |
|
124 |
header.writeHeader(out); |
|
125 |
} catch (IOException e) { |
|
126 |
throw new InitializeWriterException("DBF Writer",e); |
|
127 |
} |
|
118 | 128 |
this.header = header; |
119 | 129 |
this.channel = out; |
120 | 130 |
|
121 | 131 |
init(); |
122 | 132 |
} |
123 | 133 |
|
124 |
private void init() throws IOException { |
|
134 |
private void init() throws InitializeWriterException { |
|
135 |
try{ |
|
125 | 136 |
buffer = ByteBuffer.allocateDirect(header.getRecordLength()); |
137 |
}catch (Exception e) { |
|
138 |
throw new InitializeWriterException("DBF Writer",e); |
|
139 |
} |
|
126 | 140 |
} |
127 | 141 |
|
128 |
private void write() throws IOException {
|
|
142 |
private void write() throws WriteException {
|
|
129 | 143 |
buffer.position(0); |
130 | 144 |
int r = buffer.remaining(); |
131 |
while ( (r -= channel.write(buffer)) > 0) { |
|
132 |
; // do nothing |
|
133 |
} |
|
145 |
try { |
|
146 |
while ( (r -= channel.write(buffer)) > 0) { |
|
147 |
; // do nothing |
|
148 |
} |
|
149 |
} catch (IOException e) { |
|
150 |
throw new WriteException("DBF Writer",e); |
|
151 |
} |
|
134 | 152 |
} |
135 | 153 |
|
136 | 154 |
|
137 | 155 |
/** Write a single dbase record. |
138 | 156 |
* @param record The entries to write. |
157 |
* @throws UnsupportedEncodingException |
|
158 |
* @throws WriteException |
|
159 |
* @throws UnsupportedEncodingDriverException |
|
139 | 160 |
* @throws IOException If IO error occurs. |
140 | 161 |
* @throws DbaseFileException If the entry doesn't comply to the header. |
141 | 162 |
*/ |
142 |
public void write(IFeature feature) throws IOException{
|
|
163 |
public void write(IFeature feature) throws WriteException, UnsupportedEncodingDriverException{
|
|
143 | 164 |
IFeatureType featureType=feature.getType(); |
144 | 165 |
|
145 | 166 |
buffer.position(0); |
... | ... | |
159 | 180 |
fieldString = "0"; |
160 | 181 |
} |
161 | 182 |
} |
162 |
buffer.put(fieldString.getBytes(charset.name())); |
|
183 |
try { |
|
184 |
buffer.put(fieldString.getBytes(charset.name())); |
|
185 |
} catch (UnsupportedEncodingException e) { |
|
186 |
throw new UnsupportedEncodingDriverException("DBF Writer",e); |
|
187 |
} |
|
163 | 188 |
} |
164 | 189 |
write(); |
165 | 190 |
} |
166 | 191 |
|
167 | 192 |
/** Write a single dbase record. Useful to update a dbf. |
168 | 193 |
* @param record The entries to write. |
194 |
* @throws WriteException |
|
195 |
* @throws UnsupportedEncodingException |
|
169 | 196 |
* @throws IOException If IO error occurs. |
170 | 197 |
* @throws DbaseFileException If the entry doesn't comply to the header. |
171 | 198 |
*/ |
172 |
public void writeRecord(IFeature feature, int numReg) throws IOException{
|
|
199 |
public void writeRecord(IFeature feature, int numReg) throws WriteException, UnsupportedEncodingException{
|
|
173 | 200 |
IFeatureType featureType=feature.getType(); |
174 | 201 |
if (!(channel instanceof FileChannel)) { |
175 |
throw new IOException("DbaseFileWriterNIO: channel is not a FileChannel. Cannot position properly");
|
|
202 |
throw new WriteException("DBF Writer",new IOException("DbaseFileWriterNIO: channel is not a FileChannel. Cannot position properly"));
|
|
176 | 203 |
} |
177 | 204 |
|
178 | 205 |
if (featureType.size() != header.getNumFields()) { |
179 |
throw new IOException("Wrong number of fields " + featureType.size() + |
|
180 |
" expected " + header.getNumFields()); |
|
206 |
throw new WriteException("DBF Writer",new IOException("Wrong number of fields " + featureType.size() +
|
|
207 |
" expected " + header.getNumFields()));
|
|
181 | 208 |
} |
182 | 209 |
|
183 | 210 |
FileChannel fileChannel = (FileChannel) channel; |
... | ... | |
196 | 223 |
|
197 | 224 |
} |
198 | 225 |
|
199 |
fileChannel.write(buffer, newPos); |
|
226 |
try { |
|
227 |
fileChannel.write(buffer, newPos); |
|
228 |
} catch (IOException e) { |
|
229 |
throw new WriteException("DBF Writer",e); |
|
230 |
} |
|
200 | 231 |
} |
201 | 232 |
|
202 | 233 |
private String fieldString(String type,IFeature feature,int i){ |
... | ... | |
307 | 338 |
|
308 | 339 |
/** Release resources associated with this writer. |
309 | 340 |
* <B>Highly recommended</B> |
341 |
* @throws CloseException |
|
310 | 342 |
* @throws IOException If errors occur. |
311 | 343 |
*/ |
312 |
public void close() throws IOException {
|
|
344 |
public void close() throws CloseException {
|
|
313 | 345 |
// IANS - GEOT 193, bogus 0x00 written. According to dbf spec, optional |
314 | 346 |
// eof 0x1a marker is, well, optional. Since the original code wrote a |
315 | 347 |
// 0x00 (which is wrong anyway) lets just do away with this :) |
... | ... | |
318 | 350 |
// buffer.position(0); |
319 | 351 |
// buffer.put((byte) 0).position(0).limit(1); |
320 | 352 |
// write(); |
321 |
channel.close(); |
|
353 |
try { |
|
354 |
channel.close(); |
|
355 |
} catch (IOException e) { |
|
356 |
throw new CloseException("DBF Writer",e); |
|
357 |
} |
|
322 | 358 |
if (buffer instanceof MappedByteBuffer) { |
323 | 359 |
// NIOUtilities.clean(buffer); |
324 | 360 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/exception/FileNotFoundDriverException.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.exception; |
|
2 |
|
|
3 |
import org.gvsig.data.exception.OpenException; |
|
4 |
|
|
5 |
|
|
6 |
public class FileNotFoundDriverException extends OpenException { |
|
7 |
private String file=""; |
|
8 |
public FileNotFoundDriverException(String l,Throwable exception,String file) { |
|
9 |
super(l,exception); |
|
10 |
this.file=file; |
|
11 |
init(); |
|
12 |
} |
|
13 |
public FileNotFoundDriverException(String l,Throwable exception) { |
|
14 |
super(l,exception); |
|
15 |
init(); |
|
16 |
} |
|
17 |
/** |
|
18 |
* |
|
19 |
*/ |
|
20 |
private void init() { |
|
21 |
messageKey = "error_file_not_find_driver"; |
|
22 |
formatString = "Can?t find file: "+file+"\n in a driver: %(name) "; |
|
23 |
} |
|
24 |
|
|
25 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/exception/UnsupportedEncodingDriverException.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.exception; |
|
2 |
|
|
3 |
import org.gvsig.data.exception.ReadException; |
|
4 |
|
|
5 |
public class UnsupportedEncodingDriverException extends ReadException { |
|
6 |
|
|
7 |
public UnsupportedEncodingDriverException(String driver,Throwable exception) { |
|
8 |
super(driver,exception); |
|
9 |
init(); |
|
10 |
} |
|
11 |
|
|
12 |
/** |
|
13 |
* |
|
14 |
*/ |
|
15 |
private void init() { |
|
16 |
messageKey = "Unsupported encoding driver"; |
|
17 |
formatString = "Unsupported encoding of driver: %(name) "; |
|
18 |
} |
|
19 |
} |
trunk/libraries/libDataSourceBaseDrivers/src/org/gvsig/data/datastores/vectorial/driver/exception/BadFieldDriverException.java | ||
---|---|---|
1 |
package org.gvsig.data.datastores.vectorial.driver.exception; |
|
2 |
|
|
3 |
import org.gvsig.data.exception.OpenException; |
|
4 |
|
|
5 |
|
|
6 |
public class BadFieldDriverException extends OpenException { |
|
7 |
private String description=""; |
|
8 |
public BadFieldDriverException(String l,Throwable exception) { |
|
9 |
super(l,exception); |
|
10 |
init(); |
|
11 |
} |
|
12 |
public BadFieldDriverException(String name, Throwable exception, String description) { |
|
13 |
super(name,exception); |
|
14 |
this.description=description; |
|
15 |
init(); |
|
16 |
} |
|
17 |
/** |
|
18 |
* |
|
19 |
*/ |
|
20 |
private void init() { |
|
21 |
messageKey = "error_field_layer"; |
|
22 |
formatString = "Can?t load field of the driver: %(name) "+"\n"+"incorrect type: "+description; |
|
23 |
} |
|
24 |
|
|
25 |
} |
Also available in: Unified diff