Opens the associated {@link TracePrintStream} by wrapping the socket streams.
544974
@Override
  public void open() {
    try {
      if (this.isOpened() == false) {
        System.out.println(formatVersionInfo() + " Opening ...");

        InetSocketAddress inetSocketAddress = new InetSocketAddress(this.hostName, this.portNo);
        this.pizzaConnection = new Socket();
        this.pizzaConnection.connect(inetSocketAddress, TIMEOUT);
        this.setBufferedOutputStream(new BufferedOutputStream(this.pizzaConnection.getOutputStream(), this.getBufferSize()));
        this.setTracePrintStream(new TracePrintStream(this.getBufferedOutputStream(), this.getThreadMap()));

        sendUserCredentials();
        this.getTracePrintStream().printf("--> TraceLog opened!%n");
        this.getTracePrintStream().printf("    Time     : %tc%n", new Date());
        this.getTracePrintStream().printf("    Bufsize  : %d%n", this.getBufferSize());
        this.getTracePrintStream().printf("    Autoflush: %b%n%n", this.isAutoflush());

        this.setOpened(true);
      }
      else {
        System.err.println("WARNING: Tracelog is opened already.");
      }
    }
    catch (IOException ex) {
      ex.printStackTrace(System.err);
    }
  }
1251839
protected InputStream open()
    {
    try
      {
      if( getType() == null)
        {
        throw new IllegalStateException( String.format( "Unknown type for %s", this));
        }

      URL location = getLocation();
    
      return
        location == null
        ? System.in
        : (stream_ = location.openStream());
      }
    catch( Exception e)
      {
      throw new IllegalStateException( String.format( "Can't read %s", this), e);
      }
    }
736192
public void openStream() throws IOException {
        socket = new Socket(host, port);
        socketStream = socket.getOutputStream();
        if (null != context.get("name") && null != context.get("id")) {
            Object group = context.get("group");
            String desc = (null != group ? group + "/" : "") + context.get("name");
            write("Job started: " + desc + ": " + context.get("id"));
        }
        write("Execution: " + context.get("execid"));
    }
548366
@Override
	public void open(Configuration parameters) throws Exception {
		try {
			client = new Socket(hostIp, port);
			outputStream = client.getOutputStream();
			streamWriter = new DataOutputViewStreamWrapper(outputStream);
		}
		catch (IOException e) {
			throw new IOException("Cannot connect to the client to send back the stream", e);
		}
	}
347853
@Override
  @SuppressWarnings("unchecked")
  public void start() {
    if (isStopped()) return;
    
    logger().log("Inbound stream listening: for '" + reader.name() + "'");
    
    try {
      reader.openFor(this);
    } catch (Exception e) {
      reader.close();
      e.printStackTrace();
      throw new IllegalStateException(e.getMessage(), e);
    }
    cancellable = this.stage().scheduler().schedule(selfAs(Scheduled.class), null, 1000, probeInterval);
  }
905620
@Override
    // TODO: create AssetStreamException ?
    public InputStream openStream() {
        try {
            final ZipFile file = new ZipFile(this.file);
            return new InputStreamWrapper(file, file.getInputStream(entry));
        } catch (final Exception e) {
            throw new RuntimeException("Could not open zip file stream", e);
        }
    }
144175
@Override
    public synchronized InputStream getInputStream() throws IOException {
        if (connected == false) {
            // Implicitly open the connection if it has not yet been done so.
            connect();
        }

        Object token = ThreadIdentityManager.runAsServer();
        try {
            if (inputStream == null) {
                if ("".equals(archivePath)) {
                    inputStream = new FileInputStream(urlTargetFile);
                } else {
                    inputStream = new FilterZipFileInputStream(urlTargetFile, archivePath);
                }
            }
        } finally {
            ThreadIdentityManager.reset(token);
        }

        return inputStream;
    }
912015
public InputStream open() throws IOException, InterruptedException {
        if (recyclableInflaterInstance != null) {
            throw new IOException("Zip entry already open");
        }
        if (isDeflated) {
            recyclableInflaterInstance = nestedJarHandler.inflaterRecycler.acquire();
        }
        return new InputStream() {
            /** The data start offset within the physical zip file. */
            private final long dataStartOffsetWithinPhysicalZipFile = getEntryDataStartOffsetWithinPhysicalZipFile();

            /** A scratch buffer. */
            private final byte[] scratch = new byte[8192];

            /** The current 2GB chunk of the zip entry. */
            private ByteBuffer currChunkByteBuf;

            /** True if the current 2GB chunk is the last chunk in the zip entry. */
            private boolean isLastChunk;

            /** The index of the current 2GB chunk. */
            private int currChunkIdx;

            /** True if the end of the zip entry has been reached. */
            private boolean eof;

            /** The {@link Inflater} instance, or null if the entry is stored rather than deflated. */
            private final Inflater inflater = isDeflated ? recyclableInflaterInstance.getInflater() : null;

            /** True if this {@link InputStream} has been closed. */
            private final AtomicBoolean closed = new AtomicBoolean(false);

            /** The size of the {@link Inflate} buffer to use. */
            private static final int INFLATE_BUF_SIZE = 1024;

            // Open the first 2GB chunk.
            {
                // Calculate the chunk index for the first chunk
                currChunkIdx = (int) (dataStartOffsetWithinPhysicalZipFile / FileUtils.MAX_BUFFER_SIZE);

                // Get the MappedByteBuffer for the 2GB chunk, and duplicate it
                currChunkByteBuf = parentLogicalZipFile.physicalZipFile.getByteBuffer(currChunkIdx).duplicate();

                // Calculate the start position within the first chunk, and set the position of the slice.
                // N.B. the cast to Buffer is necessary, see:
                // https://github.com/plasma-umass/doppio/issues/497#issuecomment-334740243
                // https://github.com/classgraph/classgraph/issues/284#issuecomment-443612800
                final int chunkPos = (int) (dataStartOffsetWithinPhysicalZipFile
                        - (((long) currChunkIdx) * (long) FileUtils.MAX_BUFFER_SIZE));
                ((Buffer) currChunkByteBuf).position(chunkPos);

                // Calculate end pos for the first chunk, and truncate it if it overflows 2GB
                final long endPos = chunkPos + compressedSize;
                ((Buffer) currChunkByteBuf).limit((int) Math.min(FileUtils.MAX_BUFFER_SIZE, endPos));
                isLastChunk = endPos <= FileUtils.MAX_BUFFER_SIZE;
            }

            /** Advance to the next 2GB chunk. */
            private boolean readNextChunk() throws IOException, InterruptedException {
                currChunkIdx++;
                if (currChunkIdx >= parentLogicalZipFile.physicalZipFile.numMappedByteBuffers) {
                    // Ran out of chunks
                    return false;
                }

                // Calculate how many bytes were consumed in previous chunks
                final long chunkStartOff = ((long) currChunkIdx) * (long) FileUtils.MAX_BUFFER_SIZE;
                final long priorBytes = chunkStartOff - dataStartOffsetWithinPhysicalZipFile;
                final long remainingBytes = compressedSize - priorBytes;
                if (remainingBytes <= 0) {
                    return false;
                }

                // Get the MappedByteBuffer for the next 2GB chunk, and duplicate it
                currChunkByteBuf = parentLogicalZipFile.physicalZipFile.getByteBuffer(currChunkIdx).duplicate();

                // The start position for 2nd and subsequent chunks is 0.
                // N.B. the cast to Buffer is necessary, see:
                // https://github.com/plasma-umass/doppio/issues/497#issuecomment-334740243
                // https://github.com/classgraph/classgraph/issues/284#issuecomment-443612800
                ((Buffer) currChunkByteBuf).position(0);

                // Calculate end pos for the next chunk, and truncate it if it overflows 2GB
                ((Buffer) currChunkByteBuf).limit((int) Math.min(FileUtils.MAX_BUFFER_SIZE, remainingBytes));
                isLastChunk = remainingBytes <= FileUtils.MAX_BUFFER_SIZE;
                return true;
            }

            /**
             * Inflate deflated data.
             *
             * @param buf
             *            the buffer to inflate into.
             * @param off
             *            the offset within buf to start writing.
             * @param len
             *            the number of bytes of uncompressed data to read.
             * @return the number of bytes read.
             * @throws IOException
             *             if an I/O exception occurred.
             * @throws InterruptedException
             *             if the thread was interrupted.
             */
            private int readDeflated(final byte[] buf, final int off, final int len)
                    throws IOException, InterruptedException {
                try {
                    final byte[] inflateBuf = new byte[INFLATE_BUF_SIZE];
                    int numInflatedBytes;
                    while ((numInflatedBytes = inflater.inflate(buf, off, len)) == 0) {
                        if (inflater.finished() || inflater.needsDictionary()) {
                            eof = true;
                            return -1;
                        }
                        if (inflater.needsInput()) {
                            // Check if there's still data left in the current chunk
                            if (!currChunkByteBuf.hasRemaining()
                                    // No more bytes in current chunk -- get next chunk, and then make sure
                                    // that currChunkByteBuf.hasRemaining() subsequently returns true
                                    && !(readNextChunk() && currChunkByteBuf.hasRemaining())) {
                                // Ran out of data in the current chunk, and could not read a new chunk
                                throw new IOException("Unexpected EOF in deflated data");
                            }
                            // Set inflater input for the current chunk

                            // In JDK11+: simply use the following instead of all the lines below:
                            //     inflater.setInput(currChunkByteBuf);
                            // N.B. the ByteBuffer version of setInput doesn't seem to need the extra
                            // padding byte at the end when using the "nowrap" Inflater option.

                            // Copy from the ByteBuffer into a temporary byte[] array (needed for JDK<11). 
                            try {
                                final int remaining = currChunkByteBuf.remaining();
                                if (isLastChunk && remaining < inflateBuf.length) {
                                    // An extra dummy byte is needed at the end of the input stream when
                                    // using the "nowrap" Inflater option.
                                    // See: ZipFile.ZipFileInputStream.fill()
                                    currChunkByteBuf.get(inflateBuf, 0, remaining);
                                    inflateBuf[remaining] = (byte) 0;
                                    inflater.setInput(inflateBuf, 0, remaining + 1);
                                } else if (isLastChunk && remaining == inflateBuf.length) {
                                    // If this is the last chunk to read, and the number of remaining
                                    // bytes is exactly the size of the buffer, read one byte fewer than
                                    // the number of remaining bytes, to cause the last byte to be read
                                    // in an extra pass.
                                    currChunkByteBuf.get(inflateBuf, 0, remaining - 1);
                                    inflater.setInput(inflateBuf, 0, remaining - 1);
                                } else {
                                    // There are more than inflateBuf.length bytes remaining to be read,
                                    // or this is not the last chunk (i.e. read all remaining bytes in
                                    // this chunk, which will trigger the next chunk to be read on the
                                    // next loop iteration)
                                    final int bytesToRead = Math.min(inflateBuf.length, remaining);
                                    currChunkByteBuf.get(inflateBuf, 0, bytesToRead);
                                    inflater.setInput(inflateBuf, 0, bytesToRead);
                                }
                            } catch (final BufferUnderflowException e) {
                                // Should not happen
                                throw new IOException("Unexpected EOF in deflated data");
                            }
                        }
                    }
                    return numInflatedBytes;
                } catch (final DataFormatException e) {
                    throw new ZipException(
                            e.getMessage() != null ? e.getMessage() : "Invalid deflated zip entry data");
                }
            }

            /**
             * Copy stored (non-deflated) data from ByteBuffer to target buffer.
             *
             * @param buf
             *            the buffer to copy the stored entry into.
             * @param off
             *            the offset within buf to start writing.
             * @param len
             *            the number of bytes to read.
             * @return the number of bytes read.
             * @throws IOException
             *             if an I/O exception occurred.
             * @throws InterruptedException
             *             if the thread was interrupted.
             */
            private int readStored(final byte[] buf, final int off, final int len)
                    throws IOException, InterruptedException {
                int read = 0;
                while (read < len) {
                    if (!currChunkByteBuf.hasRemaining() && !readNextChunk()) {
                        return read == 0 ? -1 : read;
                    }
                    final int remainingToRead = len - read;
                    final int remainingInBuf = currChunkByteBuf.remaining();
                    final int numBytesRead = Math.min(remainingToRead, remainingInBuf);
                    try {
                        currChunkByteBuf.get(buf, off + read, numBytesRead);
                    } catch (final BufferUnderflowException e) {
                        // Should not happen
                        throw new EOFException("Unexpected EOF in stored (non-deflated) zip entry data");
                    }
                    read += numBytesRead;
                }
                return read;
            }

            @Override
            public int read(final byte[] buf, final int off, final int len) throws IOException {
                if (closed.get()) {
                    throw new IOException("Stream closed");
                }
                if (buf == null) {
                    throw new NullPointerException();
                } else if (off < 0 || len < 0 || len > buf.length - off) {
                    throw new IndexOutOfBoundsException();
                } else if (len == 0) {
                    return 0;
                } else if (parentLogicalZipFile.physicalZipFile.fileLen == 0) {
                    return -1;
                }
                try {
                    if (isDeflated) {
                        return readDeflated(buf, off, len);
                    } else {
                        return readStored(buf, off, len);
                    }
                } catch (final InterruptedException e) {
                    nestedJarHandler.interruptionChecker.interrupt();
                    throw new IOException("Thread was interrupted");
                }
            }

            @Override
            public int read() throws IOException {
                if (closed.get()) {
                    throw new IOException("Stream closed");
                }
                return read(scratch, 0, 1) == -1 ? -1 : scratch[0] & 0xff;
            }

            @Override
            public int available() throws IOException {
                if (closed.get()) {
                    throw new IOException("Stream closed");
                }
                if (inflater.finished()) {
                    eof = true;
                }
                return eof ? 0 : 1;
            }

            @Override
            public long skip(final long n) throws IOException {
                if (closed.get()) {
                    throw new IOException("Stream closed");
                }
                if (n < 0) {
                    throw new IllegalArgumentException("Invalid skip value");
                }
                long total = 0;
                while (total < n) {
                    final int numSkipped = read(scratch, 0, (int) Math.min(n - total, scratch.length));
                    if (numSkipped == -1) {
                        eof = true;
                        break;
                    }
                    total += numSkipped;
                }
                return total;
            }

            @Override
            public boolean markSupported() {
                return false;
            }

            @Override
            public synchronized void mark(final int readlimit) {
                throw new IllegalArgumentException("Not supported");
            }

            @Override
            public synchronized void reset() throws IOException {
                throw new IllegalArgumentException("Not supported");
            }

            @Override
            public void close() throws IOException {
                if (!closed.getAndSet(true)) {
                    currChunkByteBuf = null;
                    if (recyclableInflaterInstance != null) {
                        // Reset and recycle the Inflater
                        nestedJarHandler.inflaterRecycler.recycle(recyclableInflaterInstance);
                        recyclableInflaterInstance = null;
                    }
                }
            }
        };
    }
936419
def open(self, options=None, mimetype='application/octet-stream'):
        
        return self.connection.open(self, options, mimetype)
1164460
@Override
  public void close() {
    try {
      if (this.isOpened() == true) {
        this.getTracePrintStream().println();
        this.getTracePrintStream().printf("--> TraceLog closing!%n");
        this.getTracePrintStream().printf("    Time     : %tc%n", new Date());

        System.out.println(formatStreamErrorState() + " Closing ...");

        this.getTracePrintStream().close();
        this.getBufferedOutputStream().close();
        this.fileOutputStream.close();
        this.setOpened(false);
      }
      else {
        System.err.println("WARNING: Tracelog is closed already.");
      }
    }
    catch (IOException ex) {
      ex.printStackTrace(System.err);
    }
  }
583065
@Override
    public InputStream openStream() {
        try {
            return new BufferedInputStream(url.openStream(), 8192);
        } catch (Exception e) {
            throw new RuntimeException("Could not open stream for url " + url.toExternalForm(), e);
        }
    }
1317076
@Override
  public ClientSocket openWarm()
  {
    State state = _state;

    if (! state.isEnabled()) {
      return null;
    }
    
    /*
    long now = CurrentTime.getCurrentTime();
    
    if (isFailed(now)) {
      return null;
    }
    */

    ClientSocket stream = openRecycle();

    if (stream != null) {
      return stream;
    }

    if (canOpenWarm()) {
      return connect();
    }
    else {
      return null;
    }
  }
1148562
private OutputStream openFile(File file) throws BindException
  {
    try
    {
      return new GZIPOutputStream(new FileOutputStream(file));
    }
    catch (IOException ex)
    {
      throw new BindException(
          String.format("Failed to create output file %s: %s", file.toString(), ex.getMessage()), BindException.Type.SERIALIZATION);
    }
  }
37662
public InputStream openSerialStream(FieldList record, String strLookupKey)
    {
        try
        {
            String strFilePathName = this.getFilename(strLookupKey, false);
            File file = new File(strFilePathName);
            if (file.exists())
            {
                return new FileInputStream(strFilePathName);
            }
             
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return null;
    }
184382
@Override
  public StreamImpl stream()
    throws IOException
  {
    if (_channelStream == null) {
      _channelStream = new SocketChannelStream();
    }
    
    if (_sslSocket == null) {
      _channelStream.init(_channel);
      _streamImpl = _channelStream;
    }
    else {
      if (_sslStream == null) {
        _sslStream = new SocketStream();
      }
      
      _sslStream.init(_sslSocket);
      _streamImpl = _sslStream;
    }

    return _streamImpl;
  }
1066838
public InputStream openInputStream() throws FetchException {
        try {
            return getInternalBlobForFetch().getBinaryStream();
        } catch (SQLException e) {
            throw mRepo.toFetchException(e);
        }
    }
1135749
def _handle_stream(self, stream, address):
        """"""
        try:
            assert get_thread_ident() == self.ioloop_thread_id
            stream.set_close_callback(partial(self._stream_closed_callback,
                                              stream))
            # Our message packets are small, don't delay sending them.
            stream.set_nodelay(True)
            stream.max_write_buffer_size = self.MAX_WRITE_BUFFER_SIZE

            # Abuse IOStream object slightly by adding 'address' and 'closing'
            # attributes. Use nasty prefix to prevent naming collisions.
            stream.KATCPServer_address = address
            # Flag to indicate that no more write should be accepted so that
            # we can flush the write buffer when closing a connection
            stream.KATCPServer_closing = False

            client_conn = self.client_connection_factory(self, stream)
            self._connections[stream] = client_conn
            try:
                yield gen.maybe_future(self._device.on_client_connect(client_conn))
            except Exception:
                # If on_client_connect fails there is no reason to continue
                # trying to handle this connection. Try and send exception info
                # to the client and disconnect
                e_type, e_value, trace = sys.exc_info()
                reason = "\n".join(traceback.format_exception(
                    e_type, e_value, trace, self._tb_limit))
                log_msg = 'Device error initialising connection {0}'.format(reason)
                self._logger.error(log_msg)
                stream.write(str(Message.inform('log', log_msg)))
                stream.close(exc_info=True)
            else:
                self._line_read_loop(stream, client_conn)
        except Exception:
            self._logger.error('Unhandled exception trying '
                               'to handle new connection', exc_info=True)