Problems with ArrayList and File Transfer

I am creating a TFTP server. I have files for transferring files, but most files do not open when they arrive at the other end. This is due to the fact that the output of the ArrayList used to store bytes of files from each received packet adds a load of bytes to the beginning of the file. eg. "¬í sr java.util.ArrayListx ™ ™ Ça i sizexp w ur [B¬óøTà xp ü!". The reason for using the list is primarily because the im im server does not have the ability to tell the file size of the received file. Therefore, as far as I can tell, I can not use byte [], since this needs to be initialized with a given length. Is there any way around this?

            WRQ WRQ = new WRQ();
            ACK ACK = new ACK();
            DatagramPacket outPacket;
            byte[] bytes;
            byte[] fileOut;
            List fileBytes = new ArrayList();
            outPacket = WRQ.firstPacket(packet);
            socket.send(outPacket);

            socket.receive(packet);

            while (packet.getLength() == 516){

            bytes = WRQ.doWRQ(packet);
            fileBytes.add(bytes);

            outPacket = ACK.doACK(packet);
            socket.send(outPacket);

            socket.receive(packet); 

            }

            bytes = WRQ.doWRQ(packet);
            fileBytes.add(bytes);

            outPacket = ACK.doACK(packet);
            socket.send(outPacket);

            ObjectOutputStream os;

            ByteArrayOutputStream byteStream = new ByteArrayOutputStream(5000);
            os = new ObjectOutputStream(new BufferedOutputStream(byteStream));
            os.flush();
            os.writeObject(fileBytes);
            os.flush();
            byte[] outFile = byteStream.toByteArray();
            os.close();

            FileOutputStream foStream = new FileOutputStream(filename);
            foStream.write(outFile);
            foStream.close();
+3
source share
1 answer

ArrayList, ArrayList ByteArrayOutputStream, ObjectOutputStream, writeObject().

ArrayList . . : ArrayList, .

ArrayList. ByteArrayOutputStream FileOutputStream. ,

  • .

( ) ( BufferedOutputStream )

+4

All Articles