This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 271474 - SaveAction raise negative index exception in CloneableEditorSupport
Summary: SaveAction raise negative index exception in CloneableEditorSupport
Status: NEW
Alias: None
Product: editor
Classification: Unclassified
Component: Actions/Menu/Toolbar (show other bugs)
Version: 8.1
Hardware: PC Linux
: P2 normal (vote)
Assignee: Miloslav Metelka
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-09-14 07:37 UTC by GuillaumeR
Modified: 2017-09-14 07:37 UTC (History)
0 users

See Also:
Issue Type: DEFECT
Exception Reporter:


Attachments
the IllegalArgumentException (5.32 KB, text/plain)
2017-09-14 07:37 UTC, GuillaumeR
Details

Note You need to log in before you can comment on or make changes to this bug.
Description GuillaumeR 2017-09-14 07:37:14 UTC
Created attachment 165127 [details]
the IllegalArgumentException

Hi,

When saving a large file (in my case 195.2MB and 204.666.376 characters), an IllegalArgumentException is raise at ByteArrayOutputStream (l.74) (see attachment).

The bug is due to the stream capacity size initialization with an negative integer at CloneableEditorSupport.saveDocumentImpl(l.712) :

   int byteArrayAllocSize = myDoc.getLength() * 11 / 10;
   memoryOutputStream[0] = new MemoryOutputStream(byteArrayAllocSize);

If we use my case :

byteArrayAllocSize = 204.666.376 * 11 / 10
                   = 2.251.330.136 /10       <----- error here because 2.251.330.136 > Integer.MAX_VALUE(2.147.483.647)
                   = 225.133.007             <----- expected result
                   = -204.363.714            <----- obtained result

It can be workarounded by copying the SaveDocumentImpl content in a costum EditorSupport saveDocument method and accessing COS fields and methods with java.reflect.
This solution can only be a temporary method !!!

Here is a fix to the problem :

   int byteArrayAllocSize = myDoc.getLength() / 10 * 11;
   byteArrayAllocSize = byteArrayAllocSize < 0 ? Integer.MAX_VALUE : byteArrayAllocSize;
   memoryOutputStream[0] = new MemoryOutputStream(byteArrayAllocSize);

By the way the MemoryOutputStream class is completely useless as it does nothing more then ByteArrayOutputStream. I didn't figured why it was used...

Thank you, regards,

Guillaume