Revision 37920

View differences:

branches/v2_0_0_prep/libraries/libRemoteServices/src/org/gvsig/remoteclient/wmts/request/WMTSRequest.java
114 114
	}
115 115
	
116 116
	public void downloadFile(URL url, File dstFile, ICancellable cancel) throws IOException {
117
		if(cancel.isCanceled())
118
			throw new IOException();
117 119
		Preferences prefs = Preferences.userRoot().node( "gvsig.downloader" );
118 120
		// by default 1 minute (60000 milliseconds.
119
		int timeout = prefs.getInt("timeout", 60000);
121
		int timeout = prefs.getInt("timeout", 15000);
120 122

  
121
		DataOutputStream dos;
122
		DataInputStream is;
123
		OutputStreamWriter os = null;
124
		HttpURLConnection connection = null;
125 123
		//If the used protocol is HTTPS
126 124
		if (url.getProtocol().equals("https")) {
127 125
			try {
128 126
				disableHttsValidation();
129 127
			} catch (KeyManagementException e) {
130
				e.printStackTrace();
128
				throw new IOException();
131 129
			} catch (NoSuchAlgorithmException e) {
132
				e.printStackTrace();
130
				throw new IOException();
133 131
			}
134 132
		}
135
		connection = (HttpURLConnection)url.openConnection();
133
	
134
		HttpURLConnection connection = (HttpURLConnection)url.openConnection();
136 135
		connection.setConnectTimeout(timeout);
137
		is = new DataInputStream(url.openStream());
136
		DataInputStream is = new DataInputStream(connection.getInputStream());
137
		cancel.equals(is);
138 138

  
139
		dos = new DataOutputStream( new BufferedOutputStream(new FileOutputStream(dstFile)));
139
		DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dstFile)));
140 140
		byte[] buffer = new byte[1024 * 4];
141 141

  
142

  
143 142
		long readed = 0;
144
		for (int i = is.read(buffer); i > 0; i = is.read(buffer)){
143
		int i = -1;
144
		
145
		if(cancel != null && cancel.isCanceled()) {
146
			throw new IOException();
147
		}
148
		
149
		while((i = is.read(buffer)) > 0) {
150
			if(cancel != null && cancel.isCanceled()) {
151
				throw new IOException();
152
			}
145 153
			dos.write(buffer, 0, i);
146 154
			readed += i;
147
			if(cancel != null && cancel.isCanceled())
148
				return;
149 155
		}
150
		if(os != null) {
151
			os.close();
152
		}
156
	
157
		is.close();
153 158
		dos.close();
154
		is.close();
155
		is = null;
156
		dos = null;
159
		connection.disconnect();
157 160
	}
158 161

  
159 162
	/**
branches/v2_0_0_prep/libraries/libRemoteServices/src/org/gvsig/remoteclient/wmts/WMTSProtocolHandler.java
145 145
				checkFileError(file);
146 146
			} catch(WMTSException e) {
147 147
				file.delete();
148
				//We can throw an exception if a tile is wrong because the program crashes
149
				System.err.println("This tile is not available:" + file.getAbsoluteFile().toString());
148
				throw new WMTSException("This tile is not available:" + file.getAbsoluteFile().toString(), e);
150 149
			} catch(IOException e) {
151 150
				file.delete();
152
				//We can throw an exception if a tile is wrong because the program crashes
153
				System.err.println("This tile is not available:" + file.getAbsoluteFile().toString());
151
				throw new WMTSException("This tile is not available:" + file.getAbsoluteFile().toString(), e);
154 152
			}
155 153
			return file;
156 154
		} catch(IOException e) {
157
			e.printStackTrace();
158
            throw new ServerErrorException();
155
            throw new WMTSException(e);
159 156
		}
160 157
    }
161 158
    

Also available in: Unified diff