Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2921,12 +2921,19 @@ static BinaryReaderEx reader(BinaryContext ctx,

/**
* @param ctx Context.
* @param failIfUnregistered Flag to fail while writing object of unregistered type.
* @return Writer instance.
*/
public static BinaryWriterEx writer(BinaryContext ctx) {
public static BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistered) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add param to javadoc

BinaryThreadLocalContext locCtx = BinaryThreadLocalContext.get();

return new BinaryWriterExImpl(ctx, BinaryStreams.outputStream((int)CommonUtils.KB, locCtx.chunk()), locCtx.schemaHolder(), null);
return new BinaryWriterExImpl(
ctx,
BinaryStreams.outputStream((int)CommonUtils.KB, locCtx.chunk()),
locCtx.schemaHolder(),
null,
failIfUnregistered
);
}

/**
Expand All @@ -2935,7 +2942,7 @@ public static BinaryWriterEx writer(BinaryContext ctx) {
* @return Writer instance.
*/
public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) {
return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null);
return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null, false);
}

/**
Expand All @@ -2944,7 +2951,7 @@ public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out) {
* @return Writer instance.
*/
public static BinaryWriterEx writer(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema) {
return new BinaryWriterExImpl(ctx, out, schema, null);
return new BinaryWriterExImpl(ctx, out, schema, null, false);
}

/** @return Instance of caching handler. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ public interface BinaryWriterEx extends BinaryWriter, BinaryRawWriter, ObjectOut
*/
public boolean failIfUnregistered();

/**
* @param failIfUnregistered Fail if unregistered.
*/
public void failIfUnregistered(boolean failIfUnregistered);

/**
* @param obj Object.
* @throws org.apache.ignite.binary.BinaryObjectException In case of error.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,26 @@ class BinaryWriterExImpl implements BinaryWriterEx {
private BinaryInternalMapper mapper;

/** */
private boolean failIfUnregistered;
private final boolean failIfUnregistered;

/**
* @param ctx Context.
* @param out Output stream.
* @param handles Handles.
* @param failIfUnregistered Flag to fail while writing object of unregistered type.
*/
public BinaryWriterExImpl(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema, BinaryWriterHandles handles) {
public BinaryWriterExImpl(
BinaryContext ctx,
BinaryOutputStream out,
BinaryWriterSchemaHolder schema,
BinaryWriterHandles handles,
boolean failIfUnregistered
) {
this.ctx = ctx;
this.out = out;
this.schema = schema;
this.handles = handles;
this.failIfUnregistered = failIfUnregistered;

start = out.position();
}
Expand All @@ -104,11 +112,6 @@ public BinaryWriterExImpl(BinaryContext ctx, BinaryOutputStream out, BinaryWrite
return failIfUnregistered;
}

/** {@inheritDoc} */
@Override public void failIfUnregistered(boolean failIfUnregistered) {
this.failIfUnregistered = failIfUnregistered;
}

/** {@inheritDoc} */
@Override public void typeId(int typeId) {
this.typeId = typeId;
Expand Down Expand Up @@ -844,9 +847,7 @@ void writeBooleanField(@Nullable Boolean val) {
if (obj == null)
out.writeByte(GridBinaryMarshaller.NULL);
else {
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles());

writer.failIfUnregistered(failIfUnregistered);
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered);

writer.marshal(obj);
}
Expand All @@ -857,9 +858,7 @@ void writeBooleanField(@Nullable Boolean val) {
if (obj == null)
out.writeByte(GridBinaryMarshaller.NULL);
else {
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null);

writer.failIfUnregistered(failIfUnregistered);
BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null, failIfUnregistered);

writer.marshal(obj);
}
Expand Down Expand Up @@ -1538,9 +1537,7 @@ boolean tryWriteAsHandle(Object obj) {

/** {@inheritDoc} */
@Override public BinaryWriterEx newWriter(int typeId) {
BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles());

res.failIfUnregistered(failIfUnregistered);
BinaryWriterExImpl res = new BinaryWriterExImpl(ctx, out, schema, handles(), failIfUnregistered);

res.typeId(typeId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,7 @@ public byte[] marshal(@Nullable Object obj, boolean failIfUnregistered) throws B
if (obj == null)
return new byte[] { NULL };

try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) {
writer.failIfUnregistered(failIfUnregistered);

try (BinaryWriterEx writer = BinaryUtils.writer(ctx, failIfUnregistered)) {
writer.marshal(obj);

return writer.array();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ public BinaryObjectBuilderImpl(BinaryObjectEx obj) {

/** {@inheritDoc} */
@Override public BinaryObject build() {
try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) {
Thread curThread = Thread.currentThread();

if (curThread instanceof IgniteThread)
writer.failIfUnregistered(((IgniteThread)curThread).isForbiddenToRequestBinaryMetadata());
Thread curThread = Thread.currentThread();

try (BinaryWriterEx writer = BinaryUtils.writer(
ctx,
curThread instanceof IgniteThread && ((IgniteThread)curThread).isForbiddenToRequestBinaryMetadata())
) {
writer.typeId(typeId);

BinaryBuilderSerializer serializationCtx = new BinaryBuilderSerializer();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3019,7 +3019,7 @@ public void testThreadLocalArrayReleased() throws Exception {

BinaryMarshaller marsh = binaryMarshaller();

try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh))) {
try (BinaryWriterEx writer = BinaryUtils.writer(binaryContext(marsh), false)) {
assertEquals(true, BinaryStreamsTestUtils.threadLocalIsAcquired());

writer.writeString("Thread local test");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void test() throws Exception {

byte[] serializedTestObjectsBytes;

try (BinaryWriterEx writer = BinaryUtils.writer(ctx)) {
try (BinaryWriterEx writer = BinaryUtils.writer(ctx, false)) {
testObjects.forEach(writer::writeObject);

serializedTestObjectsBytes = writer.array();
Expand Down
Loading