-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathFileConnection.java
More file actions
98 lines (90 loc) · 3.98 KB
/
FileConnection.java
File metadata and controls
98 lines (90 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package HTTPClient;
import java.net.*;
import java.io.*;
/**
* Esta clase hace un proxy sobre los pedidos de HTTP y lee directamente sobre
* la estructura de archivos.
* El constructor toma como parametros una URL con el protocolo FILE, y almacena el
* disco source (que puede ser un Shared Network), o sea, los pedidos luego se deben
* hacer con la ruta completa.
* NOTA: Las URL son de este estilo:
* FILE://\\networked\dir1\dir2
* FILE://e:\dir1\dir2
* NOTA2: Tambien se permite que se comienze con FILE:///, o sea, con 3 barras
* en vez de 2, porque los browsers lo hacen as�.
* Ejemplo: URL= FILE:///e:\dir1\dir2
*
*/
public class FileConnection extends HTTPConnection
{
private String completeFilesDir; // Ruta completa (drive + dir) del 'server' --> Si el fileServer NO es un network Drive, el filesDir solo contiene el nombre del drive (pero NO el path)
private String filesDir;
private static char alternateSeparator = File.separatorChar == '\\' ? '/' : '\\';
/** Ver la descripci�n de la clase para ver el formato de la URL
*/
public FileConnection(URL dirUrl) throws IOException
{
super("FileConnection");
if(!dirUrl.getProtocol().equalsIgnoreCase("file"))throw new IOException("FileConnection needs 'FILE' protocol");
File tempFile;
if(dirUrl.getHost().startsWith("\\\\"))
{ // Si se trata de un Shared Network
completeFilesDir = dirUrl.getHost() + dirUrl.getFile().replace(alternateSeparator, File.separatorChar);
filesDir = dirUrl.getHost() + File.separatorChar;
// En el Shared Network no se chequea que exista, porque en s� mismo NO existe, s�lo existe con path inclu�do
}
else if(dirUrl.getFile().replace('/', '\\').startsWith("\\\\"))
{ // En algunos casos el URL queda todo en el File (depende de la VM), asi que chequeo desde aca
completeFilesDir = dirUrl.getFile().replace(alternateSeparator, File.separatorChar);
// filesDir = "\\\\" + (completeFilesDir + "\\").substring(2, (completeFilesDir.substring(2) + "\\").indexOf('\\') + 3);
filesDir = completeFilesDir + File.separatorChar;
}
else
{ // En el caso en que no sea un Shared Network Dir
if(!(tempFile = new File(dirUrl.getHost() + ":" + dirUrl.getFile())).isDirectory())throw new IOException( dirUrl.getFile() + " is NOT a directory");
else completeFilesDir = tempFile.getAbsolutePath().replace(alternateSeparator, File.separatorChar);
filesDir = dirUrl.getHost() + ":" + File.separator; // Lo que guardo aca es el Drive del host
}
}
/** En File viene la ruta completa al archivo (sin inclu�r el drive host)
* @param file ruta completa al archivo (sin inclu�r el drive host)
*
*/
public HTTPResponse Get(String file, String query, NVPair[] headers) throws IOException, ModuleException
{
if(file.startsWith("/"))file = file.substring(1);
return new FileResponse(filesDir + file);
}
}
class FileResponse extends HTTPResponse
{
private FileInputStream inp = null;
private long size;
private String file;
private byte Data [] = null;
public FileResponse(String file) throws IOException
{
//super(new HTTPClientModule[0], 0, null);
this.file = file.replace('/', '\\');
if(!new File(this.file).canRead())throw new IOException(file + " not found");
size = new File(this.file).length();
}
public InputStream getInputStream()throws IOException
{
if(inp == null) inp = new FileInputStream(file);
return inp;
}
public synchronized byte [] getData() throws IOException
{
if(Data != null)return Data;
Data = new byte[(int)size];
if(inp == null)
getInputStream().read(Data);
getInputStream().close();
return Data;
}
synchronized boolean handleResponse() throws IOException, ModuleException
{
return false;
}
}