1
2
3
4 package org.hardcode.juf;
5
6 import org.hardcode.juf.status.Status;
7 import org.hardcode.juf.status.UpdateInfo;
8
9 import java.io.DataInputStream;
10 import java.io.DataOutputStream;
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.IOException;
15 import java.io.InputStream;
16
17 import java.net.URL;
18 import java.net.URLConnection;
19
20 import java.nio.MappedByteBuffer;
21 import java.nio.channels.FileChannel;
22
23
24 /***
25 * DOCUMENT ME!
26 *
27 * @author Fernando González Cortés
28 */
29 public class JUpdateUtilities extends ListenerSupportImpl {
30 /***
31 * DOCUMENT ME!
32 *
33 * @param url
34 * @param temp
35 *
36 * @throws IOException
37 * @throws NoServerFileException
38 */
39 public void download(URL url, File temp)
40 throws IOException, NoServerFileException {
41 URLConnection conn = null;
42 InputStream is = null;
43
44 try {
45 conn = url.openConnection();
46 is = url.openStream();
47 } catch (IOException e) {
48 throw new NoServerFileException(e.getMessage());
49 }
50
51 DataInputStream dis = new DataInputStream(is);
52 int size = conn.getContentLength();
53
54 DataOutputStream dos;
55 dos = new DataOutputStream(new FileOutputStream(temp));
56
57 if (size != 0) {
58 byte[] buffer = new byte[10240];
59 int n;
60 int completed = 0;
61
62 while ((n = dis.read(buffer)) == 10240) {
63 completed += n;
64 dos.write(buffer);
65
66 notifyProgress((100 * completed) / size);
67 }
68
69 dos.write(buffer, 0, n);
70 } else {
71 byte[] buffer = new byte[10240];
72 int n;
73
74 while ((n = dis.read(buffer)) == 10240) {
75 dos.write(buffer);
76 }
77
78 dos.write(buffer, 0, n);
79 }
80
81 dis.close();
82 dos.close();
83 }
84
85 /***
86 * DOCUMENT ME!
87 *
88 * @param source DOCUMENT ME!
89 * @param dest DOCUMENT ME!
90 *
91 * @throws IOException DOCUMENT ME!
92 */
93 public void copy(File source, File dest) throws IOException {
94 FileChannel in = null;
95 FileChannel out = null;
96
97 try {
98 in = new FileInputStream(source).getChannel();
99 out = new FileOutputStream(dest).getChannel();
100
101 long size = in.size();
102 MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
103
104 out.write(buf);
105 } finally {
106 if (in != null) {
107 in.close();
108 }
109
110 if (out != null) {
111 out.close();
112 }
113 }
114 }
115
116 /***
117 * Obtiene el estado de la actualización del componente cuyo nombre se pasa
118 * como parámetro buscando en la información que se pasa como primer parámetro
119 *
120 * @param status Información de las actualizaciones de toda la aplicación
121 * @param componentName Nombre del componente cuya información se quiere obtener
122 *
123 * @return La información buscada o null si no información sobre un componente
124 * de nombre 'componentName' en el objeto UpdateInfo
125 */
126 public Status getComponentStatus(UpdateInfo status, String componentName) {
127 Status[] componentStatus = status.getStatus();
128 for (int i = 0; i < componentStatus.length; i++) {
129 if (componentStatus[i].getComponentName().equals(componentName)){
130 return componentStatus[i];
131 }
132 }
133
134 return null;
135 }
136 }