1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package jdk.jshell.execution;
26
27import java.io.DataInputStream;
28import java.io.IOException;
29import java.io.InputStream;
30import java.io.OutputStream;
31import java.util.Map;
32
33/**
34 * Read from an InputStream which has been packetized and write its contents
35 * to the named OutputStreams.
36 *
37 * @author Jan Lahoda
38 * @see Util#demultiplexInput(java.io.InputStream, java.io.OutputStream, java.io.OutputStream, java.io.OutputStream...)
39 */
40class DemultiplexInput extends Thread {
41
42    private final DataInputStream delegate;
43    private final Map<String, OutputStream> io;
44    private final Iterable<OutputStream> closeList;
45
46    DemultiplexInput(InputStream input, Map<String, OutputStream> io, Iterable<OutputStream> closeList) {
47        super("output reader");
48        setDaemon(true);
49        this.delegate = new DataInputStream(input);
50        this.io = io;
51        this.closeList = closeList;
52    }
53
54    @Override
55    public void run() {
56        try {
57            while (true) {
58                int nameLen = delegate.read();
59                if (nameLen == (-1)) {
60                    break;
61                }
62                byte[] name = new byte[nameLen];
63                DemultiplexInput.this.delegate.readFully(name);
64                int dataLen = delegate.read();
65                byte[] data = new byte[dataLen];
66                DemultiplexInput.this.delegate.readFully(data);
67                String chan = new String(name, "UTF-8");
68                OutputStream out = io.get(chan);
69                if (out == null) {
70                    debug("Unexpected channel name: %s", chan);
71                } else {
72                    out.write(data);
73                }
74            }
75        } catch (IOException ex) {
76            debug(ex, "Failed reading output");
77        } finally {
78            for (OutputStream out : closeList) {
79                try {
80                    out.close();
81                } catch (IOException ex) {
82                    debug(ex, "Failed reading output");
83                }
84            }
85        }
86    }
87
88    /**
89     * Log debugging information. Arguments as for {@code printf}.
90     *
91     * @param format a format string as described in Format string syntax
92     * @param args arguments referenced by the format specifiers in the format
93     * string.
94     */
95    private void debug(String format, Object... args) {
96        // Reserved for future logging
97    }
98
99    /**
100     * Log a serious unexpected internal exception.
101     *
102     * @param ex the exception
103     * @param where a description of the context of the exception
104     */
105    private void debug(Throwable ex, String where) {
106        // Reserved for future logging
107    }
108
109}
110