Revision 4436 org.gvsig.raster/trunk/org.gvsig.raster/org.gvsig.raster.lib/org.gvsig.raster.lib.impl/src/main/java/org/gvsig/raster/util/DefaultFileUtils.java
DefaultFileUtils.java | ||
---|---|---|
33 | 33 |
import java.io.IOException; |
34 | 34 |
import java.io.InputStream; |
35 | 35 |
import java.io.OutputStream; |
36 |
import java.net.URI; |
|
36 | 37 |
import java.security.MessageDigest; |
37 | 38 |
import java.security.NoSuchAlgorithmException; |
38 | 39 |
import java.text.NumberFormat; |
39 | 40 |
import java.util.ArrayList; |
40 | 41 |
|
42 |
import org.apache.commons.lang3.StringUtils; |
|
43 |
|
|
41 | 44 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
42 | 45 |
import org.gvsig.fmap.dal.coverage.util.FileUtils; |
43 | 46 |
import org.gvsig.fmap.dal.coverage.util.PropertyEvent; |
... | ... | |
45 | 48 |
|
46 | 49 |
|
47 | 50 |
/** |
48 |
* Utilities for files, directories and file names
|
|
51 |
* Utilities for files, directories and file names |
|
49 | 52 |
* |
50 | 53 |
* @author Nacho Brodin (nachobrodin@gmail.com) |
51 | 54 |
*/ |
52 | 55 |
public class DefaultFileUtils implements FileUtils { |
53 |
/** |
|
54 |
* Copia de ficheros |
|
55 |
* @param pathOrig Ruta de origen |
|
56 |
* @param pathDst Ruta de destino. |
|
57 |
*/ |
|
56 |
|
|
58 | 57 |
public void copyFile(String pathOrig, String pathDst) throws FileNotFoundException, IOException { |
59 | 58 |
InputStream in; |
60 | 59 |
OutputStream out; |
... | ... | |
74 | 73 |
String file = new File(pathOrig).getName(); |
75 | 74 |
if (dest.isDirectory()) |
76 | 75 |
pathDst += file; |
77 |
|
|
76 |
|
|
78 | 77 |
dest = new File(pathDst); |
79 | 78 |
if(!dest.exists()) |
80 | 79 |
dest.createNewFile(); |
... | ... | |
92 | 91 |
out.close(); |
93 | 92 |
} |
94 | 93 |
|
95 |
/** |
|
96 |
* Crea un fichero de georeferenciaci?n (world file) para un dataset |
|
97 |
* determinado |
|
98 |
* @param fileName Nombre completo del fichero de raster |
|
99 |
* @param Extent |
|
100 |
* @param pxWidth Ancho en p?xeles |
|
101 |
* @param pxHeight Alto en p?xeles |
|
102 |
* @return |
|
103 |
* @throws IOException |
|
104 |
*/ |
|
105 | 94 |
public void createWorldFile(String fileName, Extent ext, int pxWidth, int pxHeight) throws IOException { |
106 | 95 |
File tfw = null; |
107 | 96 |
|
... | ... | |
124 | 113 |
dos.close(); |
125 | 114 |
} |
126 | 115 |
|
127 |
/** |
|
128 |
* Crea un fichero de georeferenciaci?n (world file) para un dataset |
|
129 |
* determinado |
|
130 |
* @param fileName Nombre completo del fichero de raster |
|
131 |
* @param AffineTransform |
|
132 |
* @param pxWidth Ancho en p?xeles |
|
133 |
* @param pxHeight Alto en p?xeles |
|
134 |
* @return |
|
135 |
* @throws IOException |
|
136 |
*/ |
|
137 | 116 |
public void createWorldFile(String fileName, AffineTransform at, int pxWidth, int pxHeight) throws IOException { |
138 | 117 |
File tfw = null; |
139 | 118 |
|
... | ... | |
156 | 135 |
dos.close(); |
157 | 136 |
} |
158 | 137 |
|
159 |
/** |
|
160 |
* Formatea en forma de cadena un tama?o dado en bytes. El resultado ser? una |
|
161 |
* cadena con GB, MB, KB y B |
|
162 |
* @param size tama?o a formatear |
|
163 |
* @return cadena con la cantidad formateada |
|
164 |
*/ |
|
165 | 138 |
public String formatFileSize(long size) { |
166 | 139 |
double bytes = size; |
167 | 140 |
double kBytes = 0.0; |
... | ... | |
208 | 181 |
return texto + " (" + numberFormat.format(bytes) + " bytes)"; |
209 | 182 |
} |
210 | 183 |
|
211 |
/** |
|
212 |
* Obtiene la extensi?n del fichero a partir de su nombre. |
|
213 |
* @param file Nombre o ruta del fichero |
|
214 |
* @return Cadena con la extensi?n que representa el tipo de fichero. Devuelve |
|
215 |
* null si no tiene extension. |
|
216 |
*/ |
|
217 | 184 |
public String getExtensionFromFileName(String file) { |
218 | 185 |
return file.substring(file.lastIndexOf(".") + 1).toLowerCase(); |
219 | 186 |
} |
220 | 187 |
|
221 |
/** |
|
222 |
* Obtiene el nombre de fichero sin la extensi?n. |
|
223 |
* @param file Nombre o ruta del fichero |
|
224 |
* @return Cadena con la extensi?n que representa el tipo de fichero. Si no |
|
225 |
* tiene extensi?n devuelve el mismo fichero de entrada |
|
226 |
*/ |
|
227 | 188 |
public String getNameWithoutExtension(String file) { |
228 | 189 |
if (file == null) |
229 | 190 |
return null; |
... | ... | |
234 | 195 |
return file; |
235 | 196 |
} |
236 | 197 |
|
237 |
/** |
|
238 |
* Obtiene el nombre de fichero sin la extensi?n ni la ruta. |
|
239 |
* @param file Ruta del fichero |
|
240 |
* @return Cadena que representa el nombre del fichero sin extensi?n ni path de directorios |
|
241 |
*/ |
|
242 | 198 |
public String getFileNameFromCanonical(String file) { |
243 | 199 |
if (file == null) |
244 | 200 |
return null; |
... | ... | |
254 | 210 |
return file; |
255 | 211 |
} |
256 | 212 |
|
257 |
/** |
|
258 |
* Obtiene el ?ltimo trozo de la cadena a partir de los caracteres que |
|
259 |
* coincidan con el patr?n. En caso de que el patr?n no exista en la cadena |
|
260 |
* devuelve esta completa |
|
261 |
* @param string |
|
262 |
* @param pattern |
|
263 |
* @return |
|
264 |
*/ |
|
265 | 213 |
public String getLastPart(String string, String pattern) { |
266 | 214 |
int n = string.lastIndexOf(pattern); |
267 | 215 |
if (n > 0) |
... | ... | |
269 | 217 |
return string; |
270 | 218 |
} |
271 | 219 |
|
272 |
/** |
|
273 |
* Obtiene la codificaci?n de un fichero XML |
|
274 |
* @param file Nombre del fichero XML |
|
275 |
* @return Codificaci?n |
|
276 |
*/ |
|
277 | 220 |
public String readFileEncoding(String file) { |
278 | 221 |
FileReader fr; |
279 | 222 |
String encoding = null; |
... | ... | |
298 | 241 |
return encoding; |
299 | 242 |
} |
300 | 243 |
|
301 |
/** |
|
302 |
* Obtiene el nombre del fichero RMF a partir del nombre del fichero. Si el |
|
303 |
* nombre del fichero tiene una extensi?n esta llamada sustituir? la extensi?n |
|
304 |
* existente por .rmf. Si el fichero pasado no tiene extensi?n esta llamada |
|
305 |
* a?adir? .rm al final. |
|
306 |
* @param fileName Nombre del fichero raster de origen |
|
307 |
* @return Nombre del fichero rmf asociado al raster. |
|
308 |
*/ |
|
309 | 244 |
public String getRMFNameFromFileName(String fileName) { |
310 | 245 |
return getNameWithoutExtension(fileName) + ".rmf"; |
311 | 246 |
} |
312 |
|
|
247 |
|
|
313 | 248 |
/** |
314 | 249 |
* Recursive directory delete. |
315 | 250 |
* @param f |
... | ... | |
322 | 257 |
files[i].delete(); |
323 | 258 |
} |
324 | 259 |
} |
325 |
|
|
260 |
|
|
326 | 261 |
//******* Servicio de directorios temporales ************** |
327 |
|
|
262 |
|
|
328 | 263 |
/** |
329 | 264 |
* Directorio temporal para la cach?. Si gastamos el mismo que andami este se ocupar? de gestionar su |
330 | 265 |
* destrucci?n al cerrar gvSIG. |
331 | 266 |
*/ |
332 | 267 |
private String tempCacheDirectoryPath = System.getProperty("java.io.tmpdir") |
333 | 268 |
+ File.separator + "tmp-andami"; |
334 |
|
|
335 |
/** |
|
336 |
* Elimina los ficheros del directorio temporal. Realizamos esta acci?n al |
|
337 |
* levantar la librer?a. |
|
338 |
*/ |
|
269 |
|
|
339 | 270 |
public void cleanUpTempFiles() { |
340 | 271 |
try { |
341 | 272 |
File tempDirectory = new File(tempCacheDirectoryPath); |
... | ... | |
353 | 284 |
} |
354 | 285 |
} |
355 | 286 |
|
356 |
/** |
|
357 |
* Esta funci?n crea el directorio para temporales y devuelve el manejador |
|
358 |
* del directorio |
|
359 |
* @return |
|
360 |
*/ |
|
361 | 287 |
public File getTemporalFile() { |
362 | 288 |
File tempDirectory = new File(tempCacheDirectoryPath); |
363 | 289 |
if (!tempDirectory.exists()) |
364 | 290 |
tempDirectory.mkdir(); |
365 | 291 |
return tempDirectory; |
366 | 292 |
} |
367 |
|
|
368 |
/** |
|
369 |
* Esta funci?n crea el directorio para temporales y devuelve la ruta de este |
|
370 |
* @return |
|
371 |
*/ |
|
293 |
|
|
372 | 294 |
public String getTemporalPath() { |
373 | 295 |
return getTemporalFile().getAbsolutePath(); |
374 | 296 |
} |
... | ... | |
378 | 300 |
* @see org.gvsig.fmap.dal.coverage.util.FileUtils#getFormatedRasterFileName(java.lang.String) |
379 | 301 |
*/ |
380 | 302 |
public String getFormatedRasterFileName(String name) { |
381 |
if(name.startsWith("PG:host=")) { |
|
382 |
String newName = ""; |
|
383 |
String schema = null; |
|
384 |
String table = null; |
|
385 |
int index = name.indexOf(" schema='") + 8; |
|
386 |
if(index != -1) { |
|
387 |
try { |
|
388 |
schema = name.substring(index + 1, name.indexOf("'", index + 1)); |
|
389 |
newName += schema + "."; |
|
390 |
} catch (StringIndexOutOfBoundsException e) { |
|
391 |
} |
|
392 |
} |
|
393 |
index = name.indexOf(" table='") + 7; |
|
394 |
if(index != -1) { |
|
395 |
try { |
|
396 |
table = name.substring(index + 1, name.indexOf("'", index + 1)); |
|
397 |
newName += table; |
|
398 |
} catch (StringIndexOutOfBoundsException e) { |
|
399 |
} |
|
400 |
} |
|
401 |
return newName; |
|
402 |
} |
|
403 |
return name; |
|
303 |
throw new UnsupportedOperationException("Deprecated (name = '"+name+"')"); |
|
304 |
|
|
305 |
// if(name.startsWith("PG:host=")) { |
|
306 |
// String newName = ""; |
|
307 |
// String schema = null; |
|
308 |
// String table = null; |
|
309 |
// int index = name.indexOf(" schema='") + 8; |
|
310 |
// if(index != -1) { |
|
311 |
// try { |
|
312 |
// schema = name.substring(index + 1, name.indexOf("'", index + 1)); |
|
313 |
// newName += schema + "."; |
|
314 |
// } catch (StringIndexOutOfBoundsException e) { |
|
315 |
// } |
|
316 |
// } |
|
317 |
// index = name.indexOf(" table='") + 7; |
|
318 |
// if(index != -1) { |
|
319 |
// try { |
|
320 |
// table = name.substring(index + 1, name.indexOf("'", index + 1)); |
|
321 |
// newName += table; |
|
322 |
// } catch (StringIndexOutOfBoundsException e) { |
|
323 |
// } |
|
324 |
// } |
|
325 |
// return newName; |
|
326 |
// } |
|
327 |
// return name; |
|
404 | 328 |
} |
405 |
|
|
329 |
|
|
406 | 330 |
public String convertPathToMD5(String path) throws NoSuchAlgorithmException { |
407 | 331 |
MessageDigest md = java.security.MessageDigest.getInstance("MD5"); |
408 | 332 |
md.update(path.getBytes()); |
409 | 333 |
return convertToHex(md.digest()); |
410 | 334 |
} |
411 |
|
|
335 |
|
|
412 | 336 |
/** |
413 | 337 |
* Converts an array of bytes to hexadecimal string |
414 | 338 |
* @param data |
415 | 339 |
* @return |
416 | 340 |
*/ |
417 |
private String convertToHex(byte[] data) {
|
|
341 |
private String convertToHex(byte[] data) { |
|
418 | 342 |
StringBuffer buf = new StringBuffer(); |
419 |
for (int i = 0; i < data.length; i++) {
|
|
343 |
for (int i = 0; i < data.length; i++) { |
|
420 | 344 |
int halfbyte = (data[i] >>> 4) & 0x0F; |
421 | 345 |
int two_halfs = 0; |
422 |
do {
|
|
423 |
if ((0 <= halfbyte) && (halfbyte <= 9))
|
|
346 |
do { |
|
347 |
if ((0 <= halfbyte) && (halfbyte <= 9)) |
|
424 | 348 |
buf.append((char) ('0' + halfbyte)); |
425 |
else
|
|
349 |
else |
|
426 | 350 |
buf.append((char) ('a' + (halfbyte - 10))); |
427 | 351 |
halfbyte = data[i] & 0x0F; |
428 | 352 |
} while(two_halfs++ < 1); |
429 |
}
|
|
353 |
} |
|
430 | 354 |
return buf.toString(); |
431 | 355 |
} |
432 | 356 |
|
433 | 357 |
//******* Servicio de nombres de capas ?nicos ************** |
434 | 358 |
/** |
435 | 359 |
* Contador global de las capas generadas para raster. Hay que contar con que esta |
436 |
* clase es un singleton desde el manager. Si hay varias instanciaciones layerCount
|
|
437 |
* dar? valores erroneos.
|
|
360 |
* clase es un singleton desde el manager. Si hay varias instanciaciones layerCount |
|
361 |
* dar? valores erroneos. |
|
438 | 362 |
*/ |
439 | 363 |
private int layerCount = 1; |
440 |
private ArrayList<PropertyListener>
|
|
364 |
private ArrayList<PropertyListener> |
|
441 | 365 |
propetiesListeners = new ArrayList<PropertyListener>(); |
442 | 366 |
|
443 |
/** |
|
444 |
* La gesti?n de nombres ?nicos en la generaci?n de capas se lleva de forma |
|
445 |
* autom?tica. Cuando alguien crea una capa nueva, si esta no tiene nombre especifico, |
|
446 |
* obtiene su nombre mediante este m?todo. La siguiente vez que se llame dar? un nombre |
|
447 |
* distinto. El nombre de la capa ser? NewLayer_ seguido de un contador de actualizaci?n |
|
448 |
* autom?tica cada vez que se usa. |
|
449 |
* @return Nombre ?nico para la capa. |
|
450 |
*/ |
|
451 | 367 |
public String usesOnlyLayerName() { |
452 | 368 |
String oldValue = getOnlyLayerName(); |
453 | 369 |
String newValue = "NewLayer_" + (++layerCount); |
... | ... | |
457 | 373 |
return newValue; |
458 | 374 |
} |
459 | 375 |
|
460 |
/** |
|
461 |
* Obtiene el nombre ?nico de la siguiente capa sin actualizar el contador. Es |
|
462 |
* solo para consulta. La siguiente vez que se llama a getOnlyLayerName o usesOnlyLayerName |
|
463 |
* devolver? el mismo nomnbre. |
|
464 |
* @return Nombre ?nico para la capa. |
|
465 |
*/ |
|
466 | 376 |
public String getOnlyLayerName() { |
467 | 377 |
return "NewLayer_" + layerCount; |
468 | 378 |
} |
469 | 379 |
|
470 |
/** |
|
471 |
* A?adir un listener a la lista de eventos |
|
472 |
* @param listener |
|
473 |
*/ |
|
474 | 380 |
public void addOnlyLayerNameListener(PropertyListener listener) { |
475 | 381 |
if (!propetiesListeners.contains(listener)) |
476 | 382 |
propetiesListeners.add(listener); |
477 | 383 |
} |
478 | 384 |
|
479 |
/** |
|
480 |
* Elimina un listener de la lista de eventos |
|
481 |
* @param listener |
|
482 |
*/ |
|
483 | 385 |
public void removeOnlyLayerNameListener(PropertyListener listener) { |
484 | 386 |
for (int i = 0; i < propetiesListeners.size(); i++) |
485 | 387 |
if(propetiesListeners.get(i) == listener) |
Also available in: Unified diff