comparison src/treeVnc/SessionRecorder.java @ 0:756bfaf731f3

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