Guest User

Untitled

a guest
Oct 17th, 2013
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.50 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.net.Socket;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8.  
  9. public class HTTPResponse {
  10.    
  11.     public String responseLine;
  12.     public int status;
  13.     public String message;
  14.     public long time;
  15.     public int bodySize;
  16.    
  17.     public Map<String, String> header;
  18.    
  19.     public HTTPResponse(Socket connection) throws IOException, InvalidResponseException {
  20.         BufferedReader bis = new BufferedReader(
  21.                 new InputStreamReader(connection.getInputStream()));
  22.         header = new HashMap<String, String>();
  23.        
  24.         responseLine = bis.readLine();
  25.         time = System.currentTimeMillis();
  26.         if(responseLine == null)
  27.             throw new InvalidResponseException();
  28.        
  29.         System.err.println(responseLine);
  30.         String[] a = responseLine.split("\\s");
  31.         if (a.length < 3 || !a[0].equals("HTTP/1.0"))
  32.             throw new InvalidResponseException();
  33.        
  34.         status = Integer.parseInt(a[1]);
  35.         message = a[2];
  36.        
  37.         String r;
  38.         while(!(r = bis.readLine()).equals("")) {
  39.             a = r.split(":", 2);
  40.             if(a.length < 2)
  41.                 continue;
  42.             header.put(a[0].toLowerCase(), a[1].trim());
  43.         }
  44.         String sz;
  45.         if((sz = header.get("content-length")) != null) {
  46.             bodySize = Integer.parseInt(sz);
  47.             byte[] buff = new byte[bodySize];
  48.             int k = 0, read = 0;
  49.             while((k = connection.getInputStream().read(buff, read, bodySize - read)) != -1 && read < bodySize)
  50.                 read += k;
  51.             assert read == bodySize : "Not all of file read. " + read + " " + bodySize;
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment