Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use relatively safe max size of java array #516

Merged
merged 1 commit into from
Jul 13, 2017
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
2 changes: 1 addition & 1 deletion src/com/esotericsoftware/kryo/io/ByteBufferInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ public void skip (int count) throws KryoException {
public long skip (long count) throws KryoException {
long remaining = count;
while (remaining > 0) {
int skip = (int)Math.min(Integer.MAX_VALUE, remaining);
int skip = (int)Math.min(Util.MAX_SAFE_ARRAY_SIZE, remaining);
skip(skip);
remaining -= skip;
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/esotericsoftware/kryo/io/ByteBufferOutput.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ByteBufferOutput (int bufferSize) {
public ByteBufferOutput (int bufferSize, int maxBufferSize) {
if (maxBufferSize < -1) throw new IllegalArgumentException("maxBufferSize cannot be < -1: " + maxBufferSize);
this.capacity = bufferSize;
this.maxCapacity = maxBufferSize == -1 ? Integer.MAX_VALUE : maxBufferSize;
this.maxCapacity = maxBufferSize == -1 ? Util.MAX_SAFE_ARRAY_SIZE : maxBufferSize;
niobuffer = ByteBuffer.allocateDirect(bufferSize);
niobuffer.order(byteOrder);
}
Expand Down Expand Up @@ -163,7 +163,7 @@ public void setBuffer (ByteBuffer buffer, int maxBufferSize) {
if (buffer == null) throw new IllegalArgumentException("buffer cannot be null.");
if (maxBufferSize < -1) throw new IllegalArgumentException("maxBufferSize cannot be < -1: " + maxBufferSize);
this.niobuffer = buffer;
this.maxCapacity = maxBufferSize == -1 ? Integer.MAX_VALUE : maxBufferSize;
this.maxCapacity = maxBufferSize == -1 ? Util.MAX_SAFE_ARRAY_SIZE : maxBufferSize;
byteOrder = buffer.order();
capacity = buffer.capacity();
position = buffer.position();
Expand Down
2 changes: 1 addition & 1 deletion src/com/esotericsoftware/kryo/io/Input.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ public int read (byte[] bytes, int offset, int count) throws KryoException {
public long skip (long count) throws KryoException {
long remaining = count;
while (remaining > 0) {
int skip = (int)Math.min(Integer.MAX_VALUE, remaining);
int skip = (int)Math.min(Util.MAX_SAFE_ARRAY_SIZE, remaining);
skip(skip);
remaining -= skip;
}
Expand Down
4 changes: 2 additions & 2 deletions src/com/esotericsoftware/kryo/io/Output.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public Output (int bufferSize, int maxBufferSize) {
"bufferSize: " + bufferSize + " cannot be greater than maxBufferSize: " + maxBufferSize);
if (maxBufferSize < -1) throw new IllegalArgumentException("maxBufferSize cannot be < -1: " + maxBufferSize);
this.capacity = bufferSize;
this.maxCapacity = maxBufferSize == -1 ? Integer.MAX_VALUE : maxBufferSize;
this.maxCapacity = maxBufferSize == -1 ? Util.MAX_SAFE_ARRAY_SIZE : maxBufferSize;
buffer = new byte[bufferSize];
}

Expand Down Expand Up @@ -116,7 +116,7 @@ public void setBuffer (byte[] buffer, int maxBufferSize) {
"buffer has length: " + buffer.length + " cannot be greater than maxBufferSize: " + maxBufferSize);
if (maxBufferSize < -1) throw new IllegalArgumentException("maxBufferSize cannot be < -1: " + maxBufferSize);
this.buffer = buffer;
this.maxCapacity = maxBufferSize == -1 ? Integer.MAX_VALUE : maxBufferSize;
this.maxCapacity = maxBufferSize == -1 ? Util.MAX_SAFE_ARRAY_SIZE : maxBufferSize;
capacity = buffer.length;
position = 0;
total = 0;
Expand Down
29 changes: 29 additions & 0 deletions src/com/esotericsoftware/kryo/io/Util.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* Copyright (c) 2017, Nathan Sweet
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following
* conditions are met:
*
* - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
* - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.
* - Neither the name of Esoteric Software nor the names of its contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
* SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

package com.esotericsoftware.kryo.io;

/** IO package utilities.
* @author Rafal Wojdyla <ravwojdyla@gmail.com> */
class Util {

//See: https://stackoverflow.com/questions/3038392/do-java-arrays-have-a-maximum-size
static final int MAX_SAFE_ARRAY_SIZE = Integer.MAX_VALUE - 8;

}