use of java.nio.BufferUnderflowException in project jmonkeyengine by jMonkeyEngine.
the class GeoMap method writeTexCoordArray.
public FloatBuffer writeTexCoordArray(FloatBuffer store, Vector2f offset, Vector2f scale) {
if (store != null) {
if (store.remaining() < getWidth() * getHeight() * 2)
throw new BufferUnderflowException();
} else {
store = BufferUtils.createFloatBuffer(getWidth() * getHeight() * 2);
}
if (offset == null)
offset = new Vector2f();
Vector2f tcStore = new Vector2f();
for (int y = 0; y < getHeight(); y++) {
for (int x = 0; x < getWidth(); x++) {
getUV(x, y, tcStore);
store.put(offset.x + tcStore.x * scale.x);
store.put(offset.y + tcStore.y * scale.y);
}
}
return store;
}
use of java.nio.BufferUnderflowException in project jmonkeyengine by jMonkeyEngine.
the class GeoMap method writeVertexArray.
/**
* Creates a vertex array from the height data in this Geomap
*
* The scale argument specifies the scale to use for the vertex buffer.
* For example, if scale is 10,1,10, then the greatest X value is getWidth()*10
*
* @param store A preallocated FloatBuffer where to store the data (optional),
* size must be >= getWidth()*getHeight()*3
* @param scale Created vertexes are scaled by this vector
*
* @return store, or a new FloatBuffer if store is null
*
* @throws NullPointerException If isLoaded() is false
*/
public FloatBuffer writeVertexArray(FloatBuffer store, Vector3f scale, boolean center) {
if (store != null) {
if (store.remaining() < width * height * 3)
throw new BufferUnderflowException();
} else {
store = BufferUtils.createFloatBuffer(width * height * 3);
}
assert hdata.length == height * width;
Vector3f offset = new Vector3f(-getWidth() * scale.x * 0.5f, 0, -getWidth() * scale.z * 0.5f);
if (!center)
offset.zero();
int i = 0;
for (int z = 0; z < height; z++) {
for (int x = 0; x < width; x++) {
store.put((float) x * scale.x + offset.x);
store.put((float) hdata[i++] * scale.y);
store.put((float) z * scale.z + offset.z);
}
}
return store;
}
use of java.nio.BufferUnderflowException in project hs4j by killme2008.
the class AbstractIoBuffer method getString.
/**
* {@inheritDoc}
*/
@Override
public String getString(int fieldSize, CharsetDecoder decoder) throws CharacterCodingException {
checkFieldSize(fieldSize);
if (fieldSize == 0) {
return "";
}
if (!hasRemaining()) {
return "";
}
boolean utf16 = decoder.charset().name().startsWith("UTF-16");
if (utf16 && (fieldSize & 1) != 0) {
throw new IllegalArgumentException("fieldSize is not even.");
}
int oldPos = position();
int oldLimit = limit();
int end = oldPos + fieldSize;
if (oldLimit < end) {
throw new BufferUnderflowException();
}
int i;
if (!utf16) {
for (i = oldPos; i < end; i++) {
if (get(i) == 0) {
break;
}
}
if (i == end) {
limit(end);
} else {
limit(i);
}
} else {
for (i = oldPos; i < end; i += 2) {
if (get(i) == 0 && get(i + 1) == 0) {
break;
}
}
if (i == end) {
limit(end);
} else {
limit(i);
}
}
if (!hasRemaining()) {
limit(oldLimit);
position(end);
return "";
}
decoder.reset();
int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1;
CharBuffer out = CharBuffer.allocate(expectedLength);
for (; ; ) {
CoderResult cr;
if (hasRemaining()) {
cr = decoder.decode(buf(), out, true);
} else {
cr = decoder.flush(out);
}
if (cr.isUnderflow()) {
break;
}
if (cr.isOverflow()) {
CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength);
out.flip();
o.put(out);
out = o;
continue;
}
if (cr.isError()) {
// Revert the buffer back to the previous state.
limit(oldLimit);
position(oldPos);
cr.throwException();
}
}
limit(oldLimit);
position(end);
return out.flip().toString();
}
use of java.nio.BufferUnderflowException in project hs4j by killme2008.
the class AbstractIoBuffer method getObject.
/**
* {@inheritDoc}
*/
@Override
public Object getObject(final ClassLoader classLoader) throws ClassNotFoundException {
if (!prefixedDataAvailable(4)) {
throw new BufferUnderflowException();
}
int length = getInt();
if (length <= 4) {
throw new BufferDataException("Object length should be greater than 4: " + length);
}
int oldLimit = limit();
limit(position() + length);
try {
ObjectInputStream in = new ObjectInputStream(asInputStream()) {
@Override
protected ObjectStreamClass readClassDescriptor() throws IOException, ClassNotFoundException {
int type = read();
if (type < 0) {
throw new EOFException();
}
switch(type) {
case // Primitive types
0:
return super.readClassDescriptor();
case // Non-primitive types
1:
String className = readUTF();
Class<?> clazz = Class.forName(className, true, classLoader);
return ObjectStreamClass.lookup(clazz);
default:
throw new StreamCorruptedException("Unexpected class descriptor type: " + type);
}
}
@Override
protected Class<?> resolveClass(ObjectStreamClass desc) throws IOException, ClassNotFoundException {
String name = desc.getName();
try {
return Class.forName(name, false, classLoader);
} catch (ClassNotFoundException ex) {
return super.resolveClass(desc);
}
}
};
return in.readObject();
} catch (IOException e) {
throw new BufferDataException(e);
} finally {
limit(oldLimit);
}
}
use of java.nio.BufferUnderflowException in project hs4j by killme2008.
the class AbstractIoBuffer method getPrefixedString.
/**
* Reads a string which has a length field before the actual encoded string,
* using the specified <code>decoder</code> and returns it.
*
* @param prefixLength
* the length of the length field (1, 2, or 4)
* @param decoder
* the decoder to use for decoding the string
* @return the prefixed string
* @throws CharacterCodingException
* when decoding fails
* @throws BufferUnderflowException
* when there is not enough data available
*/
@Override
public String getPrefixedString(int prefixLength, CharsetDecoder decoder) throws CharacterCodingException {
if (!prefixedDataAvailable(prefixLength)) {
throw new BufferUnderflowException();
}
int fieldSize = 0;
switch(prefixLength) {
case 1:
fieldSize = getUnsigned();
break;
case 2:
fieldSize = getUnsignedShort();
break;
case 4:
fieldSize = getInt();
break;
}
if (fieldSize == 0) {
return "";
}
boolean utf16 = decoder.charset().name().startsWith("UTF-16");
if (utf16 && (fieldSize & 1) != 0) {
throw new BufferDataException("fieldSize is not even for a UTF-16 string.");
}
int oldLimit = limit();
int end = position() + fieldSize;
if (oldLimit < end) {
throw new BufferUnderflowException();
}
limit(end);
decoder.reset();
int expectedLength = (int) (remaining() * decoder.averageCharsPerByte()) + 1;
CharBuffer out = CharBuffer.allocate(expectedLength);
for (; ; ) {
CoderResult cr;
if (hasRemaining()) {
cr = decoder.decode(buf(), out, true);
} else {
cr = decoder.flush(out);
}
if (cr.isUnderflow()) {
break;
}
if (cr.isOverflow()) {
CharBuffer o = CharBuffer.allocate(out.capacity() + expectedLength);
out.flip();
o.put(out);
out = o;
continue;
}
cr.throwException();
}
limit(oldLimit);
position(end);
return out.flip().toString();
}
Aggregations