1/*
2 * Copyright (C) 2012 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
17package libcore.io;
18
19import java.util.Base64;
20
21public final class DropBox {
22
23    /**
24     * Hook for customizing how events are reported.
25     */
26    private static volatile Reporter REPORTER = new DefaultReporter();
27
28    /**
29     * Used to replace default Reporter for logging events. Must be non-null.
30     */
31    public static void setReporter(Reporter reporter) {
32        if (reporter == null) {
33            throw new NullPointerException("reporter == null");
34        }
35        REPORTER = reporter;
36    }
37
38    /**
39     * Returns non-null Reporter.
40     */
41    public static Reporter getReporter() {
42        return REPORTER;
43    }
44
45    /**
46     * Interface to allow customization of reporting behavior.
47     */
48    public static interface Reporter {
49        public void addData(String tag, byte[] data, int flags);
50        public void addText(String tag, String data);
51    }
52
53    /**
54     * Default Reporter which reports events to the log.
55     */
56    private static final class DefaultReporter implements Reporter {
57
58        public void addData(String tag, byte[] data, int flags) {
59            System.out.println(tag + ": " + Base64.getEncoder().encodeToString(data));
60        }
61
62        public void addText(String tag, String data) {
63            System.out.println(tag + ": " + data);
64        }
65    }
66
67    public static void addData(String tag, byte[] data, int flags) {
68        getReporter().addData(tag, data, flags);
69    }
70
71    public static void addText(String tag, String data) {
72        getReporter().addText(tag, data);
73    }
74}
75