annotate src/myVncProxy/SessionRecorder.java @ 191:b2f0cd0cff6c default tip

Added tag Version-1.0 for changeset 79046b4e5990
author Yu Taninari <you@cr.ie.u-ryukyu.ac.jp>
date Tue, 29 Nov 2011 15:52:44 +0900
parents 87b29d6039a6
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
24
87b29d6039a6 add package myVncProxy
e085711
parents: 0
diff changeset
1 package myVncProxy;
0
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
2 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
3 // Copyright (C) 2002 Constantin Kaplinsky. All Rights Reserved.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
4 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
5 // This is free software; you can redistribute it and/or modify
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
6 // it under the terms of the GNU General Public License as published by
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
7 // the Free Software Foundation; either version 2 of the License, or
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
8 // (at your option) any later version.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
9 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
10 // This software is distributed in the hope that it will be useful,
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
13 // GNU General Public License for more details.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
14 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
15 // You should have received a copy of the GNU General Public License
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
16 // along with this software; if not, write to the Free Software
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
18 // USA.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
19 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
20
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
21 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
22 // SessionRecorder is a class to write FBS (FrameBuffer Stream) files.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
23 // FBS files are used to save RFB sessions for later playback.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
24 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
25
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
26 import java.io.*;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
27
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
28 class SessionRecorder {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
29
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
30 protected FileOutputStream f;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
31 protected DataOutputStream df;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
32 protected long startTime, lastTimeOffset;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
33
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
34 protected byte[] buffer;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
35 protected int bufferSize;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
36 protected int bufferBytes;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
37
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
38 public SessionRecorder(String name, int bufsize) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
39 f = new FileOutputStream(name);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
40 df = new DataOutputStream(f);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
41 startTime = System.currentTimeMillis();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
42 lastTimeOffset = 0;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
43
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
44 bufferSize = bufsize;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
45 bufferBytes = 0;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
46 buffer = new byte[bufferSize];
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
47 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
48
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
49 public SessionRecorder(String name) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
50 this(name, 65536);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
51 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
52
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
53 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
54 // Close the file, free resources.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
55 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
56
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
57 public void close() throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
58 try {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
59 flush();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
60 } catch (IOException e) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
61 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
62
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
63 df = null;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
64 f.close();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
65 f = null;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
66 buffer = null;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
67 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
68
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
69 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
70 // Write the FBS file header as defined in the rfbproxy utility.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
71 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
72
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
73 public void writeHeader() throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
74 df.write("FBS 001.000\n".getBytes());
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
75 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
76
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
77 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
78 // Write one byte.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
79 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
80
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
81 public void writeByte(int b) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
82 prepareWriting();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
83 buffer[bufferBytes++] = (byte)b;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
84 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
85
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
86 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
87 // Write 16-bit value, big-endian.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
88 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
89
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
90 public void writeShortBE(int v) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
91 prepareWriting();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
92 buffer[bufferBytes++] = (byte)(v >> 8);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
93 buffer[bufferBytes++] = (byte)v;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
94 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
95
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
96 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
97 // Write 32-bit value, big-endian.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
98 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
99
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
100 public void writeIntBE(int v) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
101 prepareWriting();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
102 buffer[bufferBytes] = (byte)(v >> 24);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
103 buffer[bufferBytes + 1] = (byte)(v >> 16);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
104 buffer[bufferBytes + 2] = (byte)(v >> 8);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
105 buffer[bufferBytes + 3] = (byte)v;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
106 bufferBytes += 4;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
107 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
108
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
109 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
110 // Write 16-bit value, little-endian.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
111 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
112
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
113 public void writeShortLE(int v) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
114 prepareWriting();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
115 buffer[bufferBytes++] = (byte)v;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
116 buffer[bufferBytes++] = (byte)(v >> 8);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
117 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
118
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
119 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
120 // Write 32-bit value, little-endian.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
121 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
122
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
123 public void writeIntLE(int v) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
124 prepareWriting();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
125 buffer[bufferBytes] = (byte)v;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
126 buffer[bufferBytes + 1] = (byte)(v >> 8);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
127 buffer[bufferBytes + 2] = (byte)(v >> 16);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
128 buffer[bufferBytes + 3] = (byte)(v >> 24);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
129 bufferBytes += 4;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
130 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
131
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
132 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
133 // Write byte arrays.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
134 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
135
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
136 public void write(byte b[], int off, int len) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
137 prepareWriting();
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
138 while (len > 0) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
139 if (bufferBytes > bufferSize - 4)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
140 flush(false);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
141
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
142 int partLen;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
143 if (bufferBytes + len > bufferSize) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
144 partLen = bufferSize - bufferBytes;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
145 } else {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
146 partLen = len;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
147 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
148 System.arraycopy(b, off, buffer, bufferBytes, partLen);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
149 bufferBytes += partLen;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
150 off += partLen;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
151 len -= partLen;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
152 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
153 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
154
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
155 public void write(byte b[]) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
156 write(b, 0, b.length);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
157 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
158
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
159 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
160 // Flush the output. This method saves buffered data in the
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
161 // underlying file object adding data sizes and timestamps. If the
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
162 // updateTimeOffset is set to false, then the current time offset
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
163 // will not be changed for next write operation.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
164 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
165
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
166 public void flush(boolean updateTimeOffset) throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
167 if (bufferBytes > 0) {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
168 df.writeInt(bufferBytes);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
169 df.write(buffer, 0, (bufferBytes + 3) & 0x7FFFFFFC);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
170 df.writeInt((int)lastTimeOffset);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
171 bufferBytes = 0;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
172 if (updateTimeOffset)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
173 lastTimeOffset = -1;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
174 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
175 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
176
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
177 public void flush() throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
178 flush(true);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
179 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
180
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
181 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
182 // Before writing any data, remember time offset and flush the
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
183 // buffer before it becomes full.
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
184 //
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
185
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
186 protected void prepareWriting() throws IOException {
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
187 if (lastTimeOffset == -1)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
188 lastTimeOffset = System.currentTimeMillis() - startTime;
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
189 if (bufferBytes > bufferSize - 4)
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
190 flush(false);
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
191 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
192
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
193 }
30bb7074acb1 upload all file of tighVNCProxy
e085711
parents:
diff changeset
194