nizhikov commented on code in PR #11518:
URL: https://github.com/apache/ignite/pull/11518#discussion_r1810874207


##########
modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java:
##########
@@ -1880,6 +1892,72 @@ public void writeFieldIdNoSchemaUpdate(int fieldId) {
         fieldCnt++;
     }
 
+    /**
+     * Write byte array from the InputStream with the specified length.
+     *
+     * @param in InputStream.
+     * @param len Length of data in the stream.
+     * @return Number of bytes written.
+     */
+    public int writeByteArrayFromInputStream(InputStream in, int len) throws 
IOException {
+        out.unsafeEnsure(1 + 4 + len);
+
+        return doWriteByteArrayFromInputStream(in, len);
+    }
+
+    /**
+     * Write byte array from the InputStream with uknown length.
+     *
+     * @param in InputStream.
+     * @return Number of bytes written or -1 if stream contains more than 
{@code MAX_ARRAY_SIZE} bytes.
+     */
+    public int writeByteArrayFromInputStream(InputStream in) throws 
IOException {
+        out.unsafeEnsure(1 + 4);
+
+        int writtenLen = doWriteByteArrayFromInputStream(in, MAX_ARRAY_SIZE);
+
+        if (writtenLen == MAX_ARRAY_SIZE && in.read() != -1)

Review Comment:
   Looks like with this version we can still have one method and fail if read 
more then `MAX_ARRAY_SIZE` from InputStream. Logic relies on `-1` as unlimited 
read flag.
   
   ```
       /**
        * Write byte array from the InputStream with uknown length.
        *
        * @param in InputStream.
        * @return Number of bytes written or -1 if stream contains more than 
{@code MAX_ARRAY_SIZE} bytes.
        */
       public int writeByteArrayFromInputStream(InputStream in) throws 
IOException {
           out.unsafeEnsure(1 + 4);
   
           return doWriteByteArrayFromInputStream(in, -1);
       }
   
       /**
        * Write byte array from the InputStream. No more than {@code limit} 
bytes will be written.
        *
        * @param in InputStream.
        * @param limit Max length of data to be read from the stream.
        * @return Number of bytes written.
        * @throws IOException If an I/O error occurs.
        */
       private int doWriteByteArrayFromInputStream(InputStream in, int limit) 
throws IOException {
           out.unsafeWriteByte(GridBinaryMarshaller.BYTE_ARR);
   
           int lengthPos = out.position();
   
           out.position(lengthPos + 4);
   
           int written = 0;
   
          byte[] buf = new byte[limit > 0 ? Math.min(limit, 
DEFAULT_BUFFER_SIZE) : DEFAULT_BUFFER_SIZE];
   
           while (true) {
               int read = limit > 0
                   ? in.read(buf, 0, Math.min(buf.length, limit - written))
                   : in.read(buf, 0, buf.length);
   
               if (read == -1)
                   break;
   
               if (read + written > MAX_ARRAY_SIZE)
                   throw new IgniteException("Too much data. Can't write more 
then " + MAX_ARRAY_SIZE + " bytes from stream");
   
               out.writeByteArray(buf, 0, read);
   
               written += read;
   
               if (written >= limit)
                   break;
           }
   
           out.position(lengthPos);
           out.unsafeWriteInt(written);
           out.position(out.position() + written);
   
           return written;
       }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to