ReadInt32() public static méthode

Reads a 32-bit integer in CBOR format from a data stream. If the object read is a floating-point number, it is truncated to an integer.
The end of the stream was /// reached, or the object read isn't a number, or can't fit a 32-bit /// integer. The parameter is null.
public static ReadInt32 ( Stream stream ) : int
stream Stream A data stream.
Résultat int
Exemple #1
0
 // [Test]
 public static void TestMiniCBOR()
 {
     byte[] bytes;
     bytes = new byte[] { 0x19, 2 };
     try {
         using (var memoryStream = new MemoryStream(bytes)) {
             MiniCBOR.ReadInt32(memoryStream);
         }
         Assert.Fail("Should have failed");
     } catch (IOException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     bytes = new byte[] { 0x1a, 2 };
     try {
         using (var memoryStream = new MemoryStream(bytes)) {
             MiniCBOR.ReadInt32(memoryStream);
         }
         Assert.Fail("Should have failed");
     } catch (IOException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     bytes = new byte[] { 0x1b, 2 };
     try {
         using (var ms = new MemoryStream(bytes)) {
             MiniCBOR.ReadInt32(ms);
         }
         Assert.Fail("Should have failed");
     } catch (IOException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     bytes = new byte[] { 0x1b, 2, 2, 2, 2, 2, 2, 2, 2 };
     try {
         using (var memoryStream = new MemoryStream(bytes)) {
             MiniCBOR.ReadInt32(memoryStream);
         }
         Assert.Fail("Should have failed");
     } catch (IOException) {
         // NOTE: Intentionally empty
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     bytes = new byte[] { 0x1c, 2 };
     try {
         using (var memoryStream = new MemoryStream(bytes)) {
             MiniCBOR.ReadInt32(memoryStream);
         }
         Assert.Fail("Should have failed");
     } catch (Exception ex) {
         Assert.Fail(ex.ToString());
         throw new InvalidOperationException(String.Empty, ex);
     }
     try {
         bytes = new byte[] { 0 };
         using (var ms = new MemoryStream(bytes)) {
             Assert.AreEqual(0, MiniCBOR.ReadInt32(ms));
         }
         bytes = new byte[] { 0x17 };
         using (var ms2 = new MemoryStream(bytes)) {
             Assert.AreEqual(0x17, MiniCBOR.ReadInt32(ms2));
         }
         bytes = new byte[] { 0x18, 2 };
         using (var ms3 = new MemoryStream(bytes)) {
             Assert.AreEqual(2, MiniCBOR.ReadInt32(ms3));
         }
         bytes = new byte[] { 0x19, 0, 2 };
         using (var ms4 = new MemoryStream(bytes)) {
             Assert.AreEqual(2, MiniCBOR.ReadInt32(ms4));
         }
         bytes = new byte[] { 0x27 };
         using (var ms5 = new MemoryStream(bytes)) {
             Assert.AreEqual(-1 - 7, MiniCBOR.ReadInt32(ms5));
         }
         bytes = new byte[] { 0x37 };
         using (var ms6 = new MemoryStream(bytes)) {
             Assert.AreEqual(-1 - 0x17, MiniCBOR.ReadInt32(ms6));
         }
     } catch (IOException ioex) {
         Assert.Fail(ioex.Message);
     }
 }