1 package com.adobe.protocols.dict.util
\r
3 import com.adobe.net.proxies.RFC2817Socket;
\r
4 import flash.events.ProgressEvent;
\r
6 public class SocketHelper
\r
7 extends RFC2817Socket
\r
9 private var terminator:String = "\r\n.\r\n";
\r
10 private var buffer:String;
\r
11 public static var COMPLETE_RESPONSE:String = "completeResponse";
\r
13 public function SocketHelper()
\r
16 buffer = new String();
\r
17 addEventListener(ProgressEvent.SOCKET_DATA, incomingData);
\r
20 private function incomingData(event:ProgressEvent):void
\r
22 buffer += readUTFBytes(bytesAvailable);
\r
23 buffer = buffer.replace(/250[^\r\n]+\r\n/, ""); // Get rid of all 250s. Don't need them.
\r
24 var codeStr:String = buffer.substring(0, 3);
\r
25 if (!isNaN(parseInt(codeStr)))
\r
27 var code:uint = uint(codeStr);
\r
28 if (code == 150 || code >= 200)
\r
30 buffer = buffer.replace("\r\n", this.terminator);
\r
34 while (buffer.indexOf(this.terminator) != -1)
\r
36 var chunk:String = buffer.substring(0, buffer.indexOf(this.terminator));
\r
37 buffer = buffer.substring(chunk.length + this.terminator.length, buffer.length);
\r
38 throwResponseEvent(chunk);
\r
42 private function throwResponseEvent(response:String):void
\r
44 var responseEvent:CompleteResponseEvent = new CompleteResponseEvent();
\r
45 responseEvent.response = response;
\r
46 dispatchEvent(responseEvent);
\r