Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Sender(Socket socket) {
- List<File> files = new ArrayList<File>();
- files.add(new File(Directory.getDataPath("default.docx")));
- files.add(new File(Directory.getDataPath("database.db")));
- try {
- BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
- DataOutputStream dos = new DataOutputStream(bos);
- dos.writeInt(files.size());
- for (File file : files) {
- dos.writeLong(file.length());
- dos.writeUTF(file.getName());
- FileInputStream fis = new FileInputStream(file);
- BufferedInputStream bis = new BufferedInputStream(fis);
- int theByte = 0;
- while ((theByte = bis.read()) != -1) {
- bos.write(theByte);
- }
- bis.close();
- }
- dos.close(); // If this is disabled, the program won't work.
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public static byte[] document;
- public Downloader(Socket socket) {
- try {
- BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
- DataInputStream dis = new DataInputStream(bis);
- int filesCount = dis.readInt();
- for (int i = 0; i < filesCount; i++) {
- long size = dis.readLong();
- String fileName = dis.readUTF();
- if (fileName.equals("database.db")) {
- List<String> data = new ArrayList<String>();
- BufferedReader reader = new BufferedReader(new InputStreamReader(bis));
- String line;
- while ((line = reader.readLine()) != null) {
- if (line.trim().length() > 0) {
- data.add(line);
- }
- }
- reader.close();
- parse(data);
- } else if (fileName.equals("default.docx")) {
- ByteArrayOutputStream bos = new ByteArrayOutputStream();
- for (int x = 0; x < size; x++) {
- bos.write(bis.read());
- }
- bos.close();
- document = bos.toByteArray();
- }
- }
- //dis.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- long total = 0;
- while ((total < size && (count = in.read(buffer, 0, size-total > buffer.length ? buffer.length : (int)(size-total))) > 0)
- {
- total += count;
- out.write(buffer, 0, count);
- }
- out.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement