Wednesday 7 September 2016

Java Tutorial : Java IO (SequenceInputStream - Read from multiple ByteArrayInputStream)


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=FZ7VlYLifu4&list=UUhwKlOVR041tngjerWxVccw

SequenceInputStreamDemo.java
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;

/*
 * public SequenceInputStream(Enumeration<? extends
 *                                     InputStream> e)
 * 
 * Parameters: 
 * ----------- 
 * 
 * e - an enumeration of input streams.
 */

public class SequenceInputStreamDemo
{

    public static void main(String[] args) throws IOException
    {
        ByteArrayInputStream byteArrayInputStream1 = null;
        ByteArrayInputStream byteArrayInputStream2 = null;
        SequenceInputStream sequenceInputStream = null;

        try
        {
            String str1 = "Hello";
            String str2 = " Peter";
            byte[] byteArray1 = str1.getBytes();
            byte[] byteArray2 = str2.getBytes();
            
            byteArrayInputStream1 = new ByteArrayInputStream(byteArray1);
            byteArrayInputStream2 = new ByteArrayInputStream(byteArray2);

            Vector<ByteArrayInputStream> vector = new Vector<ByteArrayInputStream>();
            vector.add(byteArrayInputStream1);
            vector.add(byteArrayInputStream2);

            Enumeration<ByteArrayInputStream> enumeration = vector.elements();

            sequenceInputStream = new SequenceInputStream(enumeration);
            int i;
            while ((i = sequenceInputStream.read()) != -1)
            {
                System.out.print((char) i);
            }

        }
        finally
        {
            if (byteArrayInputStream1 != null)
            {
                byteArrayInputStream1.close();
            }
            if (byteArrayInputStream2 != null)
            {
                byteArrayInputStream2.close();
            }
            if (sequenceInputStream != null)
            {
                sequenceInputStream.close();
            }
        }
    }

}
Output
Hello Peter
Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_SequenceInputStream_Enumeration_BAIS_App.zip?attredirects=0&d=1
Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_SequenceInputStream_Enumeration_BAIS_App
Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/f08e719b5c34b35462be547882dfc81ea74a1c1e/BasicJava/JavaIODemo_SequenceInputStream_Enumeration_BAIS_App/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment