public void OnResponseHeaders(ProgressEvent pEvent) { /* Debug.Log ("On Response Headers"); this.expectedContentLength = pEvent.bytesTotal; Debug.Log ("Expected Content Length: " + this.expectedContentLength); if (this.expectedContentLength < this.progressContentLength) { request.Stop(); this.cleanupConnectionSuccessful(false); } */ }
public void ReadFromStream(Stream inputStream) { //Debug.Log ("Read From Stream"); //var inputStream = new BinaryReader(inputStream); var top = ReadLine(inputStream).Split(new char[] { ' ' }); /* * Stream output = null; * * if (outputFilePath == null) { * output = new MemoryStream (); * } else { * output = new FileStream(outputFilePath,FileMode.Append); * }*/ if (!int.TryParse(top[1], out status)) { throw new HTTPException("Bad Status Code"); } message = string.Join(" ", top, 2, top.Length - 2); headers.Clear(); while (true) { // Collect Headers string[] parts = ReadKeyValue(inputStream); if (parts == null) { break; } AddHeader(parts[0], parts[1]); } if (GetHeader("transfer-encoding") == "chunked") { var output = new MemoryStream(); chunks = new List <byte[]> (); while (true) { // Collect Body string hexLength = ReadLine(inputStream); //Console.WriteLine("HexLength:" + hexLength); if (hexLength == "0") { lock (chunks) { chunks.Add(new byte[] {}); } break; } int length = int.Parse(hexLength, NumberStyles.AllowHexSpecifier); for (int i = 0; i < length; i++) { output.WriteByte((byte)inputStream.ReadByte()); } lock (chunks) { /* * if (GetHeader ("content-encoding").Contains ("gzip")) * chunks.Add (UnZip(output)); * else * chunks.Add (output.ToArray ()); */ chunks.Add(output.ToArray()); } output.SetLength(0); //forget the CRLF. inputStream.ReadByte(); inputStream.ReadByte(); } while (true) { //Collect Trailers string[] parts = ReadKeyValue(inputStream); if (parts == null) { break; } AddHeader(parts[0], parts[1]); } var unchunked = new List <byte>(); foreach (var i in chunks) { unchunked.AddRange(i); } bytes = unchunked.ToArray(); } else { // Read Body contentLength = 0; try { contentLength = int.Parse(GetHeader("content-length")); } catch { contentLength = 0; } if (downloadDelegate != null) { ProgressEvent pEvent = new ProgressEvent(); pEvent.bytesLoaded = 0; pEvent.bytesTotal = contentLength; downloadDelegate.OnResponseHeaders(pEvent); } if (outputFilePath == null) { var output = new MemoryStream(); for (int i = 0; i < contentLength; i++) { output.WriteByte((byte)inputStream.ReadByte()); } /*if (GetHeader ("content-encoding").Contains ("gzip")) { * bytes = UnZip(output); * } else { * bytes = output.ToArray (); * }*/ bytes = output.ToArray(); } else if (outputFilePath != null && !onlyContentLength) //para descarga de ficheros { downloadedBytes = 0; progressDownloadedBytes = 0; var output = new FileStream(outputFilePath, FileMode.Append); //Debug.Log("Force Stop: " + forceStop.ToString()); for (int i = 0; (i < contentLength && !forceStop); i++) { output.WriteByte((byte)inputStream.ReadByte()); downloadedBytes = i + 1; if (downloadedBytes % PROGRESS_CHUNK_SIZE == 0) { if (downloadDelegate != null) { ProgressEvent pEvent = new ProgressEvent(); pEvent.bytesLoaded = downloadedBytes - progressDownloadedBytes; pEvent.bytesTotal = contentLength; downloadDelegate.OnProgress(pEvent); progressDownloadedBytes = downloadedBytes; } } } //Debug.Log("Force Stop: " + forceStop.ToString()); ProgressEvent pEvent2 = new ProgressEvent(); pEvent2.bytesLoaded = downloadedBytes - progressDownloadedBytes; pEvent2.bytesTotal = contentLength; downloadDelegate.OnProgress(pEvent2); //Debug.Log("Downloaded Bytes: " + downloadedBytes); output.Flush(); output.Close(); } } }
public void OnProgress(ProgressEvent pEvent) { this.progressContentLength = pEvent.bytesLoaded + this.progressContentLength; if (GamedoniaFiles.Instance.debug) Debug.Log ("[DownloadManager] Progress Content Length: " + this.progressContentLength); if ((this.expectedContentLength == -1 || this.expectedContentLength == 0) && (!resume)) { this.expectedContentLength = pEvent.bytesTotal; //Check de tamaño //var space:Number = 0; //TODO Check disk spacce //var space:Number = File.applicationStorageDirectory.spaceAvailable; //if (space < event.bytesTotal) { // cleanupConnectionSuccessful(false,3); //} //updateDownloadPreferences(this); } dispatchDownloadEvent (DOWNLOAD_DID_RECEIVE_DATA,this); }
public void ReadFromStream(Stream inputStream) { //Debug.Log ("Read From Stream"); //var inputStream = new BinaryReader(inputStream); var top = ReadLine (inputStream).Split (new char[] { ' ' }); /* Stream output = null; if (outputFilePath == null) { output = new MemoryStream (); } else { output = new FileStream(outputFilePath,FileMode.Append); }*/ if (!int.TryParse (top[1], out status)) throw new HTTPException ("Bad Status Code"); message = string.Join (" ", top, 2, top.Length - 2); headers.Clear (); while (true) { // Collect Headers string[] parts = ReadKeyValue (inputStream); if (parts == null) break; AddHeader (parts[0], parts[1]); } if (GetHeader ("transfer-encoding") == "chunked") { var output = new MemoryStream(); chunks = new List<byte[]> (); while (true) { // Collect Body string hexLength = ReadLine (inputStream); //Console.WriteLine("HexLength:" + hexLength); if (hexLength == "0") { lock(chunks) { chunks.Add(new byte[] {}); } break; } int length = int.Parse (hexLength, NumberStyles.AllowHexSpecifier); for (int i = 0; i < length; i++) output.WriteByte ((byte)inputStream.ReadByte ()); lock(chunks) { /* if (GetHeader ("content-encoding").Contains ("gzip")) chunks.Add (UnZip(output)); else chunks.Add (output.ToArray ()); */ chunks.Add (output.ToArray ()); } output.SetLength (0); //forget the CRLF. inputStream.ReadByte (); inputStream.ReadByte (); } while (true) { //Collect Trailers string[] parts = ReadKeyValue (inputStream); if (parts == null) break; AddHeader (parts[0], parts[1]); } var unchunked = new List<byte>(); foreach(var i in chunks) { unchunked.AddRange(i); } bytes = unchunked.ToArray(); } else { // Read Body contentLength = 0; try { contentLength = int.Parse (GetHeader ("content-length")); } catch { contentLength = 0; } if (downloadDelegate != null) { ProgressEvent pEvent = new ProgressEvent(); pEvent.bytesLoaded = 0; pEvent.bytesTotal = contentLength; downloadDelegate.OnResponseHeaders(pEvent); } if (outputFilePath == null) { var output = new MemoryStream(); for (int i = 0; i < contentLength; i++) output.WriteByte ((byte)inputStream.ReadByte ()); /*if (GetHeader ("content-encoding").Contains ("gzip")) { bytes = UnZip(output); } else { bytes = output.ToArray (); }*/ bytes = output.ToArray (); }else if (outputFilePath != null && !onlyContentLength){ //para descarga de ficheros downloadedBytes = 0; progressDownloadedBytes = 0; var output = new FileStream(outputFilePath,FileMode.Append); //Debug.Log("Force Stop: " + forceStop.ToString()); for (int i = 0; (i < contentLength && !forceStop); i++) { output.WriteByte ((byte)inputStream.ReadByte ()); downloadedBytes = i+1; if (downloadedBytes % PROGRESS_CHUNK_SIZE == 0) { if (downloadDelegate != null) { ProgressEvent pEvent = new ProgressEvent(); pEvent.bytesLoaded = downloadedBytes - progressDownloadedBytes; pEvent.bytesTotal = contentLength; downloadDelegate.OnProgress(pEvent); progressDownloadedBytes = downloadedBytes; } } } //Debug.Log("Force Stop: " + forceStop.ToString()); ProgressEvent pEvent2 = new ProgressEvent(); pEvent2.bytesLoaded = downloadedBytes - progressDownloadedBytes; pEvent2.bytesTotal = contentLength; downloadDelegate.OnProgress(pEvent2); //Debug.Log("Downloaded Bytes: " + downloadedBytes); output.Flush(); output.Close(); } } }