Write bytes to an {@link OutputStream}.

@param aOS
The output stream to write to. May not be <code>null</code>. Is closed
independent of error or success.
@param aBuf
The byte array to be written. May not be <code>null</code>.
@return {@link ESuccess}
173708
@Nonnull
  public static ESuccess writeStream (@WillClose @Nonnull final OutputStream aOS, @Nonnull final byte [] aBuf)
  {
    return writeStream (aOS, aBuf, 0, aBuf.length);
  }
639739
public int write(OutputStreamWithBuffer os, char []cbuf, int off, int len)
    throws IOException
  {
    for (int i = 0; i < len; i++) {
      write(os, cbuf[off + i]);
    }
    
    return len;
  }
358078
public static void writeFile(VirtualFile virtualFile, byte[] bytes) throws IOException {
        final File file = virtualFile.getPhysicalFile();
        file.getParentFile().mkdirs();
        final FileOutputStream fos = new FileOutputStream(file);
        try {
            fos.write(bytes);
            fos.close();
        } finally {
            safeClose(fos);
        }
    }
951429
public void encode(AsnOutputStream aos) throws EncodeException {
        if (this.userType == null && this.providerType == null)
            throw new EncodeException("Error encoding ResultSourceDiagnostic: Value not set");

        try {
            aos.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, _TAG);
            int pos = aos.StartContentDefiniteLength();

            if (this.userType != null) {
                aos.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, _TAG_U);
                int pos2 = aos.StartContentDefiniteLength();
                aos.writeInteger(this.userType.getType());
                aos.FinalizeContent(pos2);
            } else {
                aos.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, _TAG_P);
                int pos2 = aos.StartContentDefiniteLength();
                aos.writeInteger(this.providerType.getType());
                aos.FinalizeContent(pos2);
            }

            aos.FinalizeContent(pos);

        } catch (IOException e) {
            throw new EncodeException("IOException while encoding ResultSourceDiagnostic: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding ResultSourceDiagnostic: " + e.getMessage(), e);
        }

    }
318177
public void encode(AsnOutputStream aos) throws EncodeException {

        if (acn == null)
            throw new EncodeException("Error encoding DialogUniAPDU: Application Context Name must not be null");

        try {

            aos.writeTag(Tag.CLASS_APPLICATION, false, _TAG_UNIDIRECTIONAL);
            int pos = aos.StartContentDefiniteLength();

            // lets not omit protocol version, we check byte[] in tests, it
            // screws them :)
            if (!doNotSendProtocolVersion) {
                ProtocolVersion pv = TcapFactory.createProtocolVersion();
                pv.encode(aos);
            }
            this.acn.encode(aos);

            if (ui != null)
                ui.encode(aos);

            aos.FinalizeContent(pos);

        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding DialogUniAPDU: " + e.getMessage(), e);
        }
    }
85753
public void write(BitOutputStream os, boolean isLast) throws IOException {

        os.writeRawUInt(isLast, STREAM_METADATA_IS_LAST_LEN);
        os.writeRawUInt(METADATA_TYPE_STREAMINFO, STREAM_METADATA_TYPE_LEN);
        os.writeRawUInt(calcLength(), STREAM_METADATA_LENGTH_LEN);

        os.writeRawUInt(minBlockSize, STREAMINFO_MIN_BLOCK_SIZE_LEN);
        os.writeRawUInt(maxBlockSize, STREAMINFO_MAX_BLOCK_SIZE_LEN);
        os.writeRawUInt(minFrameSize, STREAMINFO_MIN_FRAME_SIZE_LEN);
        os.writeRawUInt(maxFrameSize, STREAMINFO_MAX_FRAME_SIZE_LEN);
        os.writeRawUInt(sampleRate, STREAMINFO_SAMPLE_RATE_LEN);
        os.writeRawUInt(channels - 1, STREAMINFO_CHANNELS_LEN);
        os.writeRawULong(bitsPerSample - 1, STREAMINFO_BITS_PER_SAMPLE_LEN);
        os.writeRawULong(totalSamples, STREAMINFO_TOTAL_SAMPLES_LEN);
        os.writeByteBlock(md5sum, md5sum.length);
        os.flushByteAligned();
    }
845776
public static void writeByteArrayToFile(final File file, final byte[] byteArray)
		throws IOException
	{
		try (FileOutputStream fos = new FileOutputStream(file);
			BufferedOutputStream bos = new BufferedOutputStream(fos))
		{
			bos.write(byteArray);
		}
		catch (final FileNotFoundException ex)
		{
			throw ex;
		}
		catch (final IOException ex)
		{
			throw ex;
		}
	}
297943
public static void writeGzipObjectToStream(final Object o, OutputStream out) {
    out = new BufferedOutputStream(out);
    try {
      out = new GZIPOutputStream(out, true);
      final ObjectOutputStream oos = new ObjectOutputStream(out);
      oos.writeObject(o);
      oos.flush();
    } catch (final IOException e) {
      e.printStackTrace();
    }
  }
818708
@GwtIncompatible("OutputStream")
  static OutputStream asOutputStream(final ByteOutput output) {
    checkNotNull(output);
    return new OutputStream() {
      @Override
      public void write(int b) throws IOException {
        output.write((byte) b);
      }

      @Override
      public void flush() throws IOException {
        output.flush();
      }

      @Override
      public void close() throws IOException {
        output.close();
      }
    };
  }
326434
public final Item writeStartedFlush(TransactionCommon t, AOStream stream) throws Exception
  {
    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
      SibTr.entry(tc, "writeStartedFlush");

    String key = 
      SIMPUtils.getRemoteGetKey(stream.getRemoteMEUuid(), stream.getGatheringTargetDestUuid());
    StreamInfo sinfo = streamTable.get(key);
    if ((sinfo != null) && sinfo.streamId.equals(stream.streamId))
    {
      AOStartedFlushItem item = new AOStartedFlushItem(key, stream.streamId);
      Transaction msTran = mp.resolveAndEnlistMsgStoreTransaction(t);
      this.containerItemStream.addItem(item, msTran);

      if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
        SibTr.exit(tc, "writeStartedFlush", item);

      return item;
    }

    // this should not occur
    // log error and throw exception
    SIErrorException e = new SIErrorException(
      nls.getFormattedMessage(
        "INTERNAL_MESSAGING_ERROR_CWSIP0001",
        new Object[] { "com.ibm.ws.sib.processor.impl.AnycastOutputHandler", "1:2810:1.89.4.1" },
        null));

    // FFDC
    FFDCFilter.processException(
      e,
      "com.ibm.ws.sib.processor.impl.AnycastOutputHandler.writeStartedFlush",
      "1:2817:1.89.4.1",
      this);

    SibTr.exception(tc, e);
    SibTr.error(tc, "INTERNAL_MESSAGING_ERROR_CWSIP0001",
    new Object[] { "com.ibm.ws.sib.processor.impl.AnycastOutputHandler", "1:2822:1.89.4.1" });

    if (TraceComponent.isAnyTracingEnabled() && tc.isEntryEnabled())
      SibTr.exit(tc, "writeStartedFlush", e);
    throw e;
  }
675186
public void writeTo (@Nonnull @WillNotClose final OutputStream aOS) throws IOException
  {
    // we may only need to check if this is closed if we are working with a file
    // but we should force the habit of closing whether we are working with
    // a file or memory.
    if (!m_bClosed)
      throw new IOException ("This stream is not closed");

    if (isInMemory ())
    {
      m_aMemoryOS.writeTo (aOS);
    }
    else
    {
      final InputStream aIS = FileHelper.getInputStream (m_aOutputFile);
      StreamHelper.copyInputStreamToOutputStream (aIS, aOS);
    }
  }
1008872
public void encode(AsnOutputStream aos) throws EncodeException {

        if (this.nationalErrorCode == null && this.privateErrorCode == null)
            throw new EncodeException("Error code: No error code set!");

        try {
            if (this.type == ErrorCodeType.National && this.nationalErrorCode != null) {
                aos.writeInteger(Tag.CLASS_PRIVATE, ErrorCode._TAG_NATIONAL, this.nationalErrorCode);
            } else if (this.type == ErrorCodeType.Private && this.privateErrorCode != null) {
                aos.writeInteger(Tag.CLASS_PRIVATE, ErrorCode._TAG_PRIVATE, this.privateErrorCode);
            } else {
                throw new EncodeException("Error code: No error code set!");
            }

        } catch (IOException e) {
            throw new EncodeException("IOException while encoding ErrorCode: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding ErrorCode: " + e.getMessage(), e);
        }
    }
1158470
public byte[] write(Bean bean, boolean rootType) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
        try {
            write(bean, rootType, baos);
        } catch (IOException ex) {
            throw new IllegalStateException(ex);
        }
        return baos.toByteArray();
    }
831346
@Override
    protected int writeBytesWireFormat ( byte[] dst, int dstIndex ) {
        int start = dstIndex;

        if ( log.isDebugEnabled() ) {
            log.debug("Opening " + this.name);
        }

        SMBUtil.writeInt2(57, dst, dstIndex);
        dst[ dstIndex + 2 ] = this.securityFlags;
        dst[ dstIndex + 3 ] = this.requestedOplockLevel;
        dstIndex += 4;

        SMBUtil.writeInt4(this.impersonationLevel, dst, dstIndex);
        dstIndex += 4;
        SMBUtil.writeInt8(this.smbCreateFlags, dst, dstIndex);
        dstIndex += 8;
        dstIndex += 8; // Reserved

        SMBUtil.writeInt4(this.desiredAccess, dst, dstIndex);
        dstIndex += 4;
        SMBUtil.writeInt4(this.fileAttributes, dst, dstIndex);
        dstIndex += 4;
        SMBUtil.writeInt4(this.shareAccess, dst, dstIndex);
        dstIndex += 4;
        SMBUtil.writeInt4(this.createDisposition, dst, dstIndex);
        dstIndex += 4;
        SMBUtil.writeInt4(this.createOptions, dst, dstIndex);
        dstIndex += 4;

        int nameOffsetOffset = dstIndex;
        byte[] nameBytes = this.name.getBytes(StandardCharsets.UTF_16LE);
        SMBUtil.writeInt2(nameBytes.length, dst, dstIndex + 2);
        dstIndex += 4;

        int createContextOffsetOffset = dstIndex;
        dstIndex += 4; // createContextOffset
        int createContextLengthOffset = dstIndex;
        dstIndex += 4; // createContextLength

        SMBUtil.writeInt2(dstIndex - getHeaderStart(), dst, nameOffsetOffset);

        System.arraycopy(nameBytes, 0, dst, dstIndex, nameBytes.length);
        if ( nameBytes.length == 0 ) {
            // buffer must contain at least one byte
            dstIndex++;
        }
        else {
            dstIndex += nameBytes.length;
        }

        dstIndex += pad8(dstIndex);

        if ( this.createContexts == null || this.createContexts.length == 0 ) {
            SMBUtil.writeInt4(0, dst, createContextOffsetOffset);
        }
        else {
            SMBUtil.writeInt4(dstIndex - getHeaderStart(), dst, createContextOffsetOffset);
        }
        int totalCreateContextLength = 0;
        if ( this.createContexts != null ) {
            int lastStart = -1;
            for ( CreateContextRequest createContext : this.createContexts ) {
                int structStart = dstIndex;

                SMBUtil.writeInt4(0, dst, structStart); // Next
                if ( lastStart > 0 ) {
                    // set next pointer of previous CREATE_CONTEXT
                    SMBUtil.writeInt4(structStart - dstIndex, dst, lastStart);
                }

                dstIndex += 4;
                byte[] cnBytes = createContext.getName();
                int cnOffsetOffset = dstIndex;
                SMBUtil.writeInt2(cnBytes.length, dst, dstIndex + 2);
                dstIndex += 4;

                int dataOffsetOffset = dstIndex + 2;
                dstIndex += 4;
                int dataLengthOffset = dstIndex;
                dstIndex += 4;

                SMBUtil.writeInt2(dstIndex - structStart, dst, cnOffsetOffset);
                System.arraycopy(cnBytes, 0, dst, dstIndex, cnBytes.length);
                dstIndex += cnBytes.length;
                dstIndex += pad8(dstIndex);

                SMBUtil.writeInt2(dstIndex - structStart, dst, dataOffsetOffset);
                int len = createContext.encode(dst, dstIndex);
                SMBUtil.writeInt4(len, dst, dataLengthOffset);
                dstIndex += len;

                int pad = pad8(dstIndex);
                totalCreateContextLength += len + pad;
                dstIndex += pad;
                lastStart = structStart;
            }
        }
        SMBUtil.writeInt4(totalCreateContextLength, dst, createContextLengthOffset);
        return dstIndex - start;
    }
612648
public void encode(AsnOutputStream aos) throws EncodeException {

        if (invokeId == null)
            throw new EncodeException("No Invoke ID set.");

        try {
            aos.writeTag(Tag.CLASS_CONTEXT_SPECIFIC, false, _TAG);
            int pos = aos.StartContentDefiniteLength();

            aos.writeInteger(this.invokeId);

            if (this.operationCode != null && this.parameter != null) {
                aos.writeTag(Tag.CLASS_UNIVERSAL, false, Tag.SEQUENCE);
                int pos2 = aos.StartContentDefiniteLength();
                this.operationCode.encode(aos);
                this.parameter.encode(aos);
                aos.FinalizeContent(pos2);
            }

            aos.FinalizeContent(pos);

        } catch (IOException e) {
            throw new EncodeException("IOException while encoding ReturnResult: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding ReturnResult: " + e.getMessage(), e);
        }
    }
577389
@Override
    protected int writeBytesWireFormat ( byte[] dst, int dstIndex ) {
        int start = dstIndex;
        SMBUtil.writeInt2(33, dst, dstIndex);
        dst[ dstIndex + 2 ] = this.fileInformationClass;
        dst[ dstIndex + 3 ] = this.queryFlags;
        dstIndex += 4;
        SMBUtil.writeInt4(this.fileIndex, dst, dstIndex);
        dstIndex += 4;
        System.arraycopy(this.fileId, 0, dst, dstIndex, 16);
        dstIndex += 16;

        int fnOffsetOffset = dstIndex;
        int fnLengthOffset = dstIndex + 2;
        dstIndex += 4;

        SMBUtil.writeInt4(this.outputBufferLength, dst, dstIndex);
        dstIndex += 4;

        if ( this.fileName == null ) {
            SMBUtil.writeInt2(0, dst, fnOffsetOffset);
            SMBUtil.writeInt2(0, dst, fnLengthOffset);
        }
        else {
            byte[] fnBytes = this.fileName.getBytes(StandardCharsets.UTF_16LE);
            SMBUtil.writeInt2(dstIndex - getHeaderStart(), dst, fnOffsetOffset);
            SMBUtil.writeInt2(fnBytes.length, dst, fnLengthOffset);
            System.arraycopy(fnBytes, 0, dst, dstIndex, fnBytes.length);
            dstIndex += fnBytes.length;
        }
        return dstIndex - start;
    }
591493
public void encode(AsnOutputStream aos) throws EncodeException {

        if (this.getProblem() == null)
            throw new EncodeException("Error encoding Reject: RejectProblem must not be null");

        try {
            // tag
            aos.writeTag(Tag.CLASS_PRIVATE, false, Reject._TAG_REJECT);
            int pos = aos.StartContentDefiniteLength();

            // correlationId
            byte[] buf;
            if (this.correlationId != null) {
                buf = new byte[1];
                buf[0] = (byte) (long) this.correlationId;
            } else {
                buf = new byte[0];
            }
            aos.writeOctetString(Tag.CLASS_PRIVATE, Component._TAG_INVOKE_ID, buf);

            // rejectProblem
            aos.writeInteger(Tag.CLASS_PRIVATE, Reject._TAG_REJECT_PROBLEM, this.rejectProblem.getType());

            // Empty parameter
            aos.writeTag(Tag.CLASS_PRIVATE, false, Parameter._TAG_SEQUENCE);
            aos.writeLength(0);

            aos.FinalizeContent(pos);

        } catch (IOException e) {
            throw new EncodeException("IOException while encoding Reject: " + e.getMessage(), e);
        } catch (AsnException e) {
            throw new EncodeException("AsnException while encoding Reject: " + e.getMessage(), e);
        }
    }