Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ReadCallable implements Callable<Integer> {
- byte[] mBuffer;
- int mBytesOffset;
- int mBytesCount;
- int mTimeout;
- public ReadCallable (byte[] buffer, int bytesOffset, int bytesCount, int timeout) {
- mBuffer = buffer;
- mBytesOffset = bytesOffset;
- mBytesCount = bytesCount;
- mTimeout = timeout;
- }
- @Override
- public Integer call() throws Exception {
- mStopWatch.start();
- while(mStopWatch.elapsedTime() < mTimeout) {
- try {
- int readBytes = mSocketInputStream.read(mBuffer, mBytesOffset, mBytesCount);
- if(readBytes > 0)
- return readBytes;
- } catch (Exception e) {
- }
- }
- return -1;
- }
- }
- int readData(byte[] buffer, int bytesOffset, int bytesCount, int timeout) {
- Future<Integer> future = mExecutorService.submit(new ReadCallable(buffer, bytesOffset, bytesCount, timeout));
- try {
- return future.get(timeout + 50, TimeUnit.MILLISECONDS); // // 50 - 'overhead' time to call a task.
- } catch (Exception e) {
- return -1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement