D:\Sam\InterviewsMaterial\SiteWorx\SiteWorxTest\src\java\multithread_reader_services\UpdateTargetTable.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 UpdateTargetTable 
17 {
18     WriterClass     writerClassHandle = null;
19     ReaderClass     readerClassHandle = null;
20     /** Creates a new instance of UpdateTargetTable */
21     public UpdateTargetTable(int startIndex, int lastIndex ,String streamName) 
22     {
23         readerClassHandle = new ReaderClass(streamName);
24         writerClassHandle = new WriterClass(startIndex, lastIndex);
25     }
26     /**
27      *
28      */
29     public int updateTable(byte [] targetByteArray)
30     {
31         BufferedInputStream bufferedInputStreamHandle = readerClassHandle.getInputSteam();
32         byte [] localReadData = null;
33         int errorResult = MultithreadConstants.SUCCESS;
34         try
35         {
36              localReadData = readerClassHandle.read10bytes(bufferedInputStreamHandle);
37              while(     (localReadData != null)
38                     &&  (errorResult >= MultithreadConstants.SUCCESS)
39              )
40              {
41                  errorResult = writerClassHandle.write10bytes(targetByteArray, localReadData);
42                  localReadData = readerClassHandle.read10bytes(bufferedInputStreamHandle);
43 //                 for(int index = 0; index < localReadData.length; index++)
44 //                 System.out.println("" + index + " = " + localReadData[index] + " " + (char)localReadData[index]);
45              }
46          }
47          catch(IOException myIOException)
48          {
49                 myIOException.printStackTrace();
50                 errorResult = MultithreadConstants.ERROR;
51          }        
52         closeStream();
53         return(errorResult);
54     }
55     /**
56      *
57      */
58     public void closeStream()
59     {
60         readerClassHandle.closeStream();
61     }
62     /**
63      *
64      */
65      public static void main (String[] args)
66     {
67          byte [] targetByteArray = new byte[200];
68          
69          UpdateTargetTable updateTargetTableHandle 
70                  = new UpdateTargetTable(0, 99, MultithreadConstants.FILE_PATH + MultithreadConstants.FILE_G_NAME);
71          int result = updateTargetTableHandle.updateTable(targetByteArray);
72          
73     }    
74 }
75