Skip to content

Commit a7b421e

Browse files
enedniqdev
andauthored
Only allocate frameData for content length, not more is needed. (#83)
* Only allocate frameData for content length, not more is needed. * revert formatting Co-authored-by: niqdev <[email protected]>
1 parent bc4e807 commit a7b421e

File tree

1 file changed

+5
-81
lines changed

1 file changed

+5
-81
lines changed

mjpeg-view/src/main/java/com/github/niqdev/mjpeg/MjpegInputStreamNative.java

Lines changed: 5 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.niqdev.mjpeg;
22

33
import android.graphics.Bitmap;
4-
import android.graphics.BitmapFactory;
54
import android.util.Log;
65

76
import java.io.BufferedInputStream;
@@ -65,7 +64,6 @@ private int getEndOfSeqeunce(DataInputStream in, byte[] sequence)
6564
} else seqIndex = 0;
6665
}
6766

68-
6967
return -1;
7068
}
7169

@@ -82,7 +80,6 @@ private int getEndOfSeqeunceSimplified(DataInputStream in, byte[] sequence)
8280

8381
skipBytes(headerLen + startPos);
8482

85-
8683
int seqIndex = 0;
8784
byte c;
8885
for (int i = 0; i < endPos - startPos; i++) {
@@ -96,86 +93,17 @@ private int getEndOfSeqeunceSimplified(DataInputStream in, byte[] sequence)
9693
} else seqIndex = 0;
9794
}
9895

99-
10096
return -1;
10197
}
10298

10399
private int parseContentLength(byte[] headerBytes)
104-
throws IOException, NumberFormatException, IllegalArgumentException {
100+
throws IOException, IllegalArgumentException {
105101
ByteArrayInputStream headerIn = new ByteArrayInputStream(headerBytes);
106102
Properties props = new Properties();
107103
props.load(headerIn);
108104
return Integer.parseInt(props.getProperty(CONTENT_LENGTH));
109105
}
110106

111-
// no more accessible (not used!)
112-
private Bitmap readMjpegFrame() throws IOException {
113-
mark(FRAME_MAX_LENGTH);
114-
int headerLen;
115-
try {
116-
headerLen = getStartOfSequence(this, SOI_MARKER);
117-
} catch (IOException e) {
118-
if (DEBUG) Log.d(TAG, "IOException in betting headerLen.");
119-
reset();
120-
return null;
121-
}
122-
reset();
123-
124-
if (header == null || headerLen != headerLenPrev) {
125-
header = new byte[headerLen];
126-
if (DEBUG) Log.d(TAG, "header renewed " + headerLenPrev + " -> " + headerLen);
127-
}
128-
headerLenPrev = headerLen;
129-
readFully(header);
130-
131-
int ContentLengthNew = -1;
132-
try {
133-
ContentLengthNew = parseContentLength(header);
134-
} catch (NumberFormatException nfe) {
135-
ContentLengthNew = getEndOfSeqeunceSimplified(this, EOF_MARKER);
136-
137-
if (ContentLengthNew < 0) {
138-
if (DEBUG) Log.d(TAG, "Worst case for finding EOF_MARKER");
139-
reset();
140-
ContentLengthNew = getEndOfSeqeunce(this, EOF_MARKER);
141-
}
142-
} catch (IllegalArgumentException e) {
143-
if (DEBUG) Log.d(TAG, "IllegalArgumentException in parseContentLength");
144-
ContentLengthNew = getEndOfSeqeunceSimplified(this, EOF_MARKER);
145-
146-
if (ContentLengthNew < 0) {
147-
if (DEBUG) Log.d(TAG, "Worst case for finding EOF_MARKER");
148-
reset();
149-
ContentLengthNew = getEndOfSeqeunce(this, EOF_MARKER);
150-
}
151-
} catch (IOException e) {
152-
if (DEBUG) Log.d(TAG, "IOException in parseContentLength");
153-
reset();
154-
return null;
155-
}
156-
mContentLength = ContentLengthNew;
157-
reset();
158-
159-
if (frameData == null) {
160-
frameData = new byte[FRAME_MAX_LENGTH];
161-
if (DEBUG) Log.d(TAG, "frameData newed cl=" + FRAME_MAX_LENGTH);
162-
}
163-
if (mContentLength + HEADER_MAX_LENGTH > FRAME_MAX_LENGTH) {
164-
frameData = new byte[mContentLength + HEADER_MAX_LENGTH];
165-
if (DEBUG) Log.d(TAG, "frameData renewed cl=" + (mContentLength + HEADER_MAX_LENGTH));
166-
}
167-
168-
skipBytes(headerLen);
169-
170-
readFully(frameData, 0, mContentLength);
171-
172-
if (count++ % skip == 0) {
173-
return BitmapFactory.decodeStream(new ByteArrayInputStream(frameData, 0, mContentLength));
174-
} else {
175-
return null;
176-
}
177-
}
178-
179107
// no more accessible
180108
int readMjpegFrame(Bitmap bmp) throws IOException {
181109
mark(FRAME_MAX_LENGTH);
@@ -224,13 +152,9 @@ int readMjpegFrame(Bitmap bmp) throws IOException {
224152
mContentLength = ContentLengthNew;
225153
reset();
226154

227-
if (frameData == null) {
228-
frameData = new byte[FRAME_MAX_LENGTH];
229-
if (DEBUG) Log.d(TAG, "frameData newed cl=" + FRAME_MAX_LENGTH);
230-
}
231-
if (mContentLength + HEADER_MAX_LENGTH > FRAME_MAX_LENGTH) {
232-
frameData = new byte[mContentLength + HEADER_MAX_LENGTH];
233-
if (DEBUG) Log.d(TAG, "frameData renewed cl=" + (mContentLength + HEADER_MAX_LENGTH));
155+
if (frameData == null || mContentLength > frameData.length) {
156+
frameData = new byte[mContentLength]; // + HEADER_MAX_LENGTH];
157+
if (DEBUG) Log.d(TAG, "frameData renewed cl=" + mContentLength);
234158
}
235159

236160
skipBytes(headerLen);
@@ -248,4 +172,4 @@ int readMjpegFrame(Bitmap bmp) throws IOException {
248172
void setSkip(int s) {
249173
skip = s;
250174
}
251-
}
175+
}

0 commit comments

Comments
 (0)