/[projects]/dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/util/HttpUtil.java
ViewVC logotype

Diff of /dao/DaoAdresseVedligehold/src/main/java/dk/daoas/adressevedligehold/util/HttpUtil.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 3005 by torben, Mon Feb 1 08:32:44 2016 UTC revision 3006 by torben, Tue Apr 19 15:49:14 2016 UTC
# Line 3  package dk.daoas.adressevedligehold.util Line 3  package dk.daoas.adressevedligehold.util
3  // kopieret fra dk.thoerup.genericjavautils;  // kopieret fra dk.thoerup.genericjavautils;
4    
5  import java.io.ByteArrayOutputStream;  import java.io.ByteArrayOutputStream;
6    import java.io.File;
7    import java.io.FileOutputStream;
8  import java.io.IOException;  import java.io.IOException;
9  import java.io.InputStream;  import java.io.InputStream;
10  import java.io.OutputStream;  import java.io.OutputStream;
# Line 121  public class HttpUtil { Line 123  public class HttpUtil {
123                  return readInputStream(is);                  return readInputStream(is);
124          }          }
125                    
126            public static void getContentToFile(String uri, File f, int timeout) throws IOException {
127                    URL url = new URL(uri);
128                    URLConnection connection = url.openConnection();
129                    
130                    connection.setConnectTimeout(timeout);
131                    connection.setReadTimeout(timeout);
132                    InputStream is = connection.getInputStream();
133                    
134                    saveInputStreamToFile(is,f);
135            }
136            
137          public static byte[] postContent(String uri, String data,  int timeout) throws IOException {          public static byte[] postContent(String uri, String data,  int timeout) throws IOException {
138                  URL url = new URL(uri);                  URL url = new URL(uri);
139                  HttpURLConnection connection = (HttpURLConnection) url.openConnection();                  HttpURLConnection connection = (HttpURLConnection) url.openConnection();
# Line 138  public class HttpUtil { Line 151  public class HttpUtil {
151          }          }
152                    
153          static byte[] readInputStream(InputStream is) throws IOException{          static byte[] readInputStream(InputStream is) throws IOException{
154                  byte buffer[] = new byte[1024];                  byte buffer[] = new byte[64 * 1024];
155                  //try /; //start buffer size - instead of default 32bytes                  //try /; //start buffer size - instead of default 32bytes
156                  try (ByteArrayOutputStream baos = new ByteArrayOutputStream(32768)) {                  try (ByteArrayOutputStream baos = new ByteArrayOutputStream(16*1024*1024)) {
157                          int count;                          int count;
158                                                    
159                          while ( (count = is.read(buffer)) != -1) {                          while ( (count = is.read(buffer)) != -1) {
# Line 151  public class HttpUtil { Line 164  public class HttpUtil {
164                  }                        }      
165          }          }
166                    
167            static void saveInputStreamToFile(InputStream is,File fOut) throws IOException{
168                    byte buffer[] = new byte[64 * 1024];
169                    
170                    //try /; //start buffer size - instead of default 32bytes
171                    try (FileOutputStream fos = new FileOutputStream(fOut) ) {
172                            int count;
173                            
174                            while ( (count = is.read(buffer)) != -1) {
175                                    
176                                    fos.write(buffer, 0, count);
177                            }
178                            is.close();
179                    }
180                    
181            }
182            
183  }  }
184    

Legend:
Removed from v.3005  
changed lines
  Added in v.3006

  ViewVC Help
Powered by ViewVC 1.1.20