def _read_to_end(stream, bufsize=4096): buffer = _make_buffer(bufsize) memory = MemoryStream() while True: count = stream.Read(buffer, 0, bufsize) if not count: break memory.Write(buffer, 0, count) bytes = memory.ToArray() memory.Close() return bytes
def testWeCanBindToEncodingGetString(self): """Check that we can bind to the Encoding.GetString method with variables.""" from System.Text import Encoding from System.IO import MemoryStream myBytes = Encoding.UTF8.GetBytes('Some testing string') stream = MemoryStream() stream.Write(myBytes, 0, myBytes.Length) stream.Position = 0 buff = System.Array.CreateInstance(System.Byte, 3) buff.Initialize() data = [] read = 1 while read > 0: read, _ = stream.Read(buff, 0, buff.Length) temp = Encoding.UTF8.GetString(buff, 0, read) data.append(temp) data = ''.join(data) self.assertEqual(data, 'Some testing string')
def test_we_can_bind_to_encoding_get_string(): """Check that we can bind to the Encoding.GetString method with variables.""" from System.Text import Encoding from System.IO import MemoryStream my_bytes = Encoding.UTF8.GetBytes('Some testing string') stream = MemoryStream() stream.Write(my_bytes, 0, my_bytes.Length) stream.Position = 0 buff = System.Array.CreateInstance(System.Byte, 3) buff.Initialize() data = [] read = 1 while read > 0: read = stream.Read(buff, 0, buff.Length) temp = Encoding.UTF8.GetString(buff, 0, read) data.append(temp) data = ''.join(data) assert data == 'Some testing string'
def onUpdate(): temp = 0 windSpeed = 0 windDeg = 0 weatherIdList = List[Double]() weatherPathHashSet = HashSet[String]() weatherStreamList = List[MemoryStream]() weatherConditionList = List[String]() if NetworkInterface.GetIsNetworkAvailable(): try: request = WebRequest.Create(Uri(String.Concat("http://api.openweathermap.org/data/2.5/find?q=", urlEncode(location), "&units=metric&cnt=1"))) response = None stream = None streamReader = None try: nowDateTime = DateTime.Now response = request.GetResponse() stream = response.GetResponseStream() streamReader = StreamReader(stream) jsonDictionary = JsonDecoder.decode(streamReader.ReadToEnd()) if jsonDictionary is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(jsonDictionary) and jsonDictionary.ContainsKey("list") and jsonDictionary["list"] is not None and clr.GetClrType(Array).IsInstanceOfType(jsonDictionary["list"]): for obj in jsonDictionary["list"]: if obj is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(obj): if obj.ContainsKey("main") and obj["main"] is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(obj["main"]) and obj["main"].ContainsKey("temp"): temp = obj["main"]["temp"] if obj.ContainsKey("wind") and obj["wind"] is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(obj["wind"]): if obj["wind"].ContainsKey("speed"): windSpeed = obj["wind"]["speed"] if obj["wind"].ContainsKey("deg"): windDeg = obj["wind"]["deg"] if obj.ContainsKey("weather") and obj["weather"] is not None and clr.GetClrType(Array).IsInstanceOfType(obj["weather"]): for o in obj["weather"]: if o is not None and clr.GetClrType(Dictionary[String, Object]).IsInstanceOfType(o) and o.ContainsKey("id") and o["id"] is not None: weatherIdList.Add(o["id"]) for id in weatherIdList: digit = Convert.ToInt32(id / 100) path = None s = None if digit == 2: path = "Assets\\Cloud-Lightning.png" weatherConditionList.Add("Thunderstorm") elif digit == 3: path = "Assets\\Cloud-Drizzle.png" weatherConditionList.Add("Drizzle") elif digit == 5: d = Convert.ToInt32(id / 10) if d == 0: if nowDateTime.Hour > 6 and nowDateTime.Hour <= 18: path = "Assets\\Cloud-Rain-Sun.png" else: path = "Assets\\Cloud-Rain-Moon.png" else: path = "Assets\\Cloud-Rain.png" weatherConditionList.Add("Rain") elif digit == 6: path = "Assets\\Cloud-Snow.png" weatherConditionList.Add("Snow") elif digit == 7: path = "Assets\\Cloud-Fog.png" if Convert.ToInt32(id) == 701: weatherConditionList.Add("Mist") elif Convert.ToInt32(id) == 711: weatherConditionList.Add("Smoke") elif Convert.ToInt32(id) == 721: weatherConditionList.Add("Haze") elif Convert.ToInt32(id) == 731: weatherConditionList.Add("Dust") elif Convert.ToInt32(id) == 741: weatherConditionList.Add("Fog") elif digit == 8: if Convert.ToInt32(id) == 800: if nowDateTime.Hour > 6 and nowDateTime.Hour <= 18: path = "Assets\\Sun.png" weatherConditionList.Add("Sunny") else: path = "Assets\\Moon.png" weatherConditionList.Add("Clear") elif Convert.ToInt32(id) >= 801 and Convert.ToInt32(id) <= 803: if nowDateTime.Hour > 6 and nowDateTime.Hour <= 18: path = "Assets\\Cloud-Sun.png" else: path = "Assets\\Cloud-Moon.png" weatherConditionList.Add("Cloudy") elif Convert.ToInt32(id) == 804: path = "Assets\\Cloud.png" weatherConditionList.Add("Overcast") else: if Convert.ToInt32(id) == 900: path = "Assets\\Tornado.png" weatherConditionList.Add("Tornado") elif Convert.ToInt32(id) == 905: path = "Assets\\Wind.png" weatherConditionList.Add("Windy") elif Convert.ToInt32(id) == 906: path = "Assets\\Cloud-Hail.png" weatherConditionList.Add("Hail") if path is not None and weatherPathHashSet.Contains(path) == False: fs = None try: fs = FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read) ms = MemoryStream() buffer = Array.CreateInstance(Byte, fs.Length) bytesRead = fs.Read(buffer, 0, buffer.Length) while bytesRead > 0: ms.Write(buffer, 0, bytesRead) bytesRead = fs.Read(buffer, 0, buffer.Length) ms.Seek(0, SeekOrigin.Begin) weatherStreamList.Add(ms) finally: if fs is not None: fs.Close() weatherPathHashSet.Add(path) finally: if streamReader is not None: streamReader.Close() if stream is not None: stream.Close() if response is not None: response.Close() except Exception, e: Trace.WriteLine(e.clsException.Message) Trace.WriteLine(e.clsException.StackTrace)
def onLoad(): streamList1 = List[MemoryStream]() streamList2 = List[MemoryStream]() for i in range(10): ms = None fs = None try: fs = FileStream( String.Format("Assets\\Number-{0}.png", i.ToString( CultureInfo.InvariantCulture)), FileMode.Open, FileAccess.Read, FileShare.Read) ms = MemoryStream() buffer = Array.CreateInstance(Byte, fs.Length) bytesRead = fs.Read(buffer, 0, buffer.Length) while bytesRead > 0: ms.Write(buffer, 0, bytesRead) bytesRead = fs.Read(buffer, 0, buffer.Length) ms.Seek(0, SeekOrigin.Begin) streamList1.Add(ms) except: if ms is not None: ms.Close() ms = None finally: if fs is not None: fs.Close() for path in [ "Assets\\Hour.png", "Assets\\Minute.png", "Assets\\Second.png" ]: ms = None fs = None try: fs = FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read) ms = MemoryStream() buffer = Array.CreateInstance(Byte, fs.Length) bytesRead = fs.Read(buffer, 0, buffer.Length) while bytesRead > 0: ms.Write(buffer, 0, bytesRead) bytesRead = fs.Read(buffer, 0, buffer.Length) ms.Seek(0, SeekOrigin.Begin) streamList2.Add(ms) except: if ms is not None: ms.Close() ms = None finally: if fs is not None: fs.Close() return KeyValuePair[List[MemoryStream], List[MemoryStream]](streamList1, streamList2)