public byte[] getHash(File file) { InputStream is = null; Digest.reset(); try { int read = 0; is = new FileInputStream(file); while (is.available() > 0) { read = is.read(Mem, 0, Mem.length); Digest.update(Mem, 0, read); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } finally { try { is.close(); } catch (IOException e) { } } return Digest.digest(); }
SOAPMessage doGet(URL endPoint) throws SOAPException { boolean isFailure = false; URL url = null; HttpURLConnection httpConnection = null; int responseCode = 0; try { /// Is https GET allowed?? if (endPoint.getProtocol().equals("https")) initHttps(); // Process the URL JaxmURI uri = new JaxmURI(endPoint.toString()); String userInfo = uri.getUserinfo(); url = endPoint; if (dL > 0) d("uri: " + userInfo + " " + url + " " + uri); // TBD // Will deal with https later. if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) { log.severe("SAAJ0052.p2p.protocol.mustbe.http.or.https"); throw new IllegalArgumentException( "Protocol " + url.getProtocol() + " not supported in URL " + url); } httpConnection = (HttpURLConnection) createConnection(url); httpConnection.setRequestMethod("GET"); httpConnection.setDoOutput(true); httpConnection.setDoInput(true); httpConnection.setUseCaches(false); httpConnection.setFollowRedirects(true); httpConnection.connect(); try { responseCode = httpConnection.getResponseCode(); // let HTTP_INTERNAL_ERROR (500) through because it is used for SOAP faults if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { isFailure = true; } else if ((responseCode / 100) != 2) { log.log( Level.SEVERE, "SAAJ0008.p2p.bad.response", new String[] {httpConnection.getResponseMessage()}); throw new SOAPExceptionImpl( "Bad response: (" + responseCode + httpConnection.getResponseMessage()); } } catch (IOException e) { // on JDK1.3.1_01, we end up here, but then getResponseCode() succeeds! responseCode = httpConnection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_INTERNAL_ERROR) { isFailure = true; } else { throw e; } } } catch (SOAPException ex) { throw ex; } catch (Exception ex) { log.severe("SAAJ0012.p2p.get.failed"); throw new SOAPExceptionImpl("Get failed", ex); } SOAPMessage response = null; if (responseCode == HttpURLConnection.HTTP_OK || isFailure) { try { MimeHeaders headers = new MimeHeaders(); String key, value; // Header field 0 is the status line so we skip it. int i = 1; while (true) { key = httpConnection.getHeaderFieldKey(i); value = httpConnection.getHeaderField(i); if (key == null && value == null) break; if (key != null) { StringTokenizer values = new StringTokenizer(value, ","); while (values.hasMoreTokens()) headers.addHeader(key, values.nextToken().trim()); } i++; } InputStream httpIn = (isFailure ? httpConnection.getErrorStream() : httpConnection.getInputStream()); // If no reply message is returned, // content-Length header field value is expected to be zero. // java SE 6 documentation says : // available() : an estimate of the number of bytes that can be read // (or skipped over) from this input stream without blocking // or 0 when it reaches the end of the input stream. if ((httpIn == null) || (httpConnection.getContentLength() == 0) || (httpIn.available() == 0)) { response = null; log.warning("SAAJ0014.p2p.content.zero"); } else { response = messageFactory.createMessage(headers, httpIn); } httpIn.close(); httpConnection.disconnect(); } catch (SOAPException ex) { throw ex; } catch (Exception ex) { log.log(Level.SEVERE, "SAAJ0010.p2p.cannot.read.resp", ex); throw new SOAPExceptionImpl("Unable to read response: " + ex.getMessage()); } } return response; }
public int available() throws IOException { return is.available(); }