Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.LinkedList;
- import java.util.List;
- public class MjpgUtil {
- public static int trataHeader(InputStream in) throws IOException {
- // --myboundary\r\n
- // Content-Type: image/jpeg\r\n
- // Content-Length: 37459\r\n\r\n
- byte buf1[] = new byte[1];
- int four = 0;
- List<Byte> bytes = new LinkedList<Byte>();
- while (four < 4) {
- in.read(buf1);
- if (buf1[0] == 13 || buf1[0] == 10) {
- four++;
- } else {
- four = 0;
- bytes.add(buf1[0]);
- }
- }
- byte bts[] = new byte[bytes.size()];
- int i = bts.length;
- while (i-- > 0) {
- bts[i] = bytes.get(i);
- }
- String header = new String(bts);
- header = header.replaceFirst(".*Content-Length: (\\d+).*", "$1");
- // System.out.println(header);
- return Integer.parseInt(header);
- }
- public static byte[] trataImagem(int len, InputStream in) throws Exception {
- try (ByteArrayOutputStream out = new ByteArrayOutputStream(len)) {
- byte buf[] = new byte[len];
- int i = -1;
- int tot = 0;
- while (tot < len && (i = in.read(buf)) > -1) {
- tot += i;
- out.write(buf, 0, i);
- }
- return out.toByteArray();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment