Customizing the Read () method in InputStream

Source: Internet
Author: User

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to working with you
Communication! ------
By learning about IO, we know that there is a very useful method in InputStream: the Read () method learns to find its underlying array, then think
To define a myread () to achieve the same effect.
The code is as follows:

Import java.io.*;
public class ByteCopyMp3
{
public static void Main (string[] args) throws IOException
{
Copy_1 ();
}
public static void Copy_1 () throws IOException
{
Mybufferedinputstream bufis=new Mybufferedinputstream (New FileInputStream ("D:\\kugou\\ Leslie"-When Love Has
Into the past "Farewell My Concubine". mp3 "));
Bufferedoutputstream bufos=new Bufferedoutputstream (New FileOutputStream ("D:\\pos.mp3"));
int by=0;
while ((By=bufis.myread ())!=-1)
{
Bufos.write (by);
}
Bufis.myclose ();
Bufos.close ();
}
}
Class Mybufferedinputstream
{
Private InputStream in;
Private byte[] By=new byte[1024];
private int pos=0,count=0;
Mybufferedinputstream (InputStream in)
{
This.in=in;
}
public int Myread () throws IOException
{
if (count==0)
{
Count=in.read (by);
if (count<0)
return-1;
pos=0;
BYTE B=by[pos];
pos++;
count--;
return b;
}
else if (count>0)
{
BYTE B=by[pos];
pos++;
count--;
return b;
}
return-1;
}
public void Myclose () throws IOException
{
In.close ();
}
}
Operation Result:
Original file properties:


Files after copying:

Comparison found: The original file is much larger than the copied file, indicating that no replication was successful. What is the problem?
By looking at the material: the original InputStream inside the Read method encapsulates a number of "& Operations" to ensure the integrity of the data.
Although the code is fine, the Read method we define is the return value of type int, but the byte array actually operates on bytes.
Like the return b above, where B is the byte type. Then the b&255 can be replaced with return B above to return b&255
Look at the results:
Image
Copy's done!


------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, looking forward to working with you
Communication! ------

Customizing the Read () method in InputStream

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.