android.os.Parcel.dataSize()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(8.0k)|赞(0)|评价(0)|浏览(296)

本文整理了Java中android.os.Parcel.dataSize()方法的一些代码示例,展示了Parcel.dataSize()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Parcel.dataSize()方法的具体详情如下:
包路径:android.os.Parcel
类名称:Parcel
方法名:dataSize

Parcel.dataSize介绍

[英]Returns the total amount of data contained in the parcel.
[中]返回包裹中包含的数据总量。

代码示例

代码示例来源:origin: robolectric/robolectric

@Test
public void testDataSizeAfterSomeWrites() {
 parcel.writeInt(1);
 assertThat(parcel.dataSize()).isEqualTo(4);
 parcel.writeFloat(5);
 assertThat(parcel.dataSize()).isEqualTo(8);
 parcel.writeDouble(37);
 assertThat(parcel.dataSize()).isEqualTo(16);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testMarshallFailsFastReadingUninitializedData() {
 parcel.writeString("hello everyone");
 parcel.setDataSize(parcel.dataSize() + 4);
 parcel.setDataPosition(parcel.dataSize());
 parcel.writeInt(1);
 try {
  parcel.marshall();
  fail();
 } catch (UnreliableBehaviorError e) {
  assertThat(e).hasMessage("Reading uninitialized data at position 36");
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testInvalidReadFromUninitializedData() {
 // Write two longs with an 8-byte gap in the middle:
 parcel.writeLong(333L);
 parcel.setDataSize(parcel.dataSize() + 8);
 parcel.setDataPosition(parcel.dataSize());
 parcel.writeLong(444L);
 parcel.setDataPosition(0);
 assertThat(parcel.readLong()).isEqualTo(333L);
 try {
  parcel.readLong();
  fail("should have thrown UnreliableBehaviorError");
 } catch (UnreliableBehaviorError e) {
  assertThat(e).hasMessage("Reading uninitialized data at position 8");
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testByteArrayOfZeroesReadAsZeroes() {
 parcel.writeByteArray(new byte[15]);
 assertThat(parcel.dataSize()).isEqualTo(20);
 parcel.setDataPosition(0);
 assertThat(parcel.readInt()).isEqualTo(15);
 assertThat(parcel.readLong()).isEqualTo(0);
 assertThat(parcel.readLong()).isEqualTo(0);
 assertThat(parcel.dataPosition()).isEqualTo(20);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testUnmarshallEmpty() throws IOException {
 // Unmarshall an zero-length byte string, although, pass a non-empty array to make sure the
 // length/offset are respected.
 parcel.unmarshall(new byte[] {1, 2, 3}, 1, 0);
 assertThat(parcel.dataSize()).isEqualTo(0);
 assertThat(parcel.dataPosition()).isEqualTo(0);
 // Should not throw "Did you forget to setDataPosition(0)?" because it's still empty.
 assertThat(parcel.readInt()).isEqualTo(0);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testByteArrayToleratesZeroes() {
 parcel.writeInt(19); // Length
 parcel.writeInt(0); // These are zero
 parcel.writeLong(0); // This is zero
 parcel.writeFloat(0.0f); // This is zero
 parcel.writeByteArray(new byte[0]); // This is also zero
 assertThat(parcel.dataSize()).isEqualTo(24);
 parcel.setDataPosition(0);
 assertThat(parcel.createByteArray()).isEqualTo(new byte[19]);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testReadWriteLongerStringOddLength() {
 String val = "0123456789abcde";
 parcel.writeString(val);
 parcel.setDataPosition(0);
 assertThat(parcel.readString()).isEqualTo(val);
 assertWithMessage("4B length + 15*2B data + 2B null char")
   .that(parcel.dataSize())
   .isEqualTo(36);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testWriteEmptyString() {
 parcel.writeString("");
 parcel.setDataPosition(0);
 assertThat(parcel.readString()).isEmpty();
 assertWithMessage("4B length + 2B null char + 2B padding").that(parcel.dataSize()).isEqualTo(8);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testByteArrayOfZeroesCastedToZeroes() {
 parcel.writeByteArray(new byte[17]);
 assertThat(parcel.dataSize()).named("total size").isEqualTo(24);
 parcel.setDataPosition(0);
 assertThat(parcel.readInt()).isEqualTo(17);
 assertThat(parcel.readInt()).isEqualTo(0);
 assertThat(parcel.readFloat()).isEqualTo(0.0f);
 assertThat(parcel.createByteArray()).isEqualTo(new byte[0]);
 assertThat(parcel.readString()).isEqualTo("");
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testReadWriteSingleStringEvenLength() {
 String val = "test";
 parcel.writeString(val);
 parcel.setDataPosition(0);
 assertThat(parcel.readString()).isEqualTo(val);
 assertWithMessage("4B length + 4*2B data + 2B null char + 2B padding")
   .that(parcel.dataSize())
   .isEqualTo(16);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testWriteAndCreateNullByteArray() throws Exception {
 parcel.writeByteArray(null);
 assertThat(parcel.dataSize()).isEqualTo(4);
 parcel.setDataPosition(0);
 assertThat(parcel.createByteArray()).isNull();
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testWriteAndCreateByteArray_lengthZero() {
 byte[] bytes = new byte[] {};
 parcel.writeByteArray(bytes);
 assertThat(parcel.dataSize()).isEqualTo(4);
 parcel.setDataPosition(0);
 byte[] actualBytes = parcel.createByteArray();
 assertTrue(Arrays.equals(bytes, actualBytes));
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testWriteAndReadByteArray() {
 byte[] bytes = new byte[] { -1, 2, 3, 127 };
 parcel.writeByteArray(bytes);
 assertThat(parcel.dataSize()).isEqualTo(8);
 parcel.setDataPosition(0);
 byte[] actualBytes = new byte[bytes.length];
 parcel.readByteArray(actualBytes);
 assertTrue(Arrays.equals(bytes, actualBytes));
}

代码示例来源:origin: robolectric/robolectric

@Test(expected = RuntimeException.class)
public void testWriteAndReadByteArray_badLength() {
 byte[] bytes = new byte[] { -1, 2, 3, 127 };
 parcel.writeByteArray(bytes);
 assertThat(parcel.dataSize()).isEqualTo(8);
 parcel.setDataPosition(0);
 byte[] actualBytes = new byte[1];
 parcel.readByteArray(actualBytes);
}

代码示例来源:origin: robolectric/robolectric

private void assertInvariants(Parcel p) {
  assertWithMessage("capacity >= size").that(p.dataCapacity()).isAtLeast(p.dataSize());
  assertWithMessage("position <= size").that(p.dataPosition()).isAtMost(p.dataSize());
  assertWithMessage("available = size - position")
    .that(p.dataAvail())
    .isEqualTo(p.dataSize() - p.dataPosition());
  assertWithMessage("size % 4 == 0").that(p.dataSize() % 4).isEqualTo(0);
  assertWithMessage("capacity % 4 == 0").that(p.dataSize() % 4).isEqualTo(0);
 }
}

代码示例来源:origin: robolectric/robolectric

@Test(expected = RuntimeException.class)
public void testWriteAndReadByteArray_nullNotAllowed() {
 parcel.writeByteArray(null);
 assertThat(parcel.dataSize()).isEqualTo(4);
 parcel.setDataPosition(0);
 byte[] actualBytes = new byte[1];
 parcel.readByteArray(actualBytes);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testZeroCanBeCasted_8ByteTypesCanBeReadAs4Bytes() {
 parcel.writeLong(0);
 parcel.writeDouble(0.0);
 parcel.writeLong(0);
 assertThat(parcel.dataSize()).named("total size").isEqualTo(24);
 parcel.setDataPosition(0);
 assertThat(parcel.readInt()).isEqualTo(0);
 assertThat(parcel.readFloat()).isEqualTo(0.0f);
 assertThat(parcel.createByteArray()).isEqualTo(new byte[0]);
 assertThat(parcel.dataPosition()).isEqualTo(12);
 assertThat(parcel.readInt()).isEqualTo(0);
 assertThat(parcel.readFloat()).isEqualTo(0.0f);
 assertThat(parcel.createByteArray()).isEqualTo(new byte[0]);
 assertThat(parcel.dataPosition()).isEqualTo(24);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testWriteAndReadByteArray_overwrittenLength() {
 byte[] bytes = new byte[] {-1, 2, 3, 127};
 parcel.writeByteArray(bytes);
 assertThat(parcel.dataSize()).isEqualTo(8);
 parcel.setDataPosition(0);
 parcel.writeInt(3);
 parcel.setDataPosition(0);
 try {
  parcel.createByteArray();
  fail("expected exception");
 } catch (UnreliableBehaviorError e) {
  assertThat(e).hasMessage("Byte array's length prefix is 3 but real length is 4");
 }
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testWriteAndCreateByteArray_oddLength() {
 byte[] bytes = new byte[] {-1, 2, 3, 127, -128};
 parcel.writeByteArray(bytes);
 assertWithMessage("4B length + 5B data + 3B padding").that(parcel.dataSize()).isEqualTo(12);
 parcel.setDataPosition(0);
 assertThat(parcel.createByteArray()).isEqualTo(bytes);
}

代码示例来源:origin: robolectric/robolectric

@Test
public void testWriteAndCreateByteArray_multipleOf4() {
 byte[] bytes = new byte[] {-1, 2, 3, 127};
 parcel.writeByteArray(bytes);
 // Make sure that the parcel is not storing the original array.
 bytes[0] = 55;
 bytes[1] = 55;
 bytes[2] = 55;
 bytes[3] = 55;
 assertWithMessage("4B length + 4B data").that(parcel.dataSize()).isEqualTo(8);
 parcel.setDataPosition(0);
 byte[] actualBytes = parcel.createByteArray();
 assertThat(actualBytes).isEqualTo(new byte[] {-1, 2, 3, 127});
}

相关文章

Parcel类方法