Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void doRead() throws IOException {
- if (this.state.compareAndSet(State.NEW, State.RUNNING))
- // some checks before read and create Output Stream
- if (!prepareForRead()) return;
- if ((this.state.get() == State.RUNNING)
- || this.state.compareAndSet(State.SUSPENDED, State.RUNNING)) {
- try {
- // Integer.maxValue
- int executeCount = weight;
- do {
- if (failedBytes != null) {
- boolean isOfferSucceed;
- if (failedBytes == Constants.EOF) {
- isOfferSucceed = offerEOF();
- } else {
- isOfferSucceed = offerNextBlock(failedBytes, failedBytes.length);
- }
- if (isOfferSucceed) {
- failedBytes = null;
- } else {
- //suspend operation because failed bytes did not insert into queue
- suspend();
- }
- } else {
- //read from Is next readBlockSize bytes
- byte[] contents = new byte[readBlockSize];
- int readBytes = inputStream.read(contents);
- boolean isEOF = checkIsEndOfFile(readBytes);
- if (isEOF) {
- Timber.tag(TAG).i("Achieve end of file %s", srcFileItem.getName());
- //insert EOF = {}
- offerEOF();
- } else {
- if (readBytes < contents.length) {
- byte[] bytesCopied = Arrays.copyOf(contents, readBytes);
- offerNextBlock(bytesCopied, readBytes);
- } else {
- offerNextBlock(contents, readBytes);
- }
- }
- }
- executeCount--;
- } while (isExitCondition(executeCount));
- state.compareAndSet(State.RUNNING, State.SUSPENDED);
- } finally {
- if (getState() == State.COMPLETED)
- closeStreams();
- }
- }
- }
- // Offer EOF = {} into blocking queuq
- private boolean offerEOF() {
- boolean isAddSucceed = blockingQueue.offer(Constants.EOF);
- if (isAddSucceed) {
- Timber.tag(TAG).i("Read completed for the file %s", srcFileItem.getUri());
- state.set(State.COMPLETED);
- delegate.onReadSuccess();
- } else {
- failedBytes = Constants.EOF;
- suspend();
- }
- return isAddSucceed;
- }
- // Offer next read data into blocking queue
- boolean offerNextBlock(byte[] contents, int readBytes) {
- boolean isAddSucceed = blockingQueue.offer(contents);
- if (isAddSucceed) {
- //delegate on progress changed will call
- publishReadProgress(readBytes);
- } else {
- failedBytes = contents;
- suspend();
- }
- return isAddSucceed;
- }
- void createStream() throws IOException, USBException {
- // FileSystemService.getInputStream will be called
- inputStream = api.getFileSystemService().getInputStream(usbDevice, srcFileItem);
- // you should read and write in multiples of chunk size
- // return device.partitions.get(0).getFileSystem().getChunkSize()
- int chunkSize = storageLocation.getChunkSize();
- // readblockSize = minimum of (maximum of chunk size and file size) and
- // integer value from dividing the buffer size by chunk si
- readBlockSize = (int) Math.min(Math.max(chunkSize, srcFileItem.getLength()),
- preferences.getBufferSize() / chunkSize * chunkSize);
- Timber.tag(TAG).d("createStream: IS successfully created, readBlockSize = %s", readBlockSize);
- }
- void closeStreams() throws IOException {
- if (inputStream != null) {
- inputStream.close();
- inputStream = null;
- }
- }
- @Override
- public boolean suspend() {
- return this.state.compareAndSet(State.RUNNING, State.SUSPENDED);
- }
Advertisement
Add Comment
Please, Sign In to add comment