summaryrefslogtreecommitdiffstats
path: root/media/mca/filterfw/java/android/filterfw/core/SerializedFrame.java
blob: f493fd2536eea4fcddd5801aa03bc569babdaa44 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package android.filterfw.core;

import android.filterfw.core.Frame;
import android.filterfw.core.FrameFormat;
import android.filterfw.core.FrameManager;
import android.filterfw.core.NativeBuffer;
import android.filterfw.format.ObjectFormat;
import android.graphics.Bitmap;

import java.io.InputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OptionalDataException;
import java.io.OutputStream;
import java.io.StreamCorruptedException;
import java.lang.reflect.Constructor;
import java.nio.ByteBuffer;

/**
 * A frame that serializes any assigned values. Such a frame is used when passing data objects
 * between threads.
 *
 * @hide
 */
public class SerializedFrame extends Frame {

    /**
     * The initial capacity of the serialized data stream.
     */
    private final static int INITIAL_CAPACITY = 64;

    /**
     * The internal data streams.
     */
    private DirectByteOutputStream mByteOutputStream;
    private ObjectOutputStream mObjectOut;

    /**
     * An unsynchronized output stream that writes data to an accessible byte array. Callers are
     * responsible for synchronization. This is more efficient than a ByteArrayOutputStream, as
     * there are no array copies or synchronization involved to read back written data.
     */
    private class DirectByteOutputStream extends OutputStream {
        private byte[] mBuffer = null;
        private int mOffset = 0;
        private int mDataOffset = 0;

        public DirectByteOutputStream(int size) {
            mBuffer = new byte[size];
        }

        private final void ensureFit(int bytesToWrite) {
            if (mOffset + bytesToWrite > mBuffer.length) {
                byte[] oldBuffer = mBuffer;
                mBuffer = new byte[Math.max(mOffset + bytesToWrite, mBuffer.length * 2)];
                System.arraycopy(oldBuffer, 0, mBuffer, 0, mOffset);
                oldBuffer = null;
            }
        }

        public final void markHeaderEnd() {
            mDataOffset = mOffset;
        }

        public final int getSize() {
            return mOffset;
        }

        public byte[] getByteArray() {
            return mBuffer;
        }

        @Override
        public final void write(byte b[]) {
            write(b, 0, b.length);
        }

        @Override
        public final void write(byte b[], int off, int len) {
            ensureFit(len);
            System.arraycopy(b, off, mBuffer, mOffset, len);
            mOffset += len;
        }

        @Override
        public final void write(int b) {
            ensureFit(1);
            mBuffer[mOffset++] = (byte)b;
        }

        public final void reset() {
            mOffset = mDataOffset;
        }

        public final DirectByteInputStream getInputStream() {
            return new DirectByteInputStream(mBuffer, mOffset);
        }
    }

    /**
     * An unsynchronized input stream that reads data directly from a provided byte array. Callers
     * are responsible for synchronization and ensuring that the byte buffer is valid.
     */
    private class DirectByteInputStream extends InputStream {

        private byte[] mBuffer;
        private int mPos = 0;
        private int mSize;

        public DirectByteInputStream(byte[] buffer, int size) {
            mBuffer = buffer;
            mSize = size;
        }

        @Override
        public final int available() {
            return mSize - mPos;
        }

        @Override
        public final int read() {
            return (mPos < mSize) ? (mBuffer[mPos++] & 0xFF) : -1;
        }

        @Override
        public final int read(byte[] b, int off, int len) {
            if (mPos >= mSize) {
                return -1;
            }
            if ((mPos + len) > mSize) {
                len = mSize - mPos;
            }
            System.arraycopy(mBuffer, mPos, b, off, len);
            mPos += len;
            return len;
        }

        @Override
        public final long skip(long n) {
            if ((mPos + n) > mSize) {
                n = mSize - mPos;
            }
            if (n < 0) {
                return 0;
            }
            mPos += n;
            return n;
        }
    }

    SerializedFrame(FrameFormat format, FrameManager frameManager) {
        super(format, frameManager);
        setReusable(false);

        // Setup streams
        try {
            mByteOutputStream = new DirectByteOutputStream(INITIAL_CAPACITY);
            mObjectOut = new ObjectOutputStream(mByteOutputStream);
            mByteOutputStream.markHeaderEnd();
        } catch (IOException e) {
            throw new RuntimeException("Could not create serialization streams for "
                + "SerializedFrame!", e);
        }
    }

    static SerializedFrame wrapObject(Object object, FrameManager frameManager) {
        FrameFormat format = ObjectFormat.fromObject(object, FrameFormat.TARGET_SIMPLE);
        SerializedFrame result = new SerializedFrame(format, frameManager);
        result.setObjectValue(object);
        return result;
    }

    @Override
    protected boolean hasNativeAllocation() {
        return false;
    }

    @Override
    protected void releaseNativeAllocation() {
    }

    @Override
    public Object getObjectValue() {
        return deserializeObjectValue();
    }

    @Override
    public void setInts(int[] ints) {
        assertFrameMutable();
        setGenericObjectValue(ints);
    }

    @Override
    public int[] getInts() {
        Object result = deserializeObjectValue();
        return (result instanceof int[]) ? (int[])result : null;
    }

    @Override
    public void setFloats(float[] floats) {
        assertFrameMutable();
        setGenericObjectValue(floats);
    }

    @Override
    public float[] getFloats() {
        Object result = deserializeObjectValue();
        return (result instanceof float[]) ? (float[])result : null;
    }

    @Override
    public void setData(ByteBuffer buffer, int offset, int length) {
        assertFrameMutable();
        setGenericObjectValue(ByteBuffer.wrap(buffer.array(), offset, length));
    }

    @Override
    public ByteBuffer getData() {
        Object result = deserializeObjectValue();
        return (result instanceof ByteBuffer) ? (ByteBuffer)result : null;
    }

    @Override
    public void setBitmap(Bitmap bitmap) {
        assertFrameMutable();
        setGenericObjectValue(bitmap);
    }

    @Override
    public Bitmap getBitmap() {
        Object result = deserializeObjectValue();
        return (result instanceof Bitmap) ? (Bitmap)result : null;
    }

    @Override
    protected void setGenericObjectValue(Object object) {
        serializeObjectValue(object);
    }

    private final void serializeObjectValue(Object object) {
        try {
            mByteOutputStream.reset();
            mObjectOut.writeObject(object);
            mObjectOut.flush();
            mObjectOut.close();
        } catch (IOException e) {
            throw new RuntimeException("Could not serialize object " + object + " in "
                + this + "!", e);
        }
    }

    private final Object deserializeObjectValue() {
        try {
            InputStream inputStream = mByteOutputStream.getInputStream();
            ObjectInputStream objectStream = new ObjectInputStream(inputStream);
            return objectStream.readObject();
        } catch (IOException e) {
            throw new RuntimeException("Could not deserialize object in " + this + "!", e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException("Unable to deserialize object of unknown class in "
                + this + "!", e);
        }
    }

    @Override
    public String toString() {
        return "SerializedFrame (" + getFormat() + ")";
    }
}