1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18package android.filterpacks.base;
19
20import android.filterfw.core.Filter;
21import android.filterfw.core.FilterContext;
22import android.filterfw.core.Frame;
23import android.filterfw.core.GenerateFieldPort;
24
25import java.io.OutputStream;
26import java.io.IOException;
27import java.nio.ByteBuffer;
28
29/**
30 * @hide
31 */
32public class OutputStreamTarget extends Filter {
33
34    @GenerateFieldPort(name = "stream")
35    private OutputStream mOutputStream;
36
37    public OutputStreamTarget(String name) {
38        super(name);
39    }
40
41    @Override
42    public void setupPorts() {
43        addInputPort("data");
44    }
45
46    @Override
47    public void process(FilterContext context) {
48        Frame input = pullInput("data");
49        ByteBuffer data;
50
51        if (input.getFormat().getObjectClass() == String.class) {
52            String stringVal = (String)input.getObjectValue();
53            data = ByteBuffer.wrap(stringVal.getBytes());
54        } else {
55            data = input.getData();
56        }
57        try {
58            mOutputStream.write(data.array(), 0, data.limit());
59            mOutputStream.flush();
60        } catch (IOException exception) {
61            throw new RuntimeException(
62                "OutputStreamTarget: Could not write to stream: " + exception.getMessage() + "!");
63        }
64    }
65}
66