Java by API/java.io/ByteArrayInputStream

Материал из Java эксперт
Перейти к: навигация, поиск

ByteArrayInputStream: available()

   <source lang="java">

import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class Main {

 public static void main(String args[]) throws IOException {
   ByteArrayOutputStream outStream = new ByteArrayOutputStream();
   String s = "This is a test.";
   for (int i = 0; i < s.length(); ++i)
     outStream.write(s.charAt(i));
   System.out.println("outstream: " + outStream);
   System.out.println("size: " + outStream.size());
   ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
   int inBytes = inStream.available();
   System.out.println("inStream has " + inBytes + " available bytes");
   byte inBuf[] = new byte[inBytes];
   int bytesRead = inStream.read(inBuf, 0, inBytes);
   System.out.println(bytesRead + " bytes were read");
   System.out.println("They are: " + new String(inBuf));
 }

}

 </source>
   
  
 
  



ByteArrayInputStream: read(byte[] b, int off, int len)

   <source lang="java">

import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class Main {

 public static void main(String args[]) throws IOException {
   ByteArrayOutputStream outStream = new ByteArrayOutputStream();
   String s = "This is a test.";
   for (int i = 0; i < s.length(); ++i)
     outStream.write(s.charAt(i));
   System.out.println("outstream: " + outStream);
   System.out.println("size: " + outStream.size());
   ByteArrayInputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
   int inBytes = inStream.available();
   System.out.println("inStream has " + inBytes + " available bytes");
   byte inBuf[] = new byte[inBytes];
   int bytesRead = inStream.read(inBuf, 0, inBytes);
   System.out.println(bytesRead + " bytes were read");
   System.out.println("They are: " + new String(inBuf));
 }

}

 </source>
   
  
 
  



ByteArrayInputStream: reset()

   <source lang="java">

import java.io.ByteArrayInputStream; import java.io.IOException; public class Main {

 public static void main(String args[]) throws IOException {
   String tmp = "abc";
   byte b[] = tmp.getBytes();
   ByteArrayInputStream in = new ByteArrayInputStream(b);
   for (int i = 0; i < 2; i++) {
     int c;
     while ((c = in.read()) != -1) {
       if (i == 0) {
         System.out.print((char) c);
       } else {
         System.out.print(Character.toUpperCase((char) c));
       }
     }
     System.out.println();
     in.reset();
   }
 }

}

 </source>
   
  
 
  



new ByteArrayInputStream(byte[] buf)

   <source lang="java">
 

import java.io.ByteArrayInputStream; import java.io.DataInputStream; import java.io.EOFException; import java.io.IOException; public class Main {

 public static void main(String[] args) throws IOException {
   try {
     DataInputStream in3 = new DataInputStream(
       new ByteArrayInputStream("a dbcde".getBytes()));
     while(true)
       System.out.print((char)in3.readByte());
   } catch(EOFException e) {
     System.err.println("End of stream");
   }
 }

}


 </source>
   
  
 
  



new ByteArrayInputStream(byte[] buf, int offset, int length)

   <source lang="java">

import java.io.ByteArrayInputStream; import java.io.IOException; public class Main {

 public static void main(String args[]) throws IOException {
   String tmp = "abcdefghijklmnopqrstuvwxyz";
   byte b[] = tmp.getBytes();
   ByteArrayInputStream input1 = new ByteArrayInputStream(b);
   ByteArrayInputStream input2 = new ByteArrayInputStream(b, 0, 3);
 }

}

 </source>