Stream file of specified sections to given channel.

StreamWriter uses LZF compression on wire to decrease size to transfer.

@param channel where this writes data to
@throws IOException on any I/O error
765864
public void write(WritableByteChannel channel) throws IOException
    {
        long totalSize = totalSize();
        RandomAccessReader file = sstable.openDataReader();
        ChecksumValidator validator = new File(sstable.descriptor.filenameFor(Component.CRC)).exists()
                                    ? DataIntegrityMetadata.checksumValidator(sstable.descriptor)
                                    : null;
        transferBuffer = validator == null ? new byte[DEFAULT_CHUNK_SIZE] : new byte[validator.chunkSize];

        // setting up data compression stream
        compressedOutput = new LZFOutputStream(Channels.newOutputStream(channel));
        long progress = 0L;

        try
        {
            // stream each of the required sections of the file
            for (Pair<Long, Long> section : sections)
            {
                long start = validator == null ? section.left : validator.chunkStart(section.left);
                int readOffset = (int) (section.left - start);
                // seek to the beginning of the section
                file.seek(start);
                if (validator != null)
                    validator.seek(start);

                // length of the section to read
                long length = section.right - start;
                // tracks write progress
                long bytesRead = 0;
                while (bytesRead < length)
                {
                    long lastBytesRead = write(file, validator, readOffset, length, bytesRead);
                    bytesRead += lastBytesRead;
                    progress += (lastBytesRead - readOffset);
                    session.progress(sstable.descriptor, ProgressInfo.Direction.OUT, progress, totalSize);
                    readOffset = 0;
                }

                // make sure that current section is send
                compressedOutput.flush();
            }
        }
        finally
        {
            // no matter what happens close file
            FileUtils.closeQuietly(file);
            FileUtils.closeQuietly(validator);
        }
    }
919786
private void write(final DataInputStream stream, final Writer outputWriter, final int numberOfBytes)
      throws IOException {
    final byte[] buf = new byte[65535];
    int lenRemaining = numberOfBytes;
    while (lenRemaining > 0) {
      final int len = stream.read(buf, 0, lenRemaining > 65535 ? 65535 : lenRemaining);
      if (len > 0) {
        outputWriter.write(new String(buf, 0, len, "UTF-8"));
        lenRemaining -= len;
      } else {
        break;
      }
    }
  }
104981
public void writeContentsTo(final OutputStream pStream) throws IOException {
        if (content == null) {
            throw new IOException("Cache is null, no content to write.");
        }

        content.writeTo(pStream);
    }
1351991
public OutputStream write() throws IOException {
        if(gz.exists())
            gz.delete();
        try {
            return Files.newOutputStream(file.toPath());
        } catch (InvalidPathException e) {
            throw new IOException(e);
        }
    }
165424
def write_stream(self, stream, left_chunk, right_chunk, volume):
        '''
        
        '''
        if len(left_chunk) != len(right_chunk):
            raise ValueError()

        for i in range(len(left_chunk)):
            chunk = (left_chunk[i] + right_chunk[i]) * volume
            data = struct.pack("2f", chunk, chunk)
            stream.write(data)
850130
public void write_compressed(final OtpErlangObject o, final int level) {
        @SuppressWarnings("resource")
        final OtpOutputStream oos = new OtpOutputStream(o);
        /*
         * similar to erts_term_to_binary() in external.c: We don't want to
         * compress if compression actually increases the size. Since
         * compression uses 5 extra bytes (COMPRESSED tag + size), don't
         * compress if the original term is smaller.
         */
        if (oos.size() < 5) {
            // fast path for small terms
            try {
                oos.writeToAndFlush(this);
                // if the term is written as a compressed term, the output
                // stream is closed, so we do this here, too
                close();
            } catch (final IOException e) {
                throw new java.lang.IllegalArgumentException(
                        "Intermediate stream failed for Erlang object " + o);
            }
        } else {
            final int startCount = super.count;
            // we need destCount bytes for an uncompressed term
            // -> if compression uses more, use the uncompressed term!
            final int destCount = startCount + oos.size();
            fixedSize = destCount;
            final Deflater def = new Deflater(level);
            final java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream(
                    this, def);
            try {
                write1(OtpExternal.compressedTag);
                write4BE(oos.size());
                oos.writeTo(dos);
                dos.close(); // note: closes this, too!
            } catch (final IllegalArgumentException e) {
                /*
                 * Discard further un-compressed data (if not called, there may
                 * be memory leaks).
                 *
                 * After calling java.util.zip.Deflater.end(), the deflater
                 * should not be used anymore, not even the close() method of
                 * dos. Calling dos.close() before def.end() is prevented since
                 * an unfinished DeflaterOutputStream will try to deflate its
                 * unprocessed data to the (fixed) byte array which is prevented
                 * by ensureCapacity() and would also unnecessarily process
                 * further data that is discarded anyway.
                 *
                 * Since we are re-using the byte array of this object below, we
                 * must not call close() in e.g. a finally block either (with or
                 * without a call to def.end()).
                 */
                def.end();
                // could not make the value smaller than originally
                // -> reset to starting count, write uncompressed
                super.count = startCount;
                try {
                    oos.writeTo(this);
                    // if the term is written as a compressed term, the output
                    // stream is closed, so we do this here, too
                    close();
                } catch (final IOException e2) {
                    throw new java.lang.IllegalArgumentException(
                            "Intermediate stream failed for Erlang object " + o);
                }
            } catch (final IOException e) {
                throw new java.lang.IllegalArgumentException(
                        "Intermediate stream failed for Erlang object " + o);
            } finally {
                fixedSize = Integer.MAX_VALUE;
            }
        }
    }
865883
public static long copy(ReadableByteChannel inputChannel, WritableByteChannel outputChannel) throws IOException {
    return new StreamCopier(inputChannel, outputChannel).copy();
  }
198434
public static void writeToOutputStream(Structure structure, OutputStream outputStream) throws IOException{
		// Set up this writer
		AdapterToStructureData writerToEncoder = new AdapterToStructureData();
		// Get the writer - this is what people implement
		new MmtfStructureWriter(structure, writerToEncoder);
		// Now write this data to file
		byte[] outputBytes = WriterUtils.getDataAsByteArr(writerToEncoder);
		outputStream.write(outputBytes,0,outputBytes.length);
	}
1196633
@FFDCIgnore({ IOException.class })
    private void convertFile(FileChannel fc) throws IOException {
        if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled()) {
            Tr.event(tc, "Converting FileChannel to buffers");
        }
        final WsByteBuffer[] body = new WsByteBuffer[1];
        final WsByteBufferPoolManager mgr = HttpDispatcher.getBufferManager();
        final long max = fc.size();
        final long blocksize = (1048576L < max) ? 1048576L : max;
        long offset = 0;
        while (offset < max) {
            ByteBuffer bb = fc.map(MapMode.READ_ONLY, offset, blocksize);
            offset += blocksize;
            WsByteBuffer wsbb = mgr.wrap(bb);
            body[0] = wsbb;
            try {
                this.isc.sendResponseBody(body);
            } catch (MessageSentException mse) {
                FFDCFilter.processException(mse, getClass().getName(),
                                            "convertFile", new Object[] { this, this.isc });
                if (TraceComponent.isAnyTracingEnabled() && tc.isEventEnabled()) {
                    Tr.event(tc, "Invalid state, message-sent-exception received; " + this.isc);
                }
                this.error = new IOException("Invalid state");
                throw this.error;
            } catch (IOException ioe) {
                // no FFDC required
                this.error = ioe;
                if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                    Tr.debug(tc, "Received exception during write: " + ioe);
                }
                throw ioe;
            } finally {
                wsbb.release();
            }
        } // end-while
    }
1150480
public static final long write(WritableByteChannel channel, ByteBuffer[] srcs, int offset, int length) throws IOException
    {
        long res = 0;
        for  (int ii=0;ii<length;ii++)
        {
            ByteBuffer bb = srcs[ii+offset];
            if (bb.hasRemaining())
            {
                res += channel.write(bb);
                if (bb.hasRemaining())
                {
                    break;
                }
            }
        }
        return res;
    }
1099231
@SuppressWarnings("resource")
	private void initializeContentBuffer() throws IOException {
		this.tempFile = File.createTempFile("shapeWriter", ".sh_");  //$NON-NLS-1$//$NON-NLS-2$
		this.tempFile.deleteOnExit();
		this.tempStream = Channels.newChannel(new FileOutputStream(this.tempFile));
		this.buffer = ByteBuffer.allocate(BLOCK_SIZE);
		this.bufferPosition = 0;
	}
1004989
private static void writeStream(final InputStream input, final FileWriter writer)
            throws IOException
    {
        writeStream(input, writer, LineEndingStyle.LOCAL);
    }
527051
@VisibleForTesting
  static void writeBytesToStream(byte[] bytes, final OutputStream outputStream)
      throws IOException {
    new ByteSink() {
      @Override
      public OutputStream openStream() {
        return outputStream;
      }
    }.write(bytes);
  }
449872
static public void copyStream(Reader reader, Writer writer, int transferBufferSize) throws IOException {
		if( transferBufferSize < 1 ) {
			throw new IOException("Transfer buffer size can not be smaller than 1");
		}

		char[] buffer = new char[transferBufferSize];

		int bytesRead = reader.read(buffer);
		while( bytesRead >= 0 ) {
			writer.write(buffer, 0, bytesRead);

			bytesRead = reader.read(buffer);
		}
	}
285325
@Override
	@SuppressWarnings("resource")
	public void close() throws IOException {
		flush();

		if (this.tempStream != null) {
			this.tempStream.close();
			this.tempStream = null;
		}

		if (this.stream != null && this.stream.isOpen()) {

			// Copy the channels
			if (this.buffer != null && this.tempFile != null) {
				try {
					try (ReadableByteChannel in = Channels.newChannel(new FileInputStream(this.tempFile))) {
						final ByteBuffer hbuffer = ByteBuffer.allocate(100);
						in.read(hbuffer);

						hbuffer.limit(100);
						hbuffer.position(6 * 4);
						hbuffer.order(ByteOrder.BIG_ENDIAN);
						hbuffer.putInt(toESRIWords(this.bufferPosition));
						hbuffer.rewind();
						this.stream.write(hbuffer);

						int nbRead;
						this.buffer.rewind();

						while ((nbRead = in.read(this.buffer)) >= 0) {
							this.buffer.rewind();
							this.buffer.limit(nbRead);
							this.stream.write(this.buffer);
							this.buffer.rewind();
							this.buffer.limit(this.buffer.capacity());
						}
					}

				} finally {
					this.tempFile.delete();
					this.tempFile = null;
				}

			}
			this.stream.close();
		}
	}
1025670
public static void exportToStream(final OutputStream outputStream, final Iterable<? extends NodeInterface> nodes, final Iterable<? extends RelationshipInterface> relationships, final Iterable<String> filePaths, final boolean includeFiles) throws FrameworkException {

		try (final ZipOutputStream zos = new ZipOutputStream(outputStream)) {

			final Set<String> filesToInclude = new LinkedHashSet<>();
			if (filePaths != null) {

				for (String file : filePaths) {

					filesToInclude.add(file);
				}
			}

			// set compression
			zos.setLevel(6);

			if (includeFiles) {

				logger.info("Exporting files..");

				// export files first
				exportDirectory(zos, new File("files"), "", filesToInclude.isEmpty() ? null : filesToInclude);
			}

			// export database
			exportDatabase(zos, new BufferedOutputStream(zos), nodes, relationships);

			// finish ZIP file
			zos.finish();

			// close stream
			zos.flush();
			zos.close();

		} catch (Throwable t) {

			logger.warn("", t);

			throw new FrameworkException(500, t.getMessage());
		}
	}
103797
public void write(WritableByteChannel channel) throws IOException {
        logger.debug("Writing> {}", this);
        for (Field field : fields) {
            field.write(channel);
        }
    }