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  */
  25 package jdk.jshell.execution;
  26 
  27 import java.io.DataInputStream;
  28 import java.io.IOException;
  29 import java.io.InputStream;
  30 import java.io.OutputStream;
  31 import 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  */
  40 class DemultiplexInput extends Thread {
  41 
  42     private final DataInputStream delegate;
  43     private final PipeInputStream command;
  44     private final Map<String, OutputStream> io;
  45 
  46     DemultiplexInput(InputStream input, PipeInputStream command,
  47             Map<String, OutputStream> io) {
  48         super("output reader");
  49         this.delegate = new DataInputStream(input);
  50         this.command = command;
  51         this.io = io;
  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                 if (chan.equals("command")) {
  69                     for (byte b : data) {
  70                         command.write(Byte.toUnsignedInt(b));
  71                     }
  72                 } else {
  73                     OutputStream out = io.get(chan);
  74                     if (out == null) {
  75                         debug("Unexpected channel name: %s", chan);
  76                     } else {
  77                         out.write(data);
  78                     }
  79                 }
  80             }
  81         } catch (IOException ex) {
  82             debug(ex, "Failed reading output");
  83         } finally {
  84             command.close();
  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 }