33
33
import javax .imageio .ImageIO ;
34
34
import java .awt .image .BufferedImage ;
35
35
import java .io .ByteArrayInputStream ;
36
+ import java .io .DataInputStream ;
37
+ import java .io .EOFException ;
36
38
import java .io .IOException ;
37
- import java .io .InputStream ;
38
39
import java .net .MalformedURLException ;
39
40
import java .net .URL ;
40
41
import java .net .URLConnection ;
41
- import java .util .List ;
42
- import java .util .Map ;
42
+ import java .util .concurrent .TimeUnit ;
43
43
44
44
import static org .bytedeco .javacpp .opencv_core .*;
45
45
import static org .bytedeco .javacpp .opencv_imgcodecs .cvDecodeImage ;
@@ -69,37 +69,31 @@ public static void tryLoad() throws Exception {
69
69
}
70
70
}
71
71
72
- private URL url ;
72
+ private final URL url ;
73
+ private final int connectionTimeout ;
74
+ private final int readTimeout ;
73
75
74
76
private URLConnection connection ;
75
- private InputStream input ;
77
+ private DataInputStream input ;
76
78
private byte [] pixelBuffer = new byte [1024 ];
77
- private Map <String , List <String >> headerfields ;
78
- private String boundryKey ;
79
79
private IplImage decoded = null ;
80
80
private FrameConverter <IplImage > converter = new OpenCVFrameConverter .ToIplImage ();
81
81
82
- public IPCameraFrameGrabber (String urlstr ) throws MalformedURLException {
82
+ public IPCameraFrameGrabber (String urlstr , int connectionTimeout , int readTimeout , TimeUnit unit ) throws MalformedURLException {
83
+ super ();
83
84
url = new URL (urlstr );
85
+ this .connectionTimeout = Math .toIntExact (TimeUnit .MILLISECONDS .convert (connectionTimeout , unit ));
86
+ this .readTimeout = Math .toIntExact (TimeUnit .MILLISECONDS .convert (readTimeout , unit ));
84
87
}
85
88
86
89
@ Override
87
90
public void start () throws Exception {
88
91
89
92
try {
90
93
connection = url .openConnection ();
91
- headerfields = connection .getHeaderFields ();
92
- if (headerfields .containsKey ("Content-Type" )) {
93
- List <String > ct = headerfields .get ("Content-Type" );
94
- for (int i = 0 ; i < ct .size (); ++i ) {
95
- String key = ct .get (i );
96
- int j = key .indexOf ("boundary=" );
97
- if (j != -1 ) {
98
- boundryKey = key .substring (j + 9 ); // FIXME << fragile
99
- }
100
- }
101
- }
102
- input = connection .getInputStream ();
94
+ connection .setConnectTimeout (connectionTimeout );
95
+ connection .setReadTimeout (readTimeout );
96
+ input = new DataInputStream (connection .getInputStream ());
103
97
} catch (IOException e ) {
104
98
// Make sure we rethrow the IO exception https://github.com/bytedeco/javacv/pull/300
105
99
throw new Exception (e .getMessage (), e );
@@ -169,9 +163,9 @@ byte[] readImage() throws IOException {
169
163
}
170
164
}
171
165
// find embedded jpeg in stream
172
- String subheader = sb .toString ();
166
+ final String subheader = sb .toString ();
173
167
//log.debug(subheader);
174
- int contentLength = - 1 ;
168
+
175
169
// if (boundryKey == null)
176
170
// {
177
171
// Yay! - server was nice and sent content length
@@ -180,18 +174,17 @@ byte[] readImage() throws IOException {
180
174
181
175
if (c0 < 0 ) {
182
176
//log.info("no content length returning null");
183
- return null ;
177
+ throw new EOFException ( "The camera stream ended unexpectedly" ) ;
184
178
}
185
179
186
180
c0 += 16 ;
187
- contentLength = Integer .parseInt (subheader .substring (c0 , c1 ).trim ());
181
+ final int contentLength = Integer .parseInt (subheader .substring (c0 , c1 ).trim ());
188
182
//log.debug("Content-Length: " + contentLength);
189
183
190
184
// adaptive size - careful - don't want a 2G jpeg
191
185
ensureBufferCapacity (contentLength );
192
186
193
- while (input .available () < contentLength ) ;
194
- input .read (pixelBuffer , 0 , contentLength );
187
+ input .readFully (pixelBuffer , 0 , contentLength );
195
188
input .read ();// \r
196
189
input .read ();// \n
197
190
input .read ();// \r
0 commit comments