1

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);
                    }
                }
            }   
        }
enigma
  • 45
  • 1
  • 7

1 Answers1

0

Do you have memory problems ? In Java, the objects and arrays not in use are garbage collected. See Deleting An Entire Array. I don't think that you will enconter any problems by reallocating each time.

Edit:

Since the reallocation is the problem, maybe a ByteBuffer http://docs.oracle.com/javase/7/docs/api/java/nio/ByteBuffer.html can solve this.

You can try to use java.nio instead with a FileChannel and a ByteBuffer. See http://www.java2s.com/Tutorial/Java/0180__File/UseFileChannelandByteBuffertoCopyFile.htm and FileChannel ByteBuffer and Hashing Files for examples.

Community
  • 1
  • 1
JFPicard
  • 5,029
  • 3
  • 19
  • 43
  • @ JFPicard If i want to send a large file like 1 GB it is not possible to reallocate every time and JVM will throw an error if i want to allocate such large memory. – enigma Nov 30 '15 at 13:45
  • @ JFPicard Will you elaborate a little bit?? Should i use a Bytebuffer to load data from file or load data in byte array than make a Bytebuffer with it and send that Bytebuffer object through network. – enigma Nov 30 '15 at 14:26