Example #1
0
 /// <summary>
 /// Decompresses byte array to new byte array.
 /// </summary>
 /// <param name="gzip">Gzipped byte array to decompress.</param>
 /// <returns>Returns a decompressed byte array.</returns>
 internal static byte[] Decompress(byte[] gzip)
 {
     // Create a GZIP stream with decompression mode.
     // ... Then create a buffer and write into while reading from the GZIP stream.
     using (GZipStream stream = new GZipStream(
                new MemoryStream(gzip),
                Ionic.Zlib.CompressionMode.Decompress))
     {
         const int size   = 4096;
         byte[]    buffer = new byte[size];
         using (MemoryStream memory = new MemoryStream())
         {
             int count = 0;
             do
             {
                 count = stream.Read(buffer, 0, size);
                 if (count > 0)
                 {
                     memory.Write(buffer, 0, count);
                 }
             }while (count > 0);
             return(memory.ToArray());
         }
     }
 }
Example #2
0
 public byte[] decompressgzip(int i)
 {
     byte[] buffer          = decompress(i);
     byte[] gzipInputBuffer = new byte[999999];
     try {
         Ionic.Zlib.GZipStream gzipinputstream = new Ionic.Zlib.GZipStream(
             new MemoryStream(buffer), Ionic.Zlib.CompressionMode.Decompress);
         do
         {
             if (i == gzipInputBuffer.Length)
             {
                 throw new Exception("buffer overflow!");
             }
             int k = gzipinputstream.Read(gzipInputBuffer, i,
                                          gzipInputBuffer.Length - i);
             if (k == 0)
             {
                 break;
             }
             i += k;
         } while (true);
     } catch (IOException _ex) {
         throw new Exception("error unzipping");
     }
     buffer = new byte[i];
     //System.Array.Copy(gzipInputBuffer, 0, onDemandData.buffer, 0, i);
     Buffer.BlockCopy(gzipInputBuffer, 0, buffer, 0, i);
     return(buffer);
 }
Example #3
0
 static bool IsGzipFile(string filePath)
 {
     using (var fstm = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, 64))
         using (var stm = new Ionic.Zlib.GZipStream(fstm, Ionic.Zlib.CompressionMode.Decompress))
         {
             try
             {
                 stm.Read(new byte[0], 0, 0);
                 return(true);
             }
             catch (Ionic.Zlib.ZlibException)
             {
                 return(false);
             }
         }
 }
Example #4
0
        public OnDemandData getNextNode()
        {
            OnDemandData onDemandData;

            lock (aClass19_1358) {
                onDemandData = (OnDemandData)aClass19_1358.popHead();
            }
            if (onDemandData == null)
            {
                return(null);
            }
            lock (nodeSubList) {
                onDemandData.unlinkSub();
            }
            if (onDemandData.buffer == null)
            {
                return(onDemandData);
            }
            int i = 0;

            try {
                Ionic.Zlib.GZipStream gzipinputstream = new Ionic.Zlib.GZipStream(
                    new MemoryStream(onDemandData.buffer), Ionic.Zlib.CompressionMode.Decompress);
                do
                {
                    if (i == gzipInputBuffer.Length)
                    {
                        throw new Exception("buffer overflow!");
                    }
                    int k = gzipinputstream.Read(gzipInputBuffer, i,
                                                 gzipInputBuffer.Length - i);
                    if (k == 0)
                    {
                        break;
                    }
                    i += k;
                } while (true);
            } catch (IOException _ex) {
                throw new Exception("error unzipping");
            }
            onDemandData.buffer = new byte[i];
            //System.Array.Copy(gzipInputBuffer, 0, onDemandData.buffer, 0, i);
            Buffer.BlockCopy(gzipInputBuffer, 0, onDemandData.buffer, 0, i);
            return(onDemandData);
        }
Example #5
0
        public static byte[] Decompress(byte[] gzip)
        {
            using (var stream = new Ionic.Zlib.GZipStream(
                       new MemoryStream(gzip),
                       Ionic.Zlib.CompressionMode.Decompress, false))
            {
                const int size   = 4096;
                var       buffer = new byte[size];
                using (var memory = new MemoryStream())
                {
                    int count;
                    do
                    {
                        count = stream.Read(buffer, 0, size);
                        if (count > 0)
                        {
                            memory.Write(buffer, 0, count);
                        }
                    } while (count > 0);

                    return(memory.ToArray());
                }
            }
        }
        private IEnumerator Post(CallRequestContainer reqContainer)
        {
#if PLAYFAB_REQUEST_TIMING
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();
            var startTime = DateTime.UtcNow;
#endif

            var www = new UnityWebRequest(reqContainer.FullUrl)
            {
                uploadHandler   = new UploadHandlerRaw(reqContainer.Payload),
                downloadHandler = new DownloadHandlerBuffer(),
                method          = "POST"
            };

            foreach (var headerPair in reqContainer.RequestHeaders)
            {
                if (!string.IsNullOrEmpty(headerPair.Key) && !string.IsNullOrEmpty(headerPair.Value))
                {
                    www.SetRequestHeader(headerPair.Key, headerPair.Value);
                }
                else
                {
                    Debug.LogWarning("Null header: " + headerPair.Key + " = " + headerPair.Value);
                }
            }

#if UNITY_2017_2_OR_NEWER
            yield return(www.SendWebRequest());
#else
            yield return(www.Send());
#endif

#if PLAYFAB_REQUEST_TIMING
            stopwatch.Stop();
            var timing = new PlayFabHttp.RequestTiming {
                StartTimeUtc        = startTime,
                ApiEndpoint         = reqContainer.ApiEndpoint,
                WorkerRequestMs     = (int)stopwatch.ElapsedMilliseconds,
                MainThreadRequestMs = (int)stopwatch.ElapsedMilliseconds
            };
            PlayFabHttp.SendRequestTiming(timing);
#endif

            if (!string.IsNullOrEmpty(www.error))
            {
                OnError(www.error, reqContainer);
            }
            else
            {
                try
                {
                    byte[] responseBytes    = www.downloadHandler.data;
                    bool   isGzipCompressed = responseBytes != null && responseBytes[0] == 31 && responseBytes[1] == 139;
                    string responseText     = "Unexpected error: cannot decompress GZIP stream.";
                    if (!isGzipCompressed && responseBytes != null)
                    {
                        responseText = System.Text.Encoding.UTF8.GetString(responseBytes, 0, responseBytes.Length);
                    }
#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
                    if (isGzipCompressed)
                    {
                        var stream = new MemoryStream(responseBytes);
                        using (var gZipStream = new Ionic.Zlib.GZipStream(stream, Ionic.Zlib.CompressionMode.Decompress, false))
                        {
                            var buffer = new byte[4096];
                            using (var output = new MemoryStream())
                            {
                                int read;
                                while ((read = gZipStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    output.Write(buffer, 0, read);
                                }
                                output.Seek(0, SeekOrigin.Begin);
                                var streamReader = new StreamReader(output);
                                var jsonResponse = streamReader.ReadToEnd();
                                //Debug.Log(jsonResponse);
                                OnResponse(jsonResponse, reqContainer);
                                //Debug.Log("Successful UnityHttp decompress for: " + www.url);
                            }
                        }
                    }
                    else
#endif
                    {
                        OnResponse(responseText, reqContainer);
                    }
                }
                catch (Exception e)
                {
                    OnError("Unhandled error in PlayFabUnityHttp: " + e, reqContainer);
                }
            }
            www.Dispose();
        }
Example #7
0
        private IEnumerator Post(CallRequestContainer reqContainer)
        {
#if PLAYFAB_REQUEST_TIMING
            var stopwatch = System.Diagnostics.Stopwatch.StartNew();
#endif

            var www = new UnityWebRequest(reqContainer.FullUrl)
            {
                uploadHandler   = new UploadHandlerRaw(reqContainer.Payload),
                downloadHandler = new DownloadHandlerBuffer(),
                method          = "POST"
            };

            foreach (var headerPair in reqContainer.RequestHeaders)
            {
                www.SetRequestHeader(headerPair.Key, headerPair.Value);
            }

            yield return(www.Send());

#if PLAYFAB_REQUEST_TIMING
            stopwatch.Stop();
            var timing = new PlayFabHttp.RequestTiming {
                StartTimeUtc        = startTime,
                ApiEndpoint         = reqContainer.ApiEndpoint,
                WorkerRequestMs     = (int)stopwatch.ElapsedMilliseconds,
                MainThreadRequestMs = (int)stopwatch.ElapsedMilliseconds
            };
            PlayFabHttp.SendRequestTiming(timing);
#endif

            if (!string.IsNullOrEmpty(www.error))
            {
                OnError(www.error, reqContainer);
            }
            else
            {
                try
                {
#if !UNITY_WSA && !UNITY_WP8 && !UNITY_WEBGL
                    string encoding;
                    if (www.GetResponseHeaders().TryGetValue("Content-Encoding", out encoding) && encoding.ToLower() == "gzip")
                    {
                        var stream = new MemoryStream(www.downloadHandler.data);
                        using (var gZipStream = new Ionic.Zlib.GZipStream(stream, Ionic.Zlib.CompressionMode.Decompress, false))
                        {
                            var buffer = new byte[4096];
                            using (var output = new MemoryStream())
                            {
                                int read;
                                while ((read = gZipStream.Read(buffer, 0, buffer.Length)) > 0)
                                {
                                    output.Write(buffer, 0, read);
                                }
                                output.Seek(0, SeekOrigin.Begin);
                                var streamReader = new StreamReader(output);
                                var jsonResponse = streamReader.ReadToEnd();
                                //Debug.Log(jsonResponse);
                                OnResponse(jsonResponse, reqContainer);
                            }
                        }
                    }
                    else
#endif
                    {
                        OnResponse(www.downloadHandler.text, reqContainer);
                    }
                }
                catch (Exception e)
                {
                    OnError("Unhandled error in PlayFabWWW: " + e, reqContainer);
                }
            }
        }