annotate src/SessionRecorder.java @ 5:bdb8d7c7d4d1

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