blob: faa968ac3fcad71e3123e03083f97ea0be7f9d41 [file] [log] [blame]
DRCc5dc0382011-05-13 21:42:14 +00001/* 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
23package com.tigervnc.rdr;
24
25public class JavaInStream extends InStream {
26
27 static final int defaultBufSize = 8192;
28 static final int minBulkSize = 1024;
29
Brian Hinze7f53dc2011-12-04 22:58:48 +000030 public JavaInStream(java.io.InputStream jis_, int bufSize_) {
31 jis = jis_;
DRCc5dc0382011-05-13 21:42:14 +000032 bufSize = bufSize_;
33 b = new byte[bufSize];
Brian Hinze7f53dc2011-12-04 22:58:48 +000034 ptr = end = offset = 0;
Brian Hinzad78f752011-10-30 14:08:29 +000035 timing = false;
DRCc5dc0382011-05-13 21:42:14 +000036 timeWaitedIn100us = 5;
37 timedKbits = 0;
38 }
39
Brian Hinze7f53dc2011-12-04 22:58:48 +000040 public JavaInStream(java.io.InputStream jis_) { this(jis_, defaultBufSize); }
DRCc5dc0382011-05-13 21:42:14 +000041
Brian Hinze7f53dc2011-12-04 22:58:48 +000042 public void readBytes(byte[] data, int dataPtr, int length) {
DRCc5dc0382011-05-13 21:42:14 +000043 if (length < minBulkSize) {
Brian Hinzb898b882011-10-02 02:40:37 +000044 super.readBytes(data, dataPtr, length);
DRCc5dc0382011-05-13 21:42:14 +000045 return;
46 }
47
48 int n = end - ptr;
49 if (n > length) n = length;
50
Brian Hinzb898b882011-10-02 02:40:37 +000051 System.arraycopy(b, ptr, data, dataPtr, n);
52 dataPtr += n;
DRCc5dc0382011-05-13 21:42:14 +000053 length -= n;
54 ptr += n;
55
56 while (length > 0) {
Brian Hinzb898b882011-10-02 02:40:37 +000057 n = read(data, dataPtr, length);
58 dataPtr += n;
DRCc5dc0382011-05-13 21:42:14 +000059 length -= n;
Brian Hinzb898b882011-10-02 02:40:37 +000060 offset += n;
DRCc5dc0382011-05-13 21:42:14 +000061 }
62 }
63
Brian Hinze7f53dc2011-12-04 22:58:48 +000064 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 Hinz2c5b44c2011-06-17 13:00:58 +000089 protected int overrun(int itemSize, int nItems, boolean wait) {
DRCc5dc0382011-05-13 21:42:14 +000090 if (itemSize > bufSize)
91 throw new Exception("JavaInStream overrun: max itemSize exceeded");
92
93 if (end - ptr != 0)
Brian Hinze7f53dc2011-12-04 22:58:48 +000094 System.arraycopy(b, ptr, b, 0, end - ptr);
DRCc5dc0382011-05-13 21:42:14 +000095
Brian Hinze7f53dc2011-12-04 22:58:48 +000096 offset += ptr;
97 end -= ptr;
98 ptr = 0;
DRCc5dc0382011-05-13 21:42:14 +000099
Brian Hinze7f53dc2011-12-04 22:58:48 +0000100 while (end < itemSize) {
101 int bytes_to_read = bufSize - end;
Brian Hinz13dbd6b2011-11-27 20:43:47 +0000102
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 Hinze7f53dc2011-12-04 22:58:48 +0000108
DRCc5dc0382011-05-13 21:42:14 +0000109 end += n;
110 }
111
Brian Hinze7f53dc2011-12-04 22:58:48 +0000112 if (itemSize * nItems > end)
113 nItems = end / itemSize;
DRCc5dc0382011-05-13 21:42:14 +0000114
115 return nItems;
116 }
117
Brian Hinzb898b882011-10-02 02:40:37 +0000118 private int read(byte[] buf, int bufPtr, int len, boolean wait) {
DRCc5dc0382011-05-13 21:42:14 +0000119 long before = 0;
120 if (timing)
Brian Hinza5f0fc82011-10-24 02:14:55 +0000121 before = System.nanoTime();
DRCc5dc0382011-05-13 21:42:14 +0000122
Brian Hinzad78f752011-10-30 14:08:29 +0000123 int n = -1;
124 try {
Brian Hinze7f53dc2011-12-04 22:58:48 +0000125 n = jis.read(buf, bufPtr, len);
Brian Hinzad78f752011-10-30 14:08:29 +0000126 } catch (java.io.IOException e) {
127 throw new IOException(e);
128 }
129
DRCc5dc0382011-05-13 21:42:14 +0000130 if (n < 0) throw new EndOfStream();
Brian Hinz13dbd6b2011-11-27 20:43:47 +0000131 if (n == 0) return 0;
DRCc5dc0382011-05-13 21:42:14 +0000132
133 if (timing) {
Brian Hinza5f0fc82011-10-24 02:14:55 +0000134 long after = System.nanoTime();
135 long newTimeWaited = (after - before) / 100000;
DRCc5dc0382011-05-13 21:42:14 +0000136 int newKbits = n * 8 / 1000;
137
138 // limit rate to between 10kbit/s and 40Mbit/s
139
Brian Hinzad78f752011-10-30 14:08:29 +0000140 if (newTimeWaited > newKbits*1000) {
141 newTimeWaited = newKbits*1000;
142 } else if (newTimeWaited < newKbits/4) {
143 newTimeWaited = newKbits/4;
144 }
DRCc5dc0382011-05-13 21:42:14 +0000145
146 timeWaitedIn100us += newTimeWaited;
147 timedKbits += newKbits;
148 }
149
150 return n;
151
DRCc5dc0382011-05-13 21:42:14 +0000152 }
Brian Hinze7f53dc2011-12-04 22:58:48 +0000153 private int read(byte[] buf, int bufPtr, int len) { return read(buf, bufPtr, len, true); }
DRCc5dc0382011-05-13 21:42:14 +0000154
Brian Hinze7f53dc2011-12-04 22:58:48 +0000155 private java.io.InputStream jis;
Brian Hinzb898b882011-10-02 02:40:37 +0000156 private int offset;
DRCc5dc0382011-05-13 21:42:14 +0000157 private int bufSize;
158
159 boolean timing;
160 long timeWaitedIn100us;
161 long timedKbits;
162}