http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableEnumArrayLazyValue.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableEnumArrayLazyValue.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableEnumArrayLazyValue.java
index 2e76d86..adc9959 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableEnumArrayLazyValue.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableEnumArrayLazyValue.java
@@ -96,7 +96,7 @@ class GridPortableEnumArrayLazyValue extends 
GridPortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         if (val != null) {
             if (clsName != null)
                 ctx.writeArray(writer, GridPortableMarshaller.ENUM_ARR, 
(Object[])val, clsName);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyArrayList.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyArrayList.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyArrayList.java
index a3c1294..5be228e 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyArrayList.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyArrayList.java
@@ -126,7 +126,7 @@ class GridPortableLazyArrayList extends 
AbstractList<Object> implements GridPort
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         if (delegate == null) {
             int size = reader.readIntAbsolute(off + 1);
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyLinkedList.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyLinkedList.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyLinkedList.java
index 6483f15..812d3e2 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyLinkedList.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyLinkedList.java
@@ -181,7 +181,7 @@ class GridPortableLazyLinkedList extends 
AbstractList<Object> implements GridPor
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         if (delegate == null) {
             int size = reader.readIntAbsolute(off + 1);
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMap.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMap.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMap.java
index 51cf777..46e90ee 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMap.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMap.java
@@ -79,7 +79,7 @@ class GridPortableLazyMap extends AbstractMap<Object, Object> 
implements GridPor
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         if (delegate == null) {
             int size = reader.readIntAbsolute(off + 1);
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMapEntry.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMapEntry.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMapEntry.java
index 169a667..c92665c 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMapEntry.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazyMapEntry.java
@@ -57,7 +57,7 @@ class GridPortableLazyMapEntry implements Map.Entry<Object, 
Object>, GridPortabl
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         writer.writeByte(GridPortableMarshaller.MAP_ENTRY);
 
         ctx.writeValue(writer, key);

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazySet.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazySet.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazySet.java
index ef3780d..c9a4f5b 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazySet.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableLazySet.java
@@ -44,7 +44,7 @@ class GridPortableLazySet extends 
GridPortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         if (val == null) {
             int size = reader.readIntAbsolute(off + 1);
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
index 9015425..67db65f 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
@@ -224,7 +224,7 @@ public class GridPortableMarshaller {
         if (obj == null)
             return new byte[] { NULL };
 
-        try (GridPortableWriterImpl writer = new GridPortableWriterImpl(ctx, 
off)) {
+        try (GridPortableWriterExImpl writer = new 
GridPortableWriterExImpl(ctx, off)) {
             writer.marshal(obj, false);
 
             return writer.array();
@@ -240,7 +240,7 @@ public class GridPortableMarshaller {
     @Nullable public <T> T unmarshal(byte[] bytes, @Nullable ClassLoader 
clsLdr) throws PortableException {
         assert bytes != null;
 
-        GridGridPortableReaderImpl reader = new 
GridGridPortableReaderImpl(ctx, bytes, 0, clsLdr);
+        GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx, bytes, 0, clsLdr);
 
         return (T)reader.unmarshal();
     }
@@ -269,7 +269,7 @@ public class GridPortableMarshaller {
         if (arr[0] == NULL)
             return null;
 
-        GridGridPortableReaderImpl reader = new 
GridGridPortableReaderImpl(ctx, arr, 0, ldr);
+        GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx, arr, 0, ldr);
 
         return (T)reader.deserialize();
     }
@@ -280,8 +280,8 @@ public class GridPortableMarshaller {
      * @param out Output stream.
      * @return Writer.
      */
-    public GridPortableWriterImpl writer(GridPortableOutputStream out) {
-        return new GridPortableWriterImpl(ctx, out, 0);
+    public GridPortableWriterExImpl writer(GridPortableOutputStream out) {
+        return new GridPortableWriterExImpl(ctx, out, 0);
     }
 
     /**
@@ -290,9 +290,9 @@ public class GridPortableMarshaller {
      * @param in Input stream.
      * @return Reader.
      */
-    public GridGridPortableReaderImpl reader(GridPortableInputStream in) {
+    public GridGridPortableReaderExImpl reader(GridPortableInputStream in) {
         // TODO: GG-10396 - Is class loader needed here?
-        return new GridGridPortableReaderImpl(ctx, in, in.position(), null);
+        return new GridGridPortableReaderExImpl(ctx, in, in.position(), null);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMetaDataCollector.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMetaDataCollector.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMetaDataCollector.java
index db651df..989f204 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMetaDataCollector.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMetaDataCollector.java
@@ -210,7 +210,7 @@ class GridPortableMetaDataCollector implements 
PortableWriter {
     /** {@inheritDoc} */
     @Override public PortableRawWriter rawWriter() {
         return 
(PortableRawWriter)Proxy.newProxyInstance(getClass().getClassLoader(),
-            new Class<?>[] { GridPortableRawWriter.class },
+            new Class<?>[] { GridPortableRawWriterEx.class },
             new InvocationHandler() {
                 @Override public Object invoke(Object proxy, Method mtd, 
Object[] args) throws Throwable {
                     return null;

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectArrayLazyValue.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectArrayLazyValue.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectArrayLazyValue.java
index 773e4cb..5557e4b 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectArrayLazyValue.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectArrayLazyValue.java
@@ -80,7 +80,7 @@ class GridPortableObjectArrayLazyValue extends 
GridPortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         if (clsName == null)
             ctx.writeArray(writer, GridPortableMarshaller.OBJ_ARR, 
lazyValsArr, compTypeId);
         else

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectImpl.java
index 5e50326..dcbc1b8 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectImpl.java
@@ -185,7 +185,7 @@ public final class GridPortableObjectImpl extends 
GridPortableObjectEx implement
     /**
      * @param ctx Context.
      */
-    void context(GridPortableContext ctx) {
+    public void context(GridPortableContext ctx) {
         this.ctx = ctx;
     }
 
@@ -225,7 +225,7 @@ public final class GridPortableObjectImpl extends 
GridPortableObjectEx implement
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override public <F> F field(String fieldName) throws 
PortableException {
-        GridGridPortableReaderImpl reader = new 
GridGridPortableReaderImpl(ctx, arr, start, null);
+        GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx, arr, start, null);
 
         return (F)reader.unmarshal(fieldName);
     }
@@ -233,7 +233,7 @@ public final class GridPortableObjectImpl extends 
GridPortableObjectEx implement
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override protected <F> F field(GridPortableReaderContext rCtx, 
String fieldName) {
-        GridGridPortableReaderImpl reader = new GridGridPortableReaderImpl(ctx,
+        GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx,
             new GridPortableHeapInputStream(arr),
             start,
             null,
@@ -244,7 +244,7 @@ public final class GridPortableObjectImpl extends 
GridPortableObjectEx implement
 
     /** {@inheritDoc} */
     @Override public boolean hasField(String fieldName) {
-        GridGridPortableReaderImpl reader = new 
GridGridPortableReaderImpl(ctx, arr, start, null);
+        GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx, arr, start, null);
 
         return reader.hasField(fieldName);
     }
@@ -256,7 +256,7 @@ public final class GridPortableObjectImpl extends 
GridPortableObjectEx implement
 
         if (obj0 == null) {
             // TODO: GG-10396 - Deserialize with proper class loader.
-            GridGridPortableReaderImpl reader = new 
GridGridPortableReaderImpl(ctx, arr, start, null);
+            GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx, arr, start, null);
 
             obj0 = reader.deserialize();
 

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectOffheapImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectOffheapImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectOffheapImpl.java
index d02f3fd..ba526d9 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectOffheapImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableObjectOffheapImpl.java
@@ -126,7 +126,7 @@ public class GridPortableObjectOffheapImpl extends 
GridPortableObjectEx implemen
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override public <F> F field(String fieldName) throws 
PortableException {
-        GridGridPortableReaderImpl reader = new GridGridPortableReaderImpl(ctx,
+        GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx,
             new GridPortableOffheapInputStream(ptr, size, false),
             start,
             null);
@@ -137,7 +137,7 @@ public class GridPortableObjectOffheapImpl extends 
GridPortableObjectEx implemen
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override protected <F> F field(GridPortableReaderContext rCtx, 
String fieldName) {
-        GridGridPortableReaderImpl reader = new GridGridPortableReaderImpl(ctx,
+        GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx,
             new GridPortableOffheapInputStream(ptr, size, false),
             start,
             null,
@@ -148,7 +148,7 @@ public class GridPortableObjectOffheapImpl extends 
GridPortableObjectEx implemen
 
     /** {@inheritDoc} */
     @Override public boolean hasField(String fieldName) {
-        GridGridPortableReaderImpl reader = new GridGridPortableReaderImpl(ctx,
+        GridGridPortableReaderExImpl reader = new 
GridGridPortableReaderExImpl(ctx,
             new GridPortableOffheapInputStream(ptr, size, false),
             start,
             null);
@@ -160,7 +160,7 @@ public class GridPortableObjectOffheapImpl extends 
GridPortableObjectEx implemen
     @SuppressWarnings("unchecked")
     @Nullable @Override public <T> T deserialize() throws PortableException {
         // TODO: GG-10396 - Deserialize with proper class loader.
-        GridGridPortableReaderImpl reader = new GridGridPortableReaderImpl(
+        GridGridPortableReaderExImpl reader = new GridGridPortableReaderExImpl(
             ctx,
             new GridPortableOffheapInputStream(ptr, size, false),
             start,

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainLazyValue.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainLazyValue.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainLazyValue.java
index beef2a2..c3ebcd4 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainLazyValue.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainLazyValue.java
@@ -41,7 +41,7 @@ class GridPortablePlainLazyValue extends 
GridPortableAbstractLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         writer.write(reader.array(), valOff, len);
     }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainPortableObject.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainPortableObject.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainPortableObject.java
index ff0e013..1e8e6f3 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainPortableObject.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortablePlainPortableObject.java
@@ -39,7 +39,7 @@ public class GridPortablePlainPortableObject implements 
GridPortableLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         PortableObject val = portableObj;
 
         if (val instanceof GridPortableObjectOffheapImpl)

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawReader.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawReader.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawReader.java
deleted file mode 100644
index 70e3849..0000000
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawReader.java
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ignite.internal.portable;
-
-import org.apache.ignite.portable.*;
-
-import org.jetbrains.annotations.*;
-
-/**
- * Extended reader interface.
- */
-public interface GridPortableRawReader extends PortableRawReader {
-    /**
-     * @return Object.
-     * @throws PortableException In case of error.
-     */
-    @Nullable public Object readObjectDetached() throws PortableException;
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawReaderEx.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawReaderEx.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawReaderEx.java
new file mode 100644
index 0000000..c01fd81
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawReaderEx.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.portable;
+
+import org.apache.ignite.portable.*;
+
+import org.jetbrains.annotations.*;
+
+/**
+ * Extended reader interface.
+ */
+public interface GridPortableRawReaderEx extends PortableRawReader {
+    /**
+     * @return Object.
+     * @throws PortableException In case of error.
+     */
+    @Nullable public Object readObjectDetached() throws PortableException;
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawWriter.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawWriter.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawWriter.java
deleted file mode 100644
index 11c2b55..0000000
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawWriter.java
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License.  You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.ignite.internal.portable;
-
-import org.apache.ignite.internal.processors.portable.*;
-import org.apache.ignite.portable.*;
-
-import org.jetbrains.annotations.*;
-
-/**
- * Extended writer interface.
- */
-public interface GridPortableRawWriter extends PortableRawWriter, 
AutoCloseable {
-    /**
-     * @param obj Object to write.
-     * @throws PortableException In case of error.
-     */
-    public void writeObjectDetached(@Nullable Object obj) throws 
PortableException;
-
-    /**
-     * @return Output stream.
-     */
-    public GridPortableOutputStream out();
-
-    /**
-     * Cleans resources.
-     */
-    @Override public void close();
-}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawWriterEx.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawWriterEx.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawWriterEx.java
new file mode 100644
index 0000000..c0b2f9f
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableRawWriterEx.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.portable;
+
+import org.apache.ignite.internal.processors.portable.*;
+import org.apache.ignite.portable.*;
+
+import org.jetbrains.annotations.*;
+
+/**
+ * Extended writer interface.
+ */
+public interface GridPortableRawWriterEx extends PortableRawWriter, 
AutoCloseable {
+    /**
+     * @param obj Object to write.
+     * @throws PortableException In case of error.
+     */
+    public void writeObjectDetached(@Nullable Object obj) throws 
PortableException;
+
+    /**
+     * @return Output stream.
+     */
+    public GridPortableOutputStream out();
+
+    /**
+     * Cleans resources.
+     */
+    @Override public void close();
+}

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableUtils.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableUtils.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableUtils.java
index a975a5e..cb4d246 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableUtils.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableUtils.java
@@ -135,7 +135,7 @@ public class GridPortableUtils {
      * @param writer W
      * @param val Value.
      */
-    public static void writePlainObject(GridPortableWriterImpl writer, Object 
val) {
+    public static void writePlainObject(GridPortableWriterExImpl writer, 
Object val) {
         Byte flag = PLAIN_CLASS_TO_FLAG.get(val.getClass());
 
         if (flag == null)

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableValueWithType.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableValueWithType.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableValueWithType.java
index 5b4e6c3..8d696e5 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableValueWithType.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableValueWithType.java
@@ -40,7 +40,7 @@ class GridPortableValueWithType implements 
GridPortableLazyValue {
     }
 
     /** {@inheritDoc} */
-    @Override public void writeTo(GridPortableWriterImpl writer, 
GridPortableBuilderSerializer ctx) {
+    @Override public void writeTo(GridPortableWriterExImpl writer, 
GridPortableBuilderSerializer ctx) {
         if (val instanceof GridPortableBuilderSerializationAware)
             ((GridPortableBuilderSerializationAware)val).writeTo(writer, ctx);
         else

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/96981338/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableWriterExImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableWriterExImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableWriterExImpl.java
new file mode 100644
index 0000000..f5df6d6
--- /dev/null
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableWriterExImpl.java
@@ -0,0 +1,1770 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.portable;
+
+import org.apache.ignite.*;
+import org.apache.ignite.internal.portable.streams.*;
+import org.apache.ignite.internal.processors.portable.*;
+import org.apache.ignite.internal.util.typedef.internal.*;
+import org.apache.ignite.portable.*;
+
+import org.jetbrains.annotations.*;
+
+import java.io.*;
+import java.lang.reflect.*;
+import java.math.*;
+import java.sql.*;
+import java.util.*;
+import java.util.Date;
+import java.util.concurrent.*;
+
+import static java.nio.charset.StandardCharsets.*;
+import static org.apache.ignite.internal.portable.GridPortableMarshaller.*;
+
+ /**
+ * Portable writer implementation.
+ */
+public class GridPortableWriterExImpl implements PortableWriter, 
GridPortableRawWriterEx, ObjectOutput {
+    /** Length: integer. */
+    private static final int LEN_INT = 4;
+
+    /** */
+    private static final int INIT_CAP = 1024;
+
+    /** */
+    private static final ConcurrentHashMap<Class<?>, Boolean> useOptMarshCache 
= new ConcurrentHashMap<>();
+
+    /** */
+    private final GridPortableContext ctx;
+
+    /** */
+    private final WriterContext wCtx;
+
+    /** */
+    private final int start;
+
+    /** */
+    private int mark;
+
+    /** */
+    private Class<?> cls;
+
+    /** */
+    private int typeId;
+
+    /** */
+    private boolean allowFields = true;
+
+    /** */
+    private boolean metaEnabled;
+
+    /** */
+    private int metaHashSum;
+
+    /**
+     * @param ctx Context.
+     * @param off Start offset.
+     */
+    GridPortableWriterExImpl(GridPortableContext ctx, int off) {
+        this.ctx = ctx;
+
+        GridPortableOutputStream out = new GridPortableHeapOutputStream(off + 
INIT_CAP);
+
+        out.position(off);
+
+        wCtx = new WriterContext(out, null);
+
+        start = off;
+    }
+
+    /**
+     * @param ctx Context.
+     * @param out Output stream.
+     * @param off Start offset.
+     */
+    GridPortableWriterExImpl(GridPortableContext ctx, GridPortableOutputStream 
out, int off) {
+        this.ctx = ctx;
+
+        wCtx = new WriterContext(out, null);
+
+        start = off;
+    }
+
+    /**
+     * @param ctx Context.
+     * @param off Start offset.
+     * @param typeId Type ID.
+     */
+    GridPortableWriterExImpl(GridPortableContext ctx, int off, int typeId, 
boolean metaEnabled) {
+        this(ctx, off);
+
+        this.typeId = typeId;
+
+        this.metaEnabled = metaEnabled;
+    }
+
+    /**
+     * @param ctx Context.
+     * @param wCtx Writer context.
+     */
+    private GridPortableWriterExImpl(GridPortableContext ctx, WriterContext 
wCtx) {
+        this.ctx = ctx;
+        this.wCtx = wCtx;
+
+        start = wCtx.out.position();
+    }
+
+    /**
+     * Close the writer releasing resources if necessary.
+     */
+    @Override public void close() {
+        wCtx.out.close();
+    }
+
+    /**
+     * @return Meta data hash sum or {@code null} if meta data is disabled.
+     */
+    @Nullable Integer metaDataHashSum() {
+        return metaEnabled ? metaHashSum : null;
+    }
+
+    /**
+     * @param obj Object.
+     * @param detached Detached or not.
+     * @throws PortableException In case of error.
+     */
+    void marshal(Object obj, boolean detached) throws PortableException {
+        assert obj != null;
+
+        if (useOptimizedMarshaller(obj)) {
+            writeByte(OPTM_MARSH);
+
+            try {
+                byte[] arr = ctx.optimizedMarsh().marshal(obj);
+
+                writeInt(arr.length);
+
+                write(arr);
+            }
+            catch (IgniteCheckedException e) {
+                throw new PortableException("Failed to marshal object with 
optimized marshaller: " + obj, e);
+            }
+
+            return;
+        }
+
+        cls = obj.getClass();
+
+        GridPortableClassDescriptor desc = ctx.descriptorForClass(cls);
+
+        if (desc == null)
+            throw new PortableException("Object is not portable: [class=" + 
cls + ']');
+
+        if (desc.excluded()) {
+            doWriteByte(NULL);
+            return;
+        }
+
+        if (desc.getWriteReplaceMethod() != null) {
+            Object replace;
+
+            try {
+                replace = desc.getWriteReplaceMethod().invoke(obj);
+            }
+            catch (IllegalAccessException e) {
+                throw new RuntimeException(e);
+            }
+            catch (InvocationTargetException e) {
+                if (e.getTargetException() instanceof PortableException)
+                    throw (PortableException)e.getTargetException();
+
+                throw new PortableException("Failed to execute writeReplace() 
method on " + obj, e);
+            }
+
+            if (replace == null) {
+                doWriteByte(NULL);
+                return;
+            }
+
+            if (cls != replace.getClass()) {
+                cls = replace.getClass();
+
+                desc = ctx.descriptorForClass(cls);
+
+                if (desc == null)
+                    throw new PortableException("Object is not portable: 
[class=" + cls + ']');
+            }
+
+            obj = replace;
+        }
+
+        typeId = desc.typeId();
+
+        metaEnabled = ctx.isMetaDataEnabled(typeId);
+
+        if (detached)
+            wCtx.resetHandles();
+
+        desc.write(obj, this);
+    }
+
+     /**
+      * Determines whether to use {@link 
org.apache.ignite.marshaller.optimized.OptimizedMarshaller} for serialization
+      * or not.
+      *
+      * @param obj Object to serialize.
+      * @return {@code true} if to use, {@code false} otherwise.
+      */
+     private boolean useOptimizedMarshaller(Object obj) {
+         Class<?> cls = obj.getClass();
+
+         Boolean use = useOptMarshCache.get(cls);
+
+         if (use != null)
+             return use;
+
+         if (ctx.isPredefinedClass(cls))
+             use = false;
+         else {
+             try {
+                 Method writeObj = cls.getDeclaredMethod("writeObject", 
ObjectOutputStream.class);
+                 Method readObj = cls.getDeclaredMethod("readObject", 
ObjectInputStream.class);
+
+                 if (!Modifier.isStatic(writeObj.getModifiers()) && 
!Modifier.isStatic(readObj.getModifiers()) &&
+                     writeObj.getReturnType() == void.class && 
readObj.getReturnType() == void.class)
+                     use = true;
+                 else
+                     use = false;
+
+             } catch (NoSuchMethodException e) {
+                 use = false;
+             }
+         }
+
+         useOptMarshCache.putIfAbsent(cls, use);
+
+         return use;
+     }
+
+    /**
+     * @param obj Object.
+     * @return Handle.
+     */
+    int handle(Object obj) {
+        assert obj != null;
+
+        return wCtx.handle(obj);
+    }
+
+    /**
+     * @return Array.
+     */
+    byte[] array() {
+        return wCtx.out.arrayCopy();
+    }
+
+    /**
+     * @return Output stream.
+     */
+    GridPortableOutputStream outputStream() {
+        return wCtx.out;
+    }
+
+    /**
+     * @return Stream current position.
+     */
+    int position() {
+        return wCtx.out.position();
+    }
+
+    /**
+     * Sets new position.
+     *
+     * @param pos Position.
+     */
+    void position(int pos) {
+        wCtx.out.position(pos);
+    }
+
+     /**
+     * @param bytes Number of bytes to reserve.
+     * @return Offset.
+     */
+    int reserve(int bytes) {
+        int pos = wCtx.out.position();
+
+        wCtx.out.position(pos + bytes);
+
+        return pos;
+    }
+
+    /**
+     * @param bytes Number of bytes to reserve.
+     * @return Offset.
+     */
+    int reserveAndMark(int bytes) {
+        int off0 = reserve(bytes);
+
+        mark = wCtx.out.position();
+
+        return off0;
+    }
+
+    /**
+     * @param off Offset.
+     */
+    void writeDelta(int off) {
+        wCtx.out.writeInt(off, wCtx.out.position() - mark);
+    }
+
+    /**
+     *
+     */
+    void writeLength() {
+        wCtx.out.writeInt(start + TOTAL_LEN_POS, wCtx.out.position() - start);
+    }
+
+    /**
+     *
+     */
+    void writeRawOffsetIfNeeded() {
+        if (allowFields)
+            wCtx.out.writeInt(start + RAW_DATA_OFF_POS, wCtx.out.position() - 
start);
+    }
+
+    /**
+     * @param val Byte array.
+     */
+    public void write(byte[] val) {
+        assert val != null;
+
+        wCtx.out.writeByteArray(val);
+    }
+
+    /**
+     * @param val Byte array.
+     * @param off Offset.
+     * @param len Length.
+     */
+    public void write(byte[] val, int off, int len) {
+        assert val != null;
+
+        wCtx.out.write(val, off, len);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteByte(byte val) {
+        wCtx.out.writeByte(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteShort(short val) {
+        wCtx.out.writeShort(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteInt(int val) {
+        wCtx.out.writeInt(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteLong(long val) {
+        wCtx.out.writeLong(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteFloat(float val) {
+        wCtx.out.writeFloat(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteDouble(double val) {
+        wCtx.out.writeDouble(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteChar(char val) {
+        wCtx.out.writeChar(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteBoolean(boolean val) {
+        wCtx.out.writeBoolean(val);
+    }
+
+    /**
+     * @param val String value.
+     */
+    void doWriteDecimal(@Nullable BigDecimal val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(DECIMAL);
+
+            BigInteger intVal = val.unscaledValue();
+
+            if (intVal.signum() == -1) {
+                intVal = intVal.negate();
+
+                wCtx.out.writeInt(val.scale() | 0x80000000);
+            }
+            else
+                wCtx.out.writeInt(val.scale());
+
+            byte[] vals = intVal.toByteArray();
+
+            wCtx.out.writeInt(vals.length);
+            wCtx.out.writeByteArray(vals);
+        }
+    }
+
+    /**
+     * @param val String value.
+     */
+    void doWriteString(@Nullable String val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(STRING);
+
+            if (ctx.isConvertString()) {
+                doWriteBoolean(true);
+
+                byte[] strArr = val.getBytes(UTF_8);
+
+                doWriteInt(strArr.length);
+
+                wCtx.out.writeByteArray(strArr);
+            }
+            else {
+                doWriteBoolean(false);
+
+                char[] strArr = val.toCharArray();
+
+                doWriteInt(strArr.length);
+
+                wCtx.out.writeCharArray(strArr);
+            }
+        }
+    }
+
+    /**
+     * @param uuid UUID.
+     */
+    void doWriteUuid(@Nullable UUID uuid) {
+        if (uuid == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(UUID);
+            doWriteLong(uuid.getMostSignificantBits());
+            doWriteLong(uuid.getLeastSignificantBits());
+        }
+    }
+
+    /**
+     * @param date Date.
+     */
+    void doWriteDate(@Nullable Date date) {
+        if (date == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(DATE);
+            doWriteLong(date.getTime());
+            doWriteInt(0);
+        }
+    }
+
+    /**
+     * @param ts Timestamp.
+     */
+    void doWriteTimestamp(@Nullable Timestamp ts) {
+        if (ts == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(DATE);
+            doWriteLong(ts.getTime());
+            doWriteInt(ts.getNanos() % 1000000);
+        }
+    }
+
+    /**
+     * @param obj Object.
+     * @param detached Detached or not.
+     * @throws PortableException In case of error.
+     */
+    void doWriteObject(@Nullable Object obj, boolean detached) throws 
PortableException {
+        if (obj == null)
+            doWriteByte(NULL);
+        else {
+            WriterContext wCtx = detached ? new WriterContext(this.wCtx.out, 
this.wCtx.handles) : this.wCtx;
+
+            GridPortableWriterExImpl writer = new 
GridPortableWriterExImpl(ctx, wCtx);
+
+            writer.marshal(obj, detached);
+
+            if (detached)
+                this.wCtx.out = wCtx.out;
+        }
+    }
+
+    /**
+     * @param val Byte array.
+     */
+    void doWriteByteArray(@Nullable byte[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(BYTE_ARR);
+            doWriteInt(val.length);
+
+            wCtx.out.writeByteArray(val);
+        }
+    }
+
+    /**
+     * @param val Short array.
+     */
+    void doWriteShortArray(@Nullable short[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(SHORT_ARR);
+            doWriteInt(val.length);
+
+            wCtx.out.writeShortArray(val);
+        }
+    }
+
+    /**
+     * @param val Integer array.
+     */
+    void doWriteIntArray(@Nullable int[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(INT_ARR);
+            doWriteInt(val.length);
+
+            wCtx.out.writeIntArray(val);
+        }
+    }
+
+    /**
+     * @param val Long array.
+     */
+    void doWriteLongArray(@Nullable long[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(LONG_ARR);
+            doWriteInt(val.length);
+
+            wCtx.out.writeLongArray(val);
+        }
+    }
+
+    /**
+     * @param val Float array.
+     */
+    void doWriteFloatArray(@Nullable float[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(FLOAT_ARR);
+            doWriteInt(val.length);
+
+            wCtx.out.writeFloatArray(val);
+        }
+    }
+
+    /**
+     * @param val Double array.
+     */
+    void doWriteDoubleArray(@Nullable double[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(DOUBLE_ARR);
+            doWriteInt(val.length);
+
+            wCtx.out.writeDoubleArray(val);
+        }
+    }
+
+    /**
+     * @param val Char array.
+     */
+    void doWriteCharArray(@Nullable char[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(CHAR_ARR);
+            doWriteInt(val.length);
+
+            wCtx.out.writeCharArray(val);
+        }
+    }
+
+    /**
+     * @param val Boolean array.
+     */
+    void doWriteBooleanArray(@Nullable boolean[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(BOOLEAN_ARR);
+            doWriteInt(val.length);
+
+            wCtx.out.writeBooleanArray(val);
+        }
+    }
+
+    /**
+     * @param val Array of strings.
+     */
+    void doWriteDecimalArray(@Nullable BigDecimal[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(DECIMAL_ARR);
+            doWriteInt(val.length);
+
+            for (BigDecimal str : val)
+                doWriteDecimal(str);
+        }
+    }
+
+    /**
+     * @param val Array of strings.
+     */
+    void doWriteStringArray(@Nullable String[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(STRING_ARR);
+            doWriteInt(val.length);
+
+            for (String str : val)
+                doWriteString(str);
+        }
+    }
+
+    /**
+     * @param val Array of UUIDs.
+     */
+    void doWriteUuidArray(@Nullable UUID[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(UUID_ARR);
+            doWriteInt(val.length);
+
+            for (UUID uuid : val)
+                doWriteUuid(uuid);
+        }
+    }
+
+    /**
+     * @param val Array of dates.
+     */
+    void doWriteDateArray(@Nullable Date[] val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(DATE_ARR);
+            doWriteInt(val.length);
+
+            for (Date date : val)
+                doWriteDate(date);
+        }
+    }
+
+    /**
+     * @param val Array of objects.
+     * @throws PortableException In case of error.
+     */
+    void doWriteObjectArray(@Nullable Object[] val) throws PortableException {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            GridPortableContext.Type type = 
ctx.typeId(val.getClass().getComponentType());
+
+            doWriteByte(OBJ_ARR);
+
+            if (type.registered())
+                doWriteInt(type.id());
+            else {
+                doWriteInt(UNREGISTERED_TYPE_ID);
+                doWriteString(val.getClass().getComponentType().getName());
+            }
+
+            doWriteInt(val.length);
+
+            for (Object obj : val)
+                doWriteObject(obj, false);
+        }
+    }
+
+    /**
+     * @param col Collection.
+     * @throws PortableException In case of error.
+     */
+    void doWriteCollection(@Nullable Collection<?> col) throws 
PortableException {
+        if (col == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(COL);
+            doWriteInt(col.size());
+            doWriteByte(ctx.collectionType(col.getClass()));
+
+            for (Object obj : col)
+                doWriteObject(obj, false);
+        }
+    }
+
+    /**
+     * @param map Map.
+     * @throws PortableException In case of error.
+     */
+    void doWriteMap(@Nullable Map<?, ?> map) throws PortableException {
+        if (map == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(MAP);
+            doWriteInt(map.size());
+            doWriteByte(ctx.mapType(map.getClass()));
+
+            for (Map.Entry<?, ?> e : map.entrySet()) {
+                doWriteObject(e.getKey(), false);
+                doWriteObject(e.getValue(), false);
+            }
+        }
+    }
+
+    /**
+     * @param e Map entry.
+     * @throws PortableException In case of error.
+     */
+    void doWriteMapEntry(@Nullable Map.Entry<?, ?> e) throws PortableException 
{
+        if (e == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(MAP_ENTRY);
+            doWriteObject(e.getKey(), false);
+            doWriteObject(e.getValue(), false);
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void doWriteEnum(@Nullable Enum<?> val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            GridPortableContext.Type type = ctx.typeId(val.getClass());
+
+            doWriteByte(ENUM);
+
+            if (type.registered())
+                doWriteInt(type.id());
+            else {
+                doWriteInt(UNREGISTERED_TYPE_ID);
+                doWriteString(val.getClass().getName());
+            }
+
+            doWriteInt(val.ordinal());
+        }
+    }
+
+    /**
+     * @param val Array.
+     */
+    void doWriteEnumArray(@Nullable Object[] val) {
+        assert val == null || val.getClass().getComponentType().isEnum();
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            GridPortableContext.Type type = 
ctx.typeId(val.getClass().getComponentType());
+
+            doWriteByte(ENUM_ARR);
+
+            if (type.registered())
+                doWriteInt(type.id());
+            else {
+                doWriteInt(UNREGISTERED_TYPE_ID);
+                doWriteString(val.getClass().getComponentType().getName());
+            }
+
+            doWriteInt(val.length);
+
+            // TODO: Denis: Redundant data for each element of the array.
+            for (Object o : val)
+                doWriteEnum((Enum<?>)o);
+        }
+    }
+
+    /**
+     * @param val Class.
+     */
+    void doWriteClass(@Nullable Class val) {
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            GridPortableContext.Type type = ctx.typeId(val);
+
+            doWriteByte(CLASS);
+
+            if (type.registered())
+                doWriteInt(type.id());
+            else {
+                doWriteInt(UNREGISTERED_TYPE_ID);
+                doWriteString(val.getClass().getName());
+            }
+        }
+    }
+
+    /**
+     * @param po Portable object.
+     */
+    void doWritePortableObject(@Nullable GridPortableObjectImpl po) {
+        if (po == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(PORTABLE_OBJ);
+
+            byte[] poArr = po.array();
+
+            doWriteInt(poArr.length);
+
+            wCtx.out.writeByteArray(poArr);
+
+            doWriteInt(po.start());
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeByteField(@Nullable Byte val) {
+        doWriteInt(val != null ? 2 : 1);
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(BYTE);
+            doWriteByte(val);
+        }
+    }
+
+    /**
+     * @param val Class.
+     */
+    void writeClassField(@Nullable Class val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteClass(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeShortField(@Nullable Short val) {
+        doWriteInt(val != null ? 3 : 1);
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(SHORT);
+            doWriteShort(val);
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeIntField(@Nullable Integer val) {
+        doWriteInt(val != null ? 5 : 1);
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(INT);
+            doWriteInt(val);
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeLongField(@Nullable Long val) {
+        doWriteInt(val != null ? 9 : 1);
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(LONG);
+            doWriteLong(val);
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeFloatField(@Nullable Float val) {
+        doWriteInt(val != null ? 5 : 1);
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(FLOAT);
+            doWriteFloat(val);
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeDoubleField(@Nullable Double val) {
+        doWriteInt(val != null ? 9 : 1);
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(DOUBLE);
+            doWriteDouble(val);
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeCharField(@Nullable Character val) {
+        doWriteInt(val != null ? 3 : 1);
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(CHAR);
+            doWriteChar(val);
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeBooleanField(@Nullable Boolean val) {
+        doWriteInt(val != null ? 2 : 1);
+
+        if (val == null)
+            doWriteByte(NULL);
+        else {
+            doWriteByte(BOOLEAN);
+            doWriteBoolean(val);
+        }
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeDecimalField(@Nullable BigDecimal val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteDecimal(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeStringField(@Nullable String val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteString(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeUuidField(@Nullable UUID val) {
+        doWriteInt(val != null ? 17 : 1);
+        doWriteUuid(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeDateField(@Nullable Date val) {
+        doWriteInt(val != null ? 13 : 1);
+        doWriteDate(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeTimestampField(@Nullable Timestamp val) {
+        doWriteInt(val != null ? 13 : 1);
+        doWriteTimestamp(val);
+    }
+
+    /**
+     * @param obj Object.
+     * @throws PortableException In case of error.
+     */
+    void writeObjectField(@Nullable Object obj) throws PortableException {
+        int lenPos = reserveAndMark(4);
+
+        doWriteObject(obj, false);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeByteArrayField(@Nullable byte[] val) {
+        doWriteInt(val != null ? 5 + val.length : 1);
+        doWriteByteArray(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeShortArrayField(@Nullable short[] val) {
+        doWriteInt(val != null ? 5 + (val.length << 1) : 1);
+        doWriteShortArray(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeIntArrayField(@Nullable int[] val) {
+        doWriteInt(val != null ? 5 + (val.length << 2) : 1);
+        doWriteIntArray(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeLongArrayField(@Nullable long[] val) {
+        doWriteInt(val != null ? 5 + (val.length << 3) : 1);
+        doWriteLongArray(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeFloatArrayField(@Nullable float[] val) {
+        doWriteInt(val != null ? 5 + (val.length << 2) : 1);
+        doWriteFloatArray(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeDoubleArrayField(@Nullable double[] val) {
+        doWriteInt(val != null ? 5 + (val.length << 3) : 1);
+        doWriteDoubleArray(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeCharArrayField(@Nullable char[] val) {
+        doWriteInt(val != null ? 5 + (val.length << 1) : 1);
+        doWriteCharArray(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeBooleanArrayField(@Nullable boolean[] val) {
+        doWriteInt(val != null ? 5 + val.length : 1);
+        doWriteBooleanArray(val);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeDecimalArrayField(@Nullable BigDecimal[] val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteDecimalArray(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeStringArrayField(@Nullable String[] val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteStringArray(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeUuidArrayField(@Nullable UUID[] val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteUuidArray(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeDateArrayField(@Nullable Date[] val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteDateArray(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     * @throws PortableException In case of error.
+     */
+    void writeObjectArrayField(@Nullable Object[] val) throws 
PortableException {
+        int lenPos = reserveAndMark(4);
+
+        doWriteObjectArray(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param col Collection.
+     * @throws PortableException In case of error.
+     */
+    void writeCollectionField(@Nullable Collection<?> col) throws 
PortableException {
+        int lenPos = reserveAndMark(4);
+
+        doWriteCollection(col);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param map Map.
+     * @throws PortableException In case of error.
+     */
+    void writeMapField(@Nullable Map<?, ?> map) throws PortableException {
+        int lenPos = reserveAndMark(4);
+
+        doWriteMap(map);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param e Map entry.
+     * @throws PortableException In case of error.
+     */
+    void writeMapEntryField(@Nullable Map.Entry<?, ?> e) throws 
PortableException {
+        int lenPos = reserveAndMark(4);
+
+        doWriteMapEntry(e);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeEnumField(@Nullable Enum<?> val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteEnum(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param val Value.
+     */
+    void writeEnumArrayField(@Nullable Object[] val) {
+        int lenPos = reserveAndMark(4);
+
+        doWriteEnumArray(val);
+
+        writeDelta(lenPos);
+    }
+
+    /**
+     * @param po Portable object.
+     * @throws PortableException In case of error.
+     */
+    void writePortableObjectField(@Nullable GridPortableObjectImpl po) throws 
PortableException {
+        int lenPos = reserveAndMark(4);
+
+        doWritePortableObject(po);
+
+        writeDelta(lenPos);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeByte(String fieldName, byte val) throws 
PortableException {
+        writeFieldId(fieldName, BYTE);
+        writeByteField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeByte(byte val) throws PortableException {
+        doWriteByte(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeShort(String fieldName, short val) throws 
PortableException {
+        writeFieldId(fieldName, SHORT);
+        writeShortField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeShort(short val) throws PortableException {
+        doWriteShort(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeInt(String fieldName, int val) throws 
PortableException {
+        writeFieldId(fieldName, INT);
+        writeIntField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeInt(int val) throws PortableException {
+        doWriteInt(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeLong(String fieldName, long val) throws 
PortableException {
+        writeFieldId(fieldName, LONG);
+        writeLongField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeLong(long val) throws PortableException {
+        doWriteLong(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeFloat(String fieldName, float val) throws 
PortableException {
+        writeFieldId(fieldName, FLOAT);
+        writeFloatField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeFloat(float val) throws PortableException {
+        doWriteFloat(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDouble(String fieldName, double val) throws 
PortableException {
+        writeFieldId(fieldName, DOUBLE);
+        writeDoubleField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDouble(double val) throws PortableException {
+        doWriteDouble(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeChar(String fieldName, char val) throws 
PortableException {
+        writeFieldId(fieldName, CHAR);
+        writeCharField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeChar(char val) throws PortableException {
+        doWriteChar(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeBoolean(String fieldName, boolean val) throws 
PortableException {
+        writeFieldId(fieldName, BOOLEAN);
+        writeBooleanField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeBoolean(boolean val) throws PortableException {
+        doWriteBoolean(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDecimal(String fieldName, @Nullable BigDecimal 
val) throws PortableException {
+        writeFieldId(fieldName, DECIMAL);
+        writeDecimalField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDecimal(@Nullable BigDecimal val) throws 
PortableException {
+        doWriteDecimal(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeString(String fieldName, @Nullable String val) 
throws PortableException {
+        writeFieldId(fieldName, STRING);
+        writeStringField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeString(@Nullable String val) throws 
PortableException {
+        doWriteString(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeUuid(String fieldName, @Nullable UUID val) 
throws PortableException {
+        writeFieldId(fieldName, UUID);
+        writeUuidField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeUuid(@Nullable UUID val) throws 
PortableException {
+        doWriteUuid(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDate(String fieldName, @Nullable Date val) 
throws PortableException {
+        writeFieldId(fieldName, DATE);
+        writeDateField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDate(@Nullable Date val) throws 
PortableException {
+        doWriteDate(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeTimestamp(String fieldName, @Nullable Timestamp 
val) throws PortableException {
+        writeFieldId(fieldName, DATE);
+        writeTimestampField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeTimestamp(@Nullable Timestamp val) throws 
PortableException {
+        doWriteTimestamp(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeObject(String fieldName, @Nullable Object obj) 
throws PortableException {
+        writeFieldId(fieldName, OBJ);
+        writeObjectField(obj);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeObject(@Nullable Object obj) throws 
PortableException {
+        doWriteObject(obj, false);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeObjectDetached(@Nullable Object obj) throws 
PortableException {
+        doWriteObject(obj, true);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeByteArray(String fieldName, @Nullable byte[] 
val) throws PortableException {
+        writeFieldId(fieldName, BYTE_ARR);
+        writeByteArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeByteArray(@Nullable byte[] val) throws 
PortableException {
+        doWriteByteArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeShortArray(String fieldName, @Nullable short[] 
val) throws PortableException {
+        writeFieldId(fieldName, SHORT_ARR);
+        writeShortArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeShortArray(@Nullable short[] val) throws 
PortableException {
+        doWriteShortArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeIntArray(String fieldName, @Nullable int[] val) 
throws PortableException {
+        writeFieldId(fieldName, INT_ARR);
+        writeIntArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeIntArray(@Nullable int[] val) throws 
PortableException {
+        doWriteIntArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeLongArray(String fieldName, @Nullable long[] 
val) throws PortableException {
+        writeFieldId(fieldName, LONG_ARR);
+        writeLongArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeLongArray(@Nullable long[] val) throws 
PortableException {
+        doWriteLongArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeFloatArray(String fieldName, @Nullable float[] 
val) throws PortableException {
+        writeFieldId(fieldName, FLOAT_ARR);
+        writeFloatArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeFloatArray(@Nullable float[] val) throws 
PortableException {
+        doWriteFloatArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDoubleArray(String fieldName, @Nullable 
double[] val)
+        throws PortableException {
+        writeFieldId(fieldName, DOUBLE_ARR);
+        writeDoubleArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDoubleArray(@Nullable double[] val) throws 
PortableException {
+        doWriteDoubleArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeCharArray(String fieldName, @Nullable char[] 
val) throws PortableException {
+        writeFieldId(fieldName, CHAR_ARR);
+        writeCharArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeCharArray(@Nullable char[] val) throws 
PortableException {
+        doWriteCharArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeBooleanArray(String fieldName, @Nullable 
boolean[] val)
+        throws PortableException {
+        writeFieldId(fieldName, BOOLEAN_ARR);
+        writeBooleanArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeBooleanArray(@Nullable boolean[] val) throws 
PortableException {
+        doWriteBooleanArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDecimalArray(String fieldName, @Nullable 
BigDecimal[] val)
+        throws PortableException {
+        writeFieldId(fieldName, DECIMAL_ARR);
+        writeDecimalArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDecimalArray(@Nullable BigDecimal[] val) throws 
PortableException {
+        doWriteDecimalArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeStringArray(String fieldName, @Nullable 
String[] val)
+        throws PortableException {
+        writeFieldId(fieldName, STRING_ARR);
+        writeStringArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeStringArray(@Nullable String[] val) throws 
PortableException {
+        doWriteStringArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeUuidArray(String fieldName, @Nullable UUID[] 
val) throws PortableException {
+        writeFieldId(fieldName, UUID_ARR);
+        writeUuidArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeUuidArray(@Nullable UUID[] val) throws 
PortableException {
+        doWriteUuidArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDateArray(String fieldName, @Nullable Date[] 
val) throws PortableException {
+        writeFieldId(fieldName, DATE_ARR);
+        writeDateArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeDateArray(@Nullable Date[] val) throws 
PortableException {
+        doWriteDateArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeObjectArray(String fieldName, @Nullable 
Object[] val) throws PortableException {
+        writeFieldId(fieldName, OBJ_ARR);
+        writeObjectArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeObjectArray(@Nullable Object[] val) throws 
PortableException {
+        doWriteObjectArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T> void writeCollection(String fieldName, @Nullable 
Collection<T> col)
+        throws PortableException {
+        writeFieldId(fieldName, COL);
+        writeCollectionField(col);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T> void writeCollection(@Nullable Collection<T> col) 
throws PortableException {
+        doWriteCollection(col);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <K, V> void writeMap(String fieldName, @Nullable Map<K, 
V> map)
+        throws PortableException {
+        writeFieldId(fieldName, MAP);
+        writeMapField(map);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <K, V> void writeMap(@Nullable Map<K, V> map) throws 
PortableException {
+        doWriteMap(map);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T extends Enum<?>> void writeEnum(String fieldName, T 
val) throws PortableException {
+        writeFieldId(fieldName, ENUM);
+        writeEnumField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T extends Enum<?>> void writeEnum(T val) throws 
PortableException {
+        doWriteEnum(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T extends Enum<?>> void writeEnumArray(String fieldName, 
T[] val) throws PortableException {
+        writeFieldId(fieldName, ENUM_ARR);
+        writeEnumArrayField(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public <T extends Enum<?>> void writeEnumArray(T[] val) throws 
PortableException {
+        doWriteEnumArray(val);
+    }
+
+    /** {@inheritDoc} */
+    @Override public PortableRawWriter rawWriter() {
+        if (allowFields) {
+            wCtx.out.writeInt(start + RAW_DATA_OFF_POS, wCtx.out.position() - 
start);
+
+            allowFields = false;
+        }
+
+        return this;
+    }
+
+    /** {@inheritDoc} */
+    @Override public GridPortableOutputStream out() {
+        return wCtx.out;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeBytes(String s) throws IOException {
+        int len = s.length();
+
+        writeInt(len);
+
+        for (int i = 0; i < len; i++)
+            writeByte(s.charAt(i));
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeChars(String s) throws IOException {
+        int len = s.length();
+
+        writeInt(len);
+
+        for (int i = 0; i < len; i++)
+            writeChar(s.charAt(i));
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeUTF(String s) throws IOException {
+        writeString(s);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeByte(int v) throws IOException {
+        doWriteByte((byte)v);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeShort(int v) throws IOException {
+        doWriteShort((short)v);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void writeChar(int v) throws IOException {
+        doWriteChar((char)v);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void write(int b) throws IOException {
+        doWriteByte((byte)b);
+    }
+
+    /** {@inheritDoc} */
+    @Override public void flush() throws IOException {
+        // No-op.
+    }
+
+    /**
+     * Reserve a room for an integer.
+     *
+     * @return Position in the stream where value is to be written.
+     */
+    public int reserveInt() {
+        return reserve(LEN_INT);
+    }
+
+    /**
+     * Write int value at the specific position.
+     *
+     * @param pos Position.
+     * @param val Value.
+     * @throws PortableException If failed.
+     */
+    public void writeInt(int pos, int val) throws PortableException {
+        wCtx.out.writeInt(pos, val);
+    }
+
+    /**
+     * @param fieldName Field name.
+     * @throws PortableException If fields are not allowed.
+     */
+    private void writeFieldId(String fieldName, byte fieldType) throws 
PortableException {
+        A.notNull(fieldName, "fieldName");
+
+        if (!allowFields)
+            throw new PortableException("Individual field can't be written 
after raw writer is acquired " +
+                "via rawWriter() method. Consider fixing serialization logic 
for class: " + cls.getName());
+
+        int id = ctx.fieldId(typeId, fieldName);
+
+        if (metaEnabled)
+            metaHashSum = 31 * metaHashSum + (id + fieldType);
+
+        doWriteInt(id);
+    }
+
+    /**
+     * Create new writer with same context.
+     * @param typeId type
+     * @return New writer.
+     */
+    GridPortableWriterExImpl newWriter(int typeId) {
+        GridPortableWriterExImpl res = new GridPortableWriterExImpl(ctx, wCtx);
+
+        res.typeId = typeId;
+
+        return res;
+    }
+
+    /**
+     * @return Portable context.
+     */
+    GridPortableContext context() {
+        return ctx;
+    }
+
+    /** */
+    private static class WriterContext {
+        /** */
+        private Map<Object, Integer> handles = new IdentityHashMap<>();
+
+        /** Output stream. */
+        private GridPortableOutputStream out;
+
+        /**
+         * Constructor.
+         *
+         * @param out Output stream.
+         * @param handles Handles.
+         */
+        private WriterContext(GridPortableOutputStream out, Map<Object, 
Integer> handles) {
+            this.out = out;
+            this.handles = handles == null ? new IdentityHashMap<Object, 
Integer>() : handles;
+        }
+
+        /**
+         * @param obj Object.
+         * @return Handle.
+         */
+        private int handle(Object obj) {
+            assert obj != null;
+
+            Integer h = handles.get(obj);
+
+            if (h != null)
+                return out.position() - h;
+            else {
+                handles.put(obj, out.position());
+
+                return -1;
+            }
+        }
+
+        /**
+         *
+         */
+        private void resetHandles() {
+            handles = new IdentityHashMap<>();
+        }
+    }
+}

Reply via email to