I have simple java server and client .In the server, a file is broken into several chunks of byte array Now this byte arrays has to be send through object output stream. But if every time i use a new array to load file data that is perfect but if i use the same array(It is necessary i have to memory efficient) to load the file data client receives the same (first) byte array every time.
networkUtil read and write
public Object read() {
Object o = null;
try {
o=ois.readObject();
} catch (Exception e) {
//System.out.println("Reading Error in network : " + e.toString());
}
return o;
}
public void write(Object o) {
try {
oos.writeObject(o);
} catch (IOException e) {
System.out.println("Writing Error in network : " + e.toString());
}
}
Server writing portion
public void run() {
try {
//Scanner input=new Scanner(System.in);
byte []b =new byte[1000];
int num=5;
long i=0;
//ObjectOutputStream oosp = null;
for(int j=0;j<num;j++) {
File f=new File("G:\\photography\\DSC01020.JPG");
RandomAccessFile file1=new RandomAccessFile(f,"r");
long l=file1.length();
num=(int)Math.ceil((double)l/(double)1000);
//System.out.println("it is num "+num);
//file1.close();
// RandomAccessFile file=new RandomAccessFile(f,"r");
// byte [] b =new byte[1000];
System.out.println("seeking from "+i+"left "+(l-(j*1000)));
file1.seek(i);
file1.read(b);
file1.close();
System.out.println("it is first "+b[0]+" it is second "+b[1]);
nc.write(b);//network util
//oosp.write(b);
file1.close();
i+=1000;
}
client reading portion
try {
FileOutputStream fos=new FileOutputStream("C:\\Temp\\test.jpg");
byte []a;
for(int j=0;j<225;j++) {
Object o=nc.read();//netwotk util
if(o!= null) {
if(o instanceof Data) {
Data obj=(Data)o;
//System.out.println(obj.getElement());
}
if(o instanceof byte[])
{
//System.out.println("it is byte array");
a=(byte[])o;
System.out.println("it is first "+a[0]+" it is second "+a[1]);
if(j==224)// it is hard coded for this file i have to change this for all file
{
fos.write(a,0,203);
}
else {
fos.write(a);
}
}
}
}