sombriks

MjpgUtil.java

Apr 7th, 2014
670
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.22 KB | None | 0 0
  1. import java.io.ByteArrayOutputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6.  
  7. public class MjpgUtil {
  8.  
  9.     public static int trataHeader(InputStream in) throws IOException {
  10.         // --myboundary\r\n
  11.         // Content-Type: image/jpeg\r\n
  12.         // Content-Length: 37459\r\n\r\n
  13.         byte buf1[] = new byte[1];
  14.         int four = 0;
  15.         List<Byte> bytes = new LinkedList<Byte>();
  16.         while (four < 4) {
  17.             in.read(buf1);
  18.             if (buf1[0] == 13 || buf1[0] == 10) {
  19.                 four++;
  20.             } else {
  21.                 four = 0;
  22.                 bytes.add(buf1[0]);
  23.             }
  24.         }
  25.         byte bts[] = new byte[bytes.size()];
  26.         int i = bts.length;
  27.         while (i-- > 0) {
  28.             bts[i] = bytes.get(i);
  29.         }
  30.         String header = new String(bts);
  31.         header = header.replaceFirst(".*Content-Length: (\\d+).*", "$1");
  32. //      System.out.println(header);
  33.         return Integer.parseInt(header);
  34.     }
  35.  
  36.     public static byte[] trataImagem(int len, InputStream in) throws Exception {
  37.         try (ByteArrayOutputStream out = new ByteArrayOutputStream(len)) {
  38.             byte buf[] = new byte[len];
  39.             int i = -1;
  40.             int tot = 0;
  41.             while (tot < len && (i = in.read(buf)) > -1) {
  42.                 tot += i;
  43.                 out.write(buf, 0, i);
  44.             }
  45.             return out.toByteArray();
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment