Network programming assignment for University
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

68 line
2.1 KiB

  1. package net.hoo2.auth.vmodem;
  2. import java.io.*;
  3. class Image {
  4. static final int IMAGE_BUFFER_SIZE = 128*1024;
  5. static final byte[] IMAGE_BEGIN = {(byte)0xFF, (byte)0xD8};
  6. static final byte[] IMAGE_END = {(byte)0xFF, (byte)0xD9};
  7. private Com com_;
  8. private Log log_;
  9. private Transaction transaction_;
  10. private String filename_;
  11. private int items_;
  12. private byte[] code_;
  13. Image (Com com, Log log, byte[] code, String filename, int items) {
  14. com_ = com;
  15. log_ = log;
  16. transaction_= new Transaction(null, new byte[IMAGE_BUFFER_SIZE]);
  17. filename_ = filename;
  18. items_ = items;
  19. code_ = code;
  20. }
  21. void code (byte[] code) { code_ = code; }
  22. void caption (byte[] code) {
  23. String line;
  24. line = "Running video decoder with: " + new String(code);
  25. log_.write(line, true);
  26. transaction_ = com_.request (transaction_, null, null, null, false);
  27. line = new String(transaction_.getResponse());
  28. log_.out(line);
  29. }
  30. void run () {
  31. String file, line;
  32. BufferedOutputStream ostream;
  33. for (int i =1 ; i<= items_ ; ++i) {
  34. if (items_>1)
  35. file = filename_ + "_" + i + ".jpg";
  36. else
  37. file = filename_ + ".jpg";
  38. line = new String(code_) + ": ";
  39. log_.write(line);
  40. transaction_ = com_.request(transaction_, code_, IMAGE_BEGIN, IMAGE_END, true);
  41. line = "File= " + file
  42. + " Tr= " + (transaction_.arrival - transaction_.departure) + " [msec]"
  43. + " Tt= " + (System.currentTimeMillis() - transaction_.departure) + " [msec]";
  44. log_.write(line);
  45. try {
  46. ostream = new BufferedOutputStream(new FileOutputStream(file));
  47. ostream.write(transaction_.response);
  48. ostream.flush();
  49. ostream.close();
  50. }
  51. catch (Exception exp) {
  52. System.err.println ("Error creating " + file + exp.getMessage());
  53. return;
  54. }
  55. }
  56. }
  57. }