Advertisement
kuza2010

[libaums]: UsbReadOperation class

Jan 21st, 2020
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. void doRead() throws IOException {
  2.     if (this.state.compareAndSet(State.NEW, State.RUNNING))
  3.         // some checks before read and create Output Stream
  4.         if (!prepareForRead()) return;
  5.  
  6.     if ((this.state.get() == State.RUNNING)
  7.             || this.state.compareAndSet(State.SUSPENDED, State.RUNNING)) {
  8.  
  9.         try {
  10.             // Integer.maxValue
  11.             int executeCount = weight;
  12.  
  13.             do {
  14.                 if (failedBytes != null) {
  15.                     boolean isOfferSucceed;
  16.                     if (failedBytes == Constants.EOF) {
  17.                         isOfferSucceed = offerEOF();
  18.                     } else {
  19.                         isOfferSucceed = offerNextBlock(failedBytes, failedBytes.length);
  20.                     }
  21.                     if (isOfferSucceed) {
  22.                         failedBytes = null;
  23.                     } else {
  24.                         //suspend operation because failed bytes did not insert into queue
  25.                         suspend();
  26.                     }
  27.                 } else {
  28.                     //read from Is next readBlockSize bytes
  29.                     byte[] contents = new byte[readBlockSize];
  30.                     int readBytes = inputStream.read(contents);
  31.                     boolean isEOF = checkIsEndOfFile(readBytes);
  32.  
  33.                     if (isEOF) {
  34.                         Timber.tag(TAG).i("Achieve end of file %s", srcFileItem.getName());
  35.                         //insert EOF = {}
  36.                         offerEOF();
  37.                     } else {
  38.                         if (readBytes < contents.length) {
  39.                             byte[] bytesCopied = Arrays.copyOf(contents, readBytes);
  40.                             offerNextBlock(bytesCopied, readBytes);
  41.                         } else {
  42.                             offerNextBlock(contents, readBytes);
  43.                         }
  44.                     }
  45.                 }
  46.                 executeCount--;
  47.             } while (isExitCondition(executeCount));
  48.             state.compareAndSet(State.RUNNING, State.SUSPENDED);
  49.         } finally {
  50.             if (getState() == State.COMPLETED)
  51.                 closeStreams();
  52.         }
  53.     }
  54. }
  55.  
  56. // Offer EOF = {} into blocking queuq
  57. private boolean offerEOF() {
  58.     boolean isAddSucceed = blockingQueue.offer(Constants.EOF);
  59.     if (isAddSucceed) {
  60.         Timber.tag(TAG).i("Read completed for the file %s", srcFileItem.getUri());
  61.         state.set(State.COMPLETED);
  62.         delegate.onReadSuccess();
  63.     } else {
  64.         failedBytes = Constants.EOF;
  65.         suspend();
  66.     }
  67.     return isAddSucceed;
  68. }
  69.  
  70. // Offer next read data into blocking queue
  71. boolean offerNextBlock(byte[] contents, int readBytes) {
  72.     boolean isAddSucceed = blockingQueue.offer(contents);
  73.     if (isAddSucceed) {
  74.         //delegate on progress changed will call
  75.         publishReadProgress(readBytes);
  76.     } else {
  77.         failedBytes = contents;
  78.         suspend();
  79.     }
  80.     return isAddSucceed;
  81. }
  82.  
  83. void createStream() throws IOException, USBException {
  84.     // FileSystemService.getInputStream will be called
  85.     inputStream = api.getFileSystemService().getInputStream(usbDevice, srcFileItem);
  86.     // you should read and write in multiples of chunk size
  87.     // return device.partitions.get(0).getFileSystem().getChunkSize()
  88.     int chunkSize = storageLocation.getChunkSize();
  89.  
  90.     // readblockSize = minimum of (maximum of chunk size and file size) and
  91.     // integer value from dividing the buffer size by chunk si
  92.     readBlockSize = (int) Math.min(Math.max(chunkSize, srcFileItem.getLength()),
  93.             preferences.getBufferSize() / chunkSize * chunkSize);
  94.     Timber.tag(TAG).d("createStream: IS successfully created, readBlockSize = %s", readBlockSize);
  95. }
  96.  
  97. void closeStreams() throws IOException {
  98.     if (inputStream != null) {
  99.         inputStream.close();
  100.         inputStream = null;
  101.     }
  102. }
  103.  
  104. @Override
  105. public boolean suspend() {
  106.     return this.state.compareAndSet(State.RUNNING, State.SUSPENDED);
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement