[
https://issues.apache.org/jira/browse/IO-537?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]
Borys Zibrov updated IO-537:
----------------------------
Description:
BOMInputStream constructor code sorts array of BOMs to distinguish between
UTF-32LE and UTF-16LE:
{code}
public BOMInputStream(InputStream delegate, boolean include,
ByteOrderMark... boms) {
super(delegate);
if (boms == null || boms.length == 0) {
throw new IllegalArgumentException("No BOMs specified");
}
this.include = include;
// Sort the BOMs to match the longest BOM first because some BOMs have
the same starting two bytes.
Arrays.sort(boms, ByteOrderMarkLengthComparator);
this.boms = Arrays.asList(boms);
}
{code}
The problem is the array is sorted in-place so that's 1) not expected by the
caller 2) makes code not safe, if array is shared between threads and all
create BOMInputStreams with single array of BOMs results are unpredictable.
Instead a copy of the input array should be made and then sorted.
was:
BOMInputStream constructor code sorts array of BOMs to distinguish between
UTF-32LE and UTF-16LE:
{code}
public BOMInputStream(InputStream delegate, boolean include,
ByteOrderMark... boms) {
super(delegate);
if (boms == null || boms.length == 0) {
throw new IllegalArgumentException("No BOMs specified");
}
this.include = include;
// Sort the BOMs to match the longest BOM first because some BOMs have
the same starting two bytes.
Arrays.sort(boms, ByteOrderMarkLengthComparator);
this.boms = Arrays.asList(boms);
}
{code}
The problem is the array is sorted in-place so that's 1) not expected by the
caller 2) makes code not safe, if array is shared between threads and all
create BOMInputStreams with single array of BOMs results are unpredictable.
Instead the copy of the input array should be made and then sorted.
> BOMInputStream shouldn't sort array of BOMs in-place
> ----------------------------------------------------
>
> Key: IO-537
> URL: https://issues.apache.org/jira/browse/IO-537
> Project: Commons IO
> Issue Type: Improvement
> Components: Streams/Writers
> Affects Versions: 2.4, 2.5
> Environment: OS: Ubuntu 16.04
> java version "1.7.0_80"
> Java(TM) SE Runtime Environment (build 1.7.0_80-b15)
> Java HotSpot(TM) 64-Bit Server VM (build 24.80-b11, mixed mode)
> Reporter: Borys Zibrov
> Labels: BOMInputStream
>
> BOMInputStream constructor code sorts array of BOMs to distinguish between
> UTF-32LE and UTF-16LE:
> {code}
> public BOMInputStream(InputStream delegate, boolean include,
> ByteOrderMark... boms) {
> super(delegate);
> if (boms == null || boms.length == 0) {
> throw new IllegalArgumentException("No BOMs specified");
> }
> this.include = include;
> // Sort the BOMs to match the longest BOM first because some BOMs
> have the same starting two bytes.
> Arrays.sort(boms, ByteOrderMarkLengthComparator);
> this.boms = Arrays.asList(boms);
> }
> {code}
> The problem is the array is sorted in-place so that's 1) not expected by the
> caller 2) makes code not safe, if array is shared between threads and all
> create BOMInputStreams with single array of BOMs results are unpredictable.
> Instead a copy of the input array should be made and then sorted.
--
This message was sent by Atlassian JIRA
(v6.3.15#6346)