Page 344 - 완) I MDP 프로젝트 작품 보고서(전체과 1학년)1.6
P. 344

int currByte = -1;
                        String header = null;
                        while ((currByte = urlStream.read()) > -1 && !haveHeader) {
                            stringWriter.write(currByte);
                            String tempString = stringWriter.toString();
                            int indexOf = tempString.indexOf(CONTENT_LENGTH);
                            if (indexOf > -1 && currByte == '\n') {
                                haveHeader = true;
                                header = tempString;
                            }
                        }
                        while ((currByte=urlStream.read()) != 255) {
                        }
                        int contentLength = contentLength(header);
                        byte[] imageBytes = new byte[contentLength + 1];
                        imageBytes[0] = (byte) 255;
                        int offset = 1;
                        int numRead = 0;
                        while (offset < imageBytes.length
                                && (numRead = urlStream.read(imageBytes, offset, imageBytes.length - offset
            )) >= 0) {
                            for (byte b : imageBytes)
                            offset += numRead;
                        }

                        stringWriter = new StringWriter(128);
                        return imageBytes;
                    }
                    @Override
                    public void run() {
                        while (processing && Wtrigger) {
                            try {
                                byte[] imageBytes = retrieveNextImage();
                                ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
                                BufferedImage image = ImageIO.read(bais);
                                Image im = image.getScaledInstance(1280, 720, Image.SCALE_SMOOTH);
                                jLabel1.setIcon(new ImageIcon(im));
                            } catch (SocketTimeoutException e) {
                                e.printStackTrace();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        try {
                            urlStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    private int contentLength(String header) {




                                                         -  337  -
   339   340   341   342   343   344   345   346   347   348   349