本文整理汇总了Java中org.apache.beam.sdk.coders.Coder.Context方法的典型用法代码示例。如果您正苦于以下问题:Java Coder.Context方法的具体用法?Java Coder.Context怎么用?Java Coder.Context使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.beam.sdk.coders.Coder
的用法示例。
在下文中一共展示了Coder.Context方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: decode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@VisibleForTesting
static <T> T decode(
Coder<T> coder, Coder.Context context, byte[] bytes) throws CoderException, IOException {
@SuppressWarnings("unchecked")
Coder<T> deserializedCoder = SerializableUtils.clone(coder);
byte[] buffer;
if (context == Coder.Context.NESTED) {
buffer = new byte[bytes.length + 1];
System.arraycopy(bytes, 0, buffer, 0, bytes.length);
buffer[bytes.length] = 1;
} else {
buffer = bytes;
}
CountingInputStream cis = new CountingInputStream(new ByteArrayInputStream(buffer));
T value = deserializedCoder.decode(new UnownedInputStream(cis), context);
assertThat("consumed bytes equal to encoded bytes", cis.getCount(),
equalTo((long) bytes.length));
return value;
}
示例2: encode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@Override
public void encode(
InputOrAccum<InputT, AccumT> value, OutputStream outStream, Coder.Context context)
throws CoderException, IOException {
if (value.input != null) {
outStream.write(0);
inputCoder.encode(value.input, outStream, context);
} else {
outStream.write(1);
accumCoder.encode(value.accum, outStream, context);
}
}
示例3: decode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@Override
public InputOrAccum<InputT, AccumT> decode(InputStream inStream, Coder.Context context)
throws CoderException, IOException {
if (inStream.read() == 0) {
return InputOrAccum.<InputT, AccumT>input(inputCoder.decode(inStream, context));
} else {
return InputOrAccum.<InputT, AccumT>accum(accumCoder.decode(inStream, context));
}
}
示例4: encodeToSafeStream
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Encodes {@code value} to the given {@code stream}, which should be a stream that never throws
* {@code IOException}, such as {@code ByteArrayOutputStream} or
* {@link ExposedByteArrayOutputStream}.
*/
private static <T> void encodeToSafeStream(
Coder<T> coder, T value, OutputStream stream, Coder.Context context) throws CoderException {
try {
coder.encode(value, new UnownedOutputStream(stream), context);
} catch (IOException exn) {
Throwables.propagateIfPossible(exn, CoderException.class);
throw new IllegalArgumentException(
"Forbidden IOException when writing to OutputStream", exn);
}
}
示例5: decodeFromByteArray
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
public static <T> T decodeFromByteArray(
Coder<T> coder, byte[] encodedValue, Coder.Context context) throws CoderException {
try (ExposedByteArrayInputStream stream = new ExposedByteArrayInputStream(encodedValue)) {
T result = decodeFromSafeStream(coder, stream, context);
if (stream.available() != 0) {
throw new CoderException(
stream.available() + " unexpected extra bytes after decoding " + result);
}
return result;
}
}
示例6: decodeFromSafeStream
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Decodes a value from the given {@code stream}, which should be a stream that never throws
* {@code IOException}, such as {@code ByteArrayInputStream} or
* {@link ExposedByteArrayInputStream}.
*/
private static <T> T decodeFromSafeStream(
Coder<T> coder, InputStream stream, Coder.Context context) throws CoderException {
try {
return coder.decode(new UnownedInputStream(stream), context);
} catch (IOException exn) {
Throwables.propagateIfPossible(exn, CoderException.class);
throw new IllegalArgumentException(
"Forbidden IOException when reading from InputStream", exn);
}
}
示例7: coderDeterministicInContext
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Verifies that for the given {@code Coder<T>}, {@code Coder.Context}, and values of
* type {@code T}, if the values are equal then the encoded bytes are equal.
*/
public static <T> void coderDeterministicInContext(
Coder<T> coder, Coder.Context context, T value1, T value2)
throws Exception {
try {
coder.verifyDeterministic();
} catch (NonDeterministicException e) {
fail("Expected that the coder is deterministic");
}
assertThat("Expected that the passed in values are equal()", value1, equalTo(value2));
assertThat(
encode(coder, context, value1),
equalTo(encode(coder, context, value2)));
}
示例8: encode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@Override
public void encode(RandomAccessData value, OutputStream outStream, Coder.Context context)
throws CoderException, IOException {
if (value == POSITIVE_INFINITY) {
throw new CoderException("Positive infinity can not be encoded.");
}
if (!context.isWholeStream) {
VarInt.encode(value.size, outStream);
}
value.writeTo(outStream, 0, value.size);
}
示例9: coderDecodeEncodeContentsInSameOrder
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Verifies that for the given {@code Coder<Collection<T>>},
* and value of type {@code Collection<T>}, encoding followed by decoding yields an
* equal value of type {@code Collection<T>}, in any {@code Coder.Context}.
*/
public static <T, IterableT extends Iterable<T>> void coderDecodeEncodeContentsInSameOrder(
Coder<IterableT> coder, IterableT value)
throws Exception {
for (Coder.Context context : ALL_CONTEXTS) {
CoderProperties.<T, IterableT>coderDecodeEncodeContentsInSameOrderInContext(
coder, context, value);
}
}
示例10: coderDecodeEncodeContentsInSameOrderInContext
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Verifies that for the given {@code Coder<Iterable<T>>},
* and value of type {@code Iterable<T>}, encoding followed by decoding yields an
* equal value of type {@code Collection<T>}, in the given {@code Coder.Context}.
*/
@SuppressWarnings("unchecked")
public static <T, IterableT extends Iterable<T>> void
coderDecodeEncodeContentsInSameOrderInContext(
Coder<IterableT> coder, Coder.Context context, IterableT value)
throws Exception {
Iterable<T> result = decodeEncode(coder, context, value);
// Matchers.contains() requires at least one element
if (Iterables.isEmpty(value)) {
assertThat(result, emptyIterable());
} else {
// This is the only Matchers.contains() overload that takes literal values
assertThat(result, contains((T[]) Iterables.toArray(value, Object.class)));
}
}
示例11: coderConsistentWithEquals
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Verifies that for the given {@code Coder<T>} and values of
* type {@code T}, the values are equal if and only if the
* encoded bytes are equal.
*/
public static <T> void coderConsistentWithEquals(
Coder<T> coder, T value1, T value2)
throws Exception {
for (Coder.Context context : ALL_CONTEXTS) {
CoderProperties.<T>coderConsistentWithEqualsInContext(coder, context, value1, value2);
}
}
示例12: coderConsistentWithEqualsInContext
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Verifies that for the given {@code Coder<T>}, {@code Coder.Context}, and
* values of type {@code T}, the values are equal if and only if the
* encoded bytes are equal, in any {@code Coder.Context}.
*/
public static <T> void coderConsistentWithEqualsInContext(
Coder<T> coder, Coder.Context context, T value1, T value2) throws Exception {
assertEquals(
value1.equals(value2),
Arrays.equals(
encode(coder, context, value1),
encode(coder, context, value2)));
}
示例13: decode
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
@Override
public RandomAccessData decode(InputStream inStream, Coder.Context context)
throws CoderException, IOException {
RandomAccessData rval = new RandomAccessData();
if (!context.isWholeStream) {
int length = VarInt.decodeInt(inStream);
rval.readFrom(inStream, 0, length);
} else {
ByteStreams.copy(inStream, rval.asOutputStream());
}
return rval;
}
示例14: structuralValueConsistentWithEqualsInContext
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Verifies that for the given {@code Coder<T>}, {@code Coder.Context}, and
* values of type {@code T}, the structural values are equal if and only if the
* encoded bytes are equal, in any {@code Coder.Context}.
*/
public static <T> void structuralValueConsistentWithEqualsInContext(
Coder<T> coder, Coder.Context context, T value1, T value2) throws Exception {
assertEquals(
coder.structuralValue(value1).equals(coder.structuralValue(value2)),
Arrays.equals(
encode(coder, context, value1),
encode(coder, context, value2)));
}
示例15: structuralValueDecodeEncodeEqual
import org.apache.beam.sdk.coders.Coder; //导入方法依赖的package包/类
/**
* Verifies that for the given {@code Coder<T>} and value of type {@code T},
* the structural value is equal to the structural value yield by encoding
* and decoding the original value.
*
* <p>This is useful to test the correct implementation of a Coder structural
* equality with values that don't implement the equals contract.
*/
public static <T> void structuralValueDecodeEncodeEqual(
Coder<T> coder, T value)
throws Exception {
for (Coder.Context context : ALL_CONTEXTS) {
CoderProperties.<T>structuralValueDecodeEncodeEqualInContext(
coder, context, value);
}
}