Java IO Learning (ii) Introduction to Bytearrayoutputstream, source analysis and examples

Source: Internet
Author: User
Tags abstract constructor flush int size tostring

Introduction to Bytearrayoutputstream, source analysis and examples (including outputstream)

Learn Bytearrayinputstream in front, understand the "input stream." Next, we learn the output stream corresponding to the bytearrayinputstream, that is, the bytearrayoutputstream.

In this chapter, we will first introduce the Bytearrayoutputstream, after understanding its source code, then through the example to master how to use it.

Bytearrayoutputstream Introduction

Bytearrayoutputstream is a byte array output stream. It inherits from OutputStream.

The data in the Bytearrayoutputstream is written to a byte array. The buffer automatically grows as the data is being written. You can use Tobytearray () and toString () to get the data.

OutputStream Function List

Let's look at the function interface of Bytearrayoutputstream's parent class OutputStream.

Constructor
OutputStream () void close () void    flush ()
         void    write (byte[] buffer, int offset, int count)
         void    write (byte[] buffer)
abstract void    write (int onebyte)

Bytearrayoutputstream Function List

Constructor
Bytearrayoutputstream ()
bytearrayoutputstream (int size)
     
             void close    ()
synchronized void    Reset ()
             int     size ()
synchronized byte[]  tobytearray ()
             String  toString (int Hibyte)
             string  tostring (String charsetname)
             string  tostring ()
synchronized void    Write (byte[] buffer, int offset, int len)
synchronized void    write (int onebyte)
synchronized void    WriteTo (OutputStream out)

OutputStream and Bytearrayoutputstream Source analysis

OutputStream is the bytearrayoutputstream of the parent class, we first look at the source of OutputStream, and then learn Bytearrayoutputstream source code.

1. Outputstream.java source Analysis (based on jdk1.7.40)

 package java.io;
    Public abstract class OutputStream implements Closeable, flushable {//writes Byte B to output stream.
    It is implemented in subclasses!
     
    public abstract void Write (int b) throws IOException;
    Writes byte array b to the byte array output stream.
    public void Write (byte b[]) throws IOException {write (b, 0, b.length); //Writes byte array B to "byte array output stream" and off is "starting position of array B", Len is the length of writing written to public void write (byte b[, int off, int len) throws Ioex
        ception {if (b = = null) {throw new NullPointerException (); else if (Off < 0) | | (Off > B.length) | |
                   (Len < 0) | | ((off + len) > b.length) | | ((off + len) < 0))
        {throw new indexoutofboundsexception ();
        else if (len = = 0) {return;
        for (int i = 0; i < len; i++) {write (B[off + i]);
} public void Flush () throws IOException {} public void Close () throws IOException {} }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.