Advertisement
Guest User

Untitled

a guest
Nov 25th, 2014
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. public Sender(Socket socket) {
  2.  
  3. List<File> files = new ArrayList<File>();
  4. files.add(new File(Directory.getDataPath("default.docx")));
  5. files.add(new File(Directory.getDataPath("database.db")));
  6.  
  7. try {
  8. BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
  9. DataOutputStream dos = new DataOutputStream(bos);
  10. dos.writeInt(files.size());
  11. for (File file : files) {
  12. dos.writeLong(file.length());
  13. dos.writeUTF(file.getName());
  14. FileInputStream fis = new FileInputStream(file);
  15. BufferedInputStream bis = new BufferedInputStream(fis);
  16. int theByte = 0;
  17. while ((theByte = bis.read()) != -1) {
  18. bos.write(theByte);
  19. }
  20. bis.close();
  21. }
  22. dos.close(); // If this is disabled, the program won't work.
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. }
  27.  
  28. public static byte[] document;
  29.  
  30. public Downloader(Socket socket) {
  31. try {
  32. BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
  33. DataInputStream dis = new DataInputStream(bis);
  34. int filesCount = dis.readInt();
  35. for (int i = 0; i < filesCount; i++) {
  36. long size = dis.readLong();
  37. String fileName = dis.readUTF();
  38. if (fileName.equals("database.db")) {
  39. List<String> data = new ArrayList<String>();
  40. BufferedReader reader = new BufferedReader(new InputStreamReader(bis));
  41. String line;
  42. while ((line = reader.readLine()) != null) {
  43. if (line.trim().length() > 0) {
  44. data.add(line);
  45. }
  46. }
  47. reader.close();
  48. parse(data);
  49. } else if (fileName.equals("default.docx")) {
  50. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  51. for (int x = 0; x < size; x++) {
  52. bos.write(bis.read());
  53. }
  54. bos.close();
  55. document = bos.toByteArray();
  56. }
  57. }
  58. //dis.close();
  59. } catch (Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. long total = 0;
  65. while ((total < size && (count = in.read(buffer, 0, size-total > buffer.length ? buffer.length : (int)(size-total))) > 0)
  66. {
  67. total += count;
  68. out.write(buffer, 0, count);
  69. }
  70. out.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement