D:\Sam\InterviewsMaterial\SiteWorx\SiteWorxTest\src\java\multithread_reader_services\ReaderClass.java
 1 /*
 2  * To change this template, choose Tools | Templates
 3  * and open the template in the editor.
 4  */
 5 package multithread_reader_services;
 6 
 7 import  java.io.*;
 8 import java.text.*;
 9 import java.lang.*;
10 import java.util.*;
11 
12 /**
13  *
14  * @author sameldin
15  */
16 public class ReaderClass 
17 {
18         private String streamName;
19         BufferedInputStream bufferedInputStreamHandle = null; 
20         
21         public ReaderClass(String name)  
22         {
23                 this.streamName = name;
24         }
25         /*
26          *
27          */
28         public byte[] read10bytes(BufferedInputStream inputStream) throws IOException
29         {
30             if(null == inputStream)
31                 return(null);
32             
33             byte [] localReadData = new byte[MultithreadConstants.MAX_SIZE];
34             
35             int count;
36             count = inputStream.read(localReadData,0, MultithreadConstants.MAX_SIZE);
37             if(-1 == count)
38                 return(null);
39             else
40                 //reads 10 bytes and returns only when all 10 bytes have been filled.
41                 return(localReadData);
42         }
43         /*
44          *
45          */
46          public BufferedInputStream getInputSteam()
47          {
48              try
49              {
50                 FileInputStream localFile = new FileInputStream(streamName);
51                 bufferedInputStreamHandle = new BufferedInputStream(localFile);
52              }
53              catch(IOException myIOException)
54              {
55                 myIOException.printStackTrace();
56                 return(null);
57              }
58              return(bufferedInputStreamHandle);
59          }
60     /**
61      *
62      */
63     public void closeStream()
64     {
65             try
66              {
67                 bufferedInputStreamHandle.close();
68              }
69              catch(IOException myIOException)
70              {
71                 myIOException.printStackTrace();
72              }
73     }
74     /**
75      *
76      */
77      public static void main (String[] args)
78     {
79          ReaderClass readerClassHandle = new ReaderClass(MultithreadConstants.FILE_PATH + MultithreadConstants.FILE_A_NAME);
80          BufferedInputStream bufferedInputStreamHandle = readerClassHandle.getInputSteam();
81          byte [] localReadData = null;
82          try
83          {
84              localReadData = readerClassHandle.read10bytes(bufferedInputStreamHandle);
85              if(localReadData != null)
86              {
87                  for(int index = 0; index < localReadData.length; index++)
88                  System.out.println("" + index + " = " + localReadData[index] + " " + (char)localReadData[index]);
89              }
90          }
91          catch(IOException myIOException)
92          {
93                 myIOException.printStackTrace();
94          }
95          
96     }
97 }