Advertisement
Guest User

Untitled

a guest
Aug 30th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1.  
  2. public class ReadCallable implements Callable<Integer> {
  3. byte[] mBuffer;
  4. int mBytesOffset;
  5. int mBytesCount;
  6. int mTimeout;
  7.  
  8. public ReadCallable (byte[] buffer, int bytesOffset, int bytesCount, int timeout) {
  9. mBuffer = buffer;
  10. mBytesOffset = bytesOffset;
  11. mBytesCount = bytesCount;
  12. mTimeout = timeout;
  13. }
  14.  
  15. @Override
  16. public Integer call() throws Exception {
  17. mStopWatch.start();
  18. while(mStopWatch.elapsedTime() < mTimeout) {
  19. try {
  20. int readBytes = mSocketInputStream.read(mBuffer, mBytesOffset, mBytesCount);
  21. if(readBytes > 0)
  22. return readBytes;
  23. } catch (Exception e) {
  24. }
  25. }
  26.  
  27. return -1;
  28. }
  29. }
  30.  
  31. int readData(byte[] buffer, int bytesOffset, int bytesCount, int timeout) {
  32.  
  33. Future<Integer> future = mExecutorService.submit(new ReadCallable(buffer, bytesOffset, bytesCount, timeout));
  34. try {
  35. return future.get(timeout + 50, TimeUnit.MILLISECONDS); // // 50 - 'overhead' time to call a task.
  36. } catch (Exception e) {
  37. return -1;
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement