Skip to content

Commit c8352ed

Browse files
committed
Converted any dangling tabs to spaces.
1 parent b6d0a83 commit c8352ed

File tree

4 files changed

+256
-256
lines changed

4 files changed

+256
-256
lines changed

src/net/tootallnate/websocket/WebSocket.java

Lines changed: 64 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,15 @@ public final class WebSocket {
7979
* The bytes that make up the current text frame being read.
8080
*/
8181
private ByteBuffer currentFrame;
82-
/**
83-
* Queue of buffers that need to be sent to the client.
84-
*/
85-
private BlockingQueue<ByteBuffer> bufferQueue;
86-
/**
87-
* Lock object to ensure that data is sent from the bufferQueue in
88-
* the proper order
89-
*/
90-
private Object bufferQueueMutex = new Object();
82+
/**
83+
* Queue of buffers that need to be sent to the client.
84+
*/
85+
private BlockingQueue<ByteBuffer> bufferQueue;
86+
/**
87+
* Lock object to ensure that data is sent from the bufferQueue in
88+
* the proper order
89+
*/
90+
private Object bufferQueueMutex = new Object();
9191

9292

9393
// CONSTRUCTOR /////////////////////////////////////////////////////////////
@@ -96,16 +96,16 @@ public final class WebSocket {
9696
* @param socketChannel The <tt>SocketChannel</tt> instance to read and
9797
* write to. The channel should already be registered
9898
* with a Selector before construction of this object.
99-
* @param bufferQueue The Queue that we should use to buffer data that
100-
* hasn't been sent to the client yet.
99+
* @param bufferQueue The Queue that we should use to buffer data that
100+
* hasn't been sent to the client yet.
101101
* @param listener The {@link WebSocketListener} to notify of events when
102102
* they occur.
103103
*/
104104
WebSocket(SocketChannel socketChannel, BlockingQueue<ByteBuffer> bufferQueue,
105-
WebSocketListener listener)
106-
{
105+
WebSocketListener listener)
106+
{
107107
this.socketChannel = socketChannel;
108-
this.bufferQueue = bufferQueue;
108+
this.bufferQueue = bufferQueue;
109109
this.handshakeComplete = false;
110110
this.remoteHandshake = this.currentFrame = null;
111111
this.buffer = ByteBuffer.allocate(1);
@@ -150,10 +150,10 @@ public void close() throws IOException {
150150
this.wsl.onClose(this);
151151
}
152152

153-
/**
154-
* @return True if all of the text was sent to the client by this thread.
155-
* False if some of the text had to be buffered to be sent later.
156-
*/
153+
/**
154+
* @return True if all of the text was sent to the client by this thread.
155+
* False if some of the text had to be buffered to be sent later.
156+
*/
157157
public boolean send(String text) throws IOException {
158158
if (!this.handshakeComplete) throw new NotYetConnectedException();
159159
if (text == null) throw new NullPointerException("Cannot send 'null' data to a WebSocket.");
@@ -164,49 +164,49 @@ public boolean send(String text) throws IOException {
164164
b.put(START_OF_FRAME);
165165
b.put(textBytes);
166166
b.put(END_OF_FRAME);
167-
b.rewind();
167+
b.rewind();
168168

169-
// See if we have any backlog that needs to be sent first
170-
if (handleWrite()) {
171-
// Write the ByteBuffer to the socket
172-
this.socketChannel.write(b);
173-
}
169+
// See if we have any backlog that needs to be sent first
170+
if (handleWrite()) {
171+
// Write the ByteBuffer to the socket
172+
this.socketChannel.write(b);
173+
}
174174

175-
// If we didn't get it all sent, add it to the buffer of buffers
176-
if (b.remaining() > 0) {
177-
if (!this.bufferQueue.offer(b)) {
178-
throw new IOException("Buffers are full, message could not be sent to" +
179-
this.socketChannel.socket().getRemoteSocketAddress());
180-
}
181-
return false;
182-
}
175+
// If we didn't get it all sent, add it to the buffer of buffers
176+
if (b.remaining() > 0) {
177+
if (!this.bufferQueue.offer(b)) {
178+
throw new IOException("Buffers are full, message could not be sent to" +
179+
this.socketChannel.socket().getRemoteSocketAddress());
180+
}
181+
return false;
182+
}
183183

184-
return true;
184+
return true;
185185
}
186186

187-
boolean hasBufferedData() {
188-
return !this.bufferQueue.isEmpty();
189-
}
187+
boolean hasBufferedData() {
188+
return !this.bufferQueue.isEmpty();
189+
}
190190

191-
/**
192-
* @return True if all data has been sent to the client, false if there
193-
* is still some buffered.
194-
*/
195-
boolean handleWrite() throws IOException {
196-
synchronized (this.bufferQueueMutex) {
197-
ByteBuffer buffer = this.bufferQueue.peek();
198-
while (buffer != null) {
199-
this.socketChannel.write(buffer);
200-
if (buffer.remaining() > 0) {
201-
return false; // Didn't finish this buffer. There's more to send.
202-
} else {
203-
this.bufferQueue.poll(); // Buffer finished. Remove it.
204-
buffer = this.bufferQueue.peek();
205-
}
206-
}
207-
return true;
208-
}
209-
}
191+
/**
192+
* @return True if all data has been sent to the client, false if there
193+
* is still some buffered.
194+
*/
195+
boolean handleWrite() throws IOException {
196+
synchronized (this.bufferQueueMutex) {
197+
ByteBuffer buffer = this.bufferQueue.peek();
198+
while (buffer != null) {
199+
this.socketChannel.write(buffer);
200+
if (buffer.remaining() > 0) {
201+
return false; // Didn't finish this buffer. There's more to send.
202+
} else {
203+
this.bufferQueue.poll(); // Buffer finished. Remove it.
204+
buffer = this.bufferQueue.peek();
205+
}
206+
}
207+
return true;
208+
}
209+
}
210210

211211
public SocketChannel socketChannel() {
212212
return this.socketChannel;
@@ -254,16 +254,16 @@ private void recieveHandshake() throws IOException, NoSuchAlgorithmException {
254254
&& h[h.length-19] == LF
255255
&& h[h.length-18] == CR
256256
&& h[h.length-17] == LF)){
257-
byte[] handShakeBody = new byte[16];
258-
handShakeBody[0]=h[h.length-16];
257+
byte[] handShakeBody = new byte[16];
258+
handShakeBody[0]=h[h.length-16];
259259
handShakeBody[1]=h[h.length-15];
260260
handShakeBody[2]=h[h.length-14];
261261
handShakeBody[3]=h[h.length-13];
262262
handShakeBody[4]=h[h.length-12];
263263
handShakeBody[5]=h[h.length-11];
264264
handShakeBody[6]=h[h.length-10];
265265
handShakeBody[7]=h[h.length-9];
266-
handShakeBody[8]=h[h.length-8];
266+
handShakeBody[8]=h[h.length-8];
267267
handShakeBody[9]=h[h.length-7];
268268
handShakeBody[10]=h[h.length-6];
269269
handShakeBody[11]=h[h.length-5];
@@ -281,8 +281,8 @@ else if ((h.length>=12 && h[h.length-12] == CR
281281
&& h[h.length-11] == LF
282282
&& h[h.length-10] == CR
283283
&& h[h.length-9] == LF) && new String(this.remoteHandshake.array(), UTF8_CHARSET).contains("Sec-WebSocket-Key1")) {
284-
byte[] handShakeBody = new byte[8];
285-
handShakeBody[0]=h[h.length-8];
284+
byte[] handShakeBody = new byte[8];
285+
handShakeBody[0]=h[h.length-8];
286286
handShakeBody[1]=h[h.length-7];
287287
handShakeBody[2]=h[h.length-6];
288288
handShakeBody[3]=h[h.length-5];
@@ -299,13 +299,13 @@ else if ((h.length>=12 && h[h.length-12] == CR
299299
&& h[h.length-2] == CR
300300
&& h[h.length-1] == LF) && !(new String(this.remoteHandshake.array(), UTF8_CHARSET).contains("Sec")) ||
301301
(h.length==23 && h[h.length-1] == 0) ) {
302-
303-
completeHandshake(null);
304-
}
302+
303+
completeHandshake(null);
304+
}
305305
}
306306

307307
private void completeHandshake(byte[] handShakeBody) throws IOException, NoSuchAlgorithmException {
308-
byte[] handshakeBytes = this.remoteHandshake.array();
308+
byte[] handshakeBytes = this.remoteHandshake.array();
309309
String handshake = new String(handshakeBytes, UTF8_CHARSET);
310310
this.handshakeComplete = true;
311311
if (this.wsl.onHandshakeRecieved(this, handshake, handShakeBody)) {

src/net/tootallnate/websocket/WebSocketClient.java

Lines changed: 75 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ public abstract class WebSocketClient implements Runnable, WebSocketListener {
5252
*/
5353
private byte[] key3=null;
5454
public static enum Draft{
55-
DRAFT75,
56-
DRAFT76
55+
DRAFT75,
56+
DRAFT76
5757
}
5858

5959
// CONSTRUCTOR /////////////////////////////////////////////////////////////
@@ -151,18 +151,18 @@ public void run() {
151151
"Connection: Upgrade\r\n" +
152152
"Host: " + host + "\r\n" +
153153
"Origin: " + origin + "\r\n";
154-
if(this.draft==Draft.DRAFT76)
155-
{
156-
request+="Sec-WebSocket-Key1: " + this.generateKey() + "\r\n";
157-
request+="Sec-WebSocket-Key2: " + this.generateKey() + "\r\n";
158-
this.key3=new byte[8];
159-
r.nextBytes(this.key3);
160-
}
154+
if(this.draft==Draft.DRAFT76)
155+
{
156+
request+="Sec-WebSocket-Key1: " + this.generateKey() + "\r\n";
157+
request+="Sec-WebSocket-Key2: " + this.generateKey() + "\r\n";
158+
this.key3=new byte[8];
159+
r.nextBytes(this.key3);
160+
}
161161
//extraHeaders.toString() +
162162
request+="\r\n";
163163
conn.socketChannel().write(ByteBuffer.wrap(request.getBytes(WebSocket.UTF8_CHARSET)));
164164
if(this.key3 !=null){
165-
conn.socketChannel().write(ByteBuffer.wrap(this.key3));
165+
conn.socketChannel().write(ByteBuffer.wrap(this.key3));
166166
}
167167
}
168168

@@ -175,42 +175,42 @@ public void run() {
175175
} catch (IOException ex) {
176176
ex.printStackTrace();
177177
} catch (NoSuchAlgorithmException e) {
178-
// TODO Auto-generated catch block
179-
e.printStackTrace();
180-
}
178+
// TODO Auto-generated catch block
179+
e.printStackTrace();
180+
}
181181
}
182182
private String generateKey(){
183-
Random r=new Random();
184-
long maxNumber=4294967295L;
185-
long spaces=r.nextInt(12)+1;
186-
int max=new Long(maxNumber/spaces).intValue();
187-
max=Math.abs(max);
188-
int number=r.nextInt(max)+1;
189-
if(this.number1==0){
190-
this.number1=number;
191-
}
192-
else{
193-
this.number2=number;
194-
}
195-
long product=number*spaces;
196-
String key=Long.toString(product);
197-
int numChars=r.nextInt(12);
198-
for (int i=0;i<numChars;i++){
199-
int position=r.nextInt(key.length());
200-
position=Math.abs(position);
201-
char randChar=(char)(r.nextInt(95)+33);
202-
//exclude numbers here
203-
if(randChar >= 48 && randChar <=57){
204-
randChar-=15;
205-
}
206-
key=new StringBuilder(key).insert(position, randChar).toString();
207-
}
208-
for (int i=0;i<spaces;i++){
209-
int position=r.nextInt(key.length()-1)+1;
210-
position=Math.abs(position);
211-
key=new StringBuilder(key).insert(position,"\u0020").toString();
212-
}
213-
return key;
183+
Random r=new Random();
184+
long maxNumber=4294967295L;
185+
long spaces=r.nextInt(12)+1;
186+
int max=new Long(maxNumber/spaces).intValue();
187+
max=Math.abs(max);
188+
int number=r.nextInt(max)+1;
189+
if(this.number1==0){
190+
this.number1=number;
191+
}
192+
else{
193+
this.number2=number;
194+
}
195+
long product=number*spaces;
196+
String key=Long.toString(product);
197+
int numChars=r.nextInt(12);
198+
for (int i=0;i<numChars;i++){
199+
int position=r.nextInt(key.length());
200+
position=Math.abs(position);
201+
char randChar=(char)(r.nextInt(95)+33);
202+
//exclude numbers here
203+
if(randChar >= 48 && randChar <=57){
204+
randChar-=15;
205+
}
206+
key=new StringBuilder(key).insert(position, randChar).toString();
207+
}
208+
for (int i=0;i<spaces;i++){
209+
int position=r.nextInt(key.length()-1)+1;
210+
position=Math.abs(position);
211+
key=new StringBuilder(key).insert(position,"\u0020").toString();
212+
}
213+
return key;
214214
}
215215

216216
// WebSocketListener IMPLEMENTATION ////////////////////////////////////////
@@ -228,36 +228,36 @@ private String generateKey(){
228228
public boolean onHandshakeRecieved(WebSocket conn, String handshake,byte[] reply) throws IOException, NoSuchAlgorithmException {
229229
// TODO: Do some parsing of the returned handshake, and close connection
230230
// (return false) if we recieved anything unexpected.
231-
if(this.draft==Draft.DRAFT76){
232-
if(reply==null){
233-
return false;
234-
}
235-
byte[] challenge=new byte[]{
236-
(byte)( this.number1 >> 24 ),
237-
(byte)( (this.number1 << 8) >> 24 ),
238-
(byte)( (this.number1 << 16) >> 24 ),
239-
(byte)( (this.number1 << 24) >> 24 ),
240-
(byte)( this.number2 >> 24 ),
241-
(byte)( (this.number2 << 8) >> 24 ),
242-
(byte)( (this.number2 << 16) >> 24 ),
243-
(byte)( (this.number2 << 24) >> 24 ),
244-
this.key3[0],
245-
this.key3[1],
246-
this.key3[2],
247-
this.key3[3],
248-
this.key3[4],
249-
this.key3[5],
250-
this.key3[6],
251-
this.key3[7]
252-
};
253-
MessageDigest md5=MessageDigest.getInstance("MD5");
254-
byte[] expected=md5.digest(challenge);
255-
for(int i=0;i<reply.length;i++){
256-
if(expected[i]!=reply[i]){
257-
return false;
258-
}
259-
}
260-
}
231+
if(this.draft==Draft.DRAFT76){
232+
if(reply==null){
233+
return false;
234+
}
235+
byte[] challenge=new byte[]{
236+
(byte)( this.number1 >> 24 ),
237+
(byte)( (this.number1 << 8) >> 24 ),
238+
(byte)( (this.number1 << 16) >> 24 ),
239+
(byte)( (this.number1 << 24) >> 24 ),
240+
(byte)( this.number2 >> 24 ),
241+
(byte)( (this.number2 << 8) >> 24 ),
242+
(byte)( (this.number2 << 16) >> 24 ),
243+
(byte)( (this.number2 << 24) >> 24 ),
244+
this.key3[0],
245+
this.key3[1],
246+
this.key3[2],
247+
this.key3[3],
248+
this.key3[4],
249+
this.key3[5],
250+
this.key3[6],
251+
this.key3[7]
252+
};
253+
MessageDigest md5=MessageDigest.getInstance("MD5");
254+
byte[] expected=md5.digest(challenge);
255+
for(int i=0;i<reply.length;i++){
256+
if(expected[i]!=reply[i]){
257+
return false;
258+
}
259+
}
260+
}
261261
return true;
262262
}
263263

@@ -288,7 +288,7 @@ public void onClose(WebSocket conn) {
288288

289289
@Override
290290
public net.tootallnate.websocket.WebSocketListener.Draft getDraft() {
291-
return (net.tootallnate.websocket.WebSocketListener.Draft)net.tootallnate.websocket.WebSocketListener.Draft.valueOf(this.draft.name());
291+
return (net.tootallnate.websocket.WebSocketListener.Draft)net.tootallnate.websocket.WebSocketListener.Draft.valueOf(this.draft.name());
292292
}
293293

294294
// ABTRACT METHODS /////////////////////////////////////////////////////////

src/net/tootallnate/websocket/WebSocketListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* @author Nathan Rajlich
1010
*/
1111
interface WebSocketListener {
12-
/**
12+
/**
1313
* Enum for WebSocket Draft
1414
*/
1515
public static enum Draft {

0 commit comments

Comments
 (0)