DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // |
| 20 | // A JavaInStream reads from a java.io.InputStream |
| 21 | // |
| 22 | |
| 23 | package com.tigervnc.rdr; |
| 24 | |
| 25 | public class JavaInStream extends InStream { |
| 26 | |
| 27 | static final int defaultBufSize = 8192; |
| 28 | static final int minBulkSize = 1024; |
| 29 | |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 30 | public JavaInStream(java.io.InputStream jis_, int bufSize_) { |
| 31 | jis = jis_; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 32 | bufSize = bufSize_; |
| 33 | b = new byte[bufSize]; |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 34 | ptr = end = offset = 0; |
Brian Hinz | ad78f75 | 2011-10-30 14:08:29 +0000 | [diff] [blame] | 35 | timing = false; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 36 | timeWaitedIn100us = 5; |
| 37 | timedKbits = 0; |
| 38 | } |
| 39 | |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 40 | public JavaInStream(java.io.InputStream jis_) { this(jis_, defaultBufSize); } |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 41 | |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 42 | public void readBytes(byte[] data, int dataPtr, int length) { |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 43 | if (length < minBulkSize) { |
Brian Hinz | b898b88 | 2011-10-02 02:40:37 +0000 | [diff] [blame] | 44 | super.readBytes(data, dataPtr, length); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 45 | return; |
| 46 | } |
| 47 | |
| 48 | int n = end - ptr; |
| 49 | if (n > length) n = length; |
| 50 | |
Brian Hinz | b898b88 | 2011-10-02 02:40:37 +0000 | [diff] [blame] | 51 | System.arraycopy(b, ptr, data, dataPtr, n); |
| 52 | dataPtr += n; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 53 | length -= n; |
| 54 | ptr += n; |
| 55 | |
| 56 | while (length > 0) { |
Brian Hinz | b898b88 | 2011-10-02 02:40:37 +0000 | [diff] [blame] | 57 | n = read(data, dataPtr, length); |
| 58 | dataPtr += n; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 59 | length -= n; |
Brian Hinz | b898b88 | 2011-10-02 02:40:37 +0000 | [diff] [blame] | 60 | offset += n; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 64 | public int pos() { return offset + ptr; } |
| 65 | |
| 66 | public void startTiming() { |
| 67 | timing = true; |
| 68 | |
| 69 | // Carry over up to 1s worth of previous rate for smoothing. |
| 70 | |
| 71 | if (timeWaitedIn100us > 10000) { |
| 72 | timedKbits = timedKbits * 10000 / timeWaitedIn100us; |
| 73 | timeWaitedIn100us = 10000; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | public void stopTiming() { |
| 78 | timing = false; |
| 79 | if (timeWaitedIn100us < timedKbits/2) |
| 80 | timeWaitedIn100us = timedKbits/2; // upper limit 20Mbit/s |
| 81 | } |
| 82 | |
| 83 | public long kbitsPerSecond() { |
| 84 | return timedKbits * 10000 / timeWaitedIn100us; |
| 85 | } |
| 86 | |
| 87 | public long timeWaited() { return timeWaitedIn100us; } |
| 88 | |
Brian Hinz | 2c5b44c | 2011-06-17 13:00:58 +0000 | [diff] [blame] | 89 | protected int overrun(int itemSize, int nItems, boolean wait) { |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 90 | if (itemSize > bufSize) |
| 91 | throw new Exception("JavaInStream overrun: max itemSize exceeded"); |
| 92 | |
| 93 | if (end - ptr != 0) |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 94 | System.arraycopy(b, ptr, b, 0, end - ptr); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 95 | |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 96 | offset += ptr; |
| 97 | end -= ptr; |
| 98 | ptr = 0; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 99 | |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 100 | while (end < itemSize) { |
| 101 | int bytes_to_read = bufSize - end; |
Brian Hinz | 13dbd6b | 2011-11-27 20:43:47 +0000 | [diff] [blame] | 102 | |
| 103 | if (!timing) { |
| 104 | bytes_to_read = Math.min(bytes_to_read, Math.max(itemSize*nItems, 8)); |
| 105 | } |
| 106 | |
| 107 | int n = read(b, end, bytes_to_read, wait); |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 108 | |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 109 | end += n; |
| 110 | } |
| 111 | |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 112 | if (itemSize * nItems > end) |
| 113 | nItems = end / itemSize; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 114 | |
| 115 | return nItems; |
| 116 | } |
| 117 | |
Brian Hinz | b898b88 | 2011-10-02 02:40:37 +0000 | [diff] [blame] | 118 | private int read(byte[] buf, int bufPtr, int len, boolean wait) { |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 119 | long before = 0; |
| 120 | if (timing) |
Brian Hinz | a5f0fc8 | 2011-10-24 02:14:55 +0000 | [diff] [blame] | 121 | before = System.nanoTime(); |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 122 | |
Brian Hinz | ad78f75 | 2011-10-30 14:08:29 +0000 | [diff] [blame] | 123 | int n = -1; |
| 124 | try { |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 125 | n = jis.read(buf, bufPtr, len); |
Brian Hinz | ad78f75 | 2011-10-30 14:08:29 +0000 | [diff] [blame] | 126 | } catch (java.io.IOException e) { |
| 127 | throw new IOException(e); |
| 128 | } |
| 129 | |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 130 | if (n < 0) throw new EndOfStream(); |
Brian Hinz | 13dbd6b | 2011-11-27 20:43:47 +0000 | [diff] [blame] | 131 | if (n == 0) return 0; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 132 | |
| 133 | if (timing) { |
Brian Hinz | a5f0fc8 | 2011-10-24 02:14:55 +0000 | [diff] [blame] | 134 | long after = System.nanoTime(); |
| 135 | long newTimeWaited = (after - before) / 100000; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 136 | int newKbits = n * 8 / 1000; |
| 137 | |
| 138 | // limit rate to between 10kbit/s and 40Mbit/s |
| 139 | |
Brian Hinz | ad78f75 | 2011-10-30 14:08:29 +0000 | [diff] [blame] | 140 | if (newTimeWaited > newKbits*1000) { |
| 141 | newTimeWaited = newKbits*1000; |
| 142 | } else if (newTimeWaited < newKbits/4) { |
| 143 | newTimeWaited = newKbits/4; |
| 144 | } |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 145 | |
| 146 | timeWaitedIn100us += newTimeWaited; |
| 147 | timedKbits += newKbits; |
| 148 | } |
| 149 | |
| 150 | return n; |
| 151 | |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 152 | } |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 153 | private int read(byte[] buf, int bufPtr, int len) { return read(buf, bufPtr, len, true); } |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 154 | |
Brian Hinz | e7f53dc | 2011-12-04 22:58:48 +0000 | [diff] [blame] | 155 | private java.io.InputStream jis; |
Brian Hinz | b898b88 | 2011-10-02 02:40:37 +0000 | [diff] [blame] | 156 | private int offset; |
DRC | c5dc038 | 2011-05-13 21:42:14 +0000 | [diff] [blame] | 157 | private int bufSize; |
| 158 | |
| 159 | boolean timing; |
| 160 | long timeWaitedIn100us; |
| 161 | long timedKbits; |
| 162 | } |