Skip to content
Open
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
17 changes: 15 additions & 2 deletions jme3-core/src/main/java/com/jme3/util/LittleEndien.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ public int read(byte[] buf) throws IOException {
return in.read(buf);
}

/**
* Attempts(!) to read up to len bytes into buf at offset off.
*/
@Override
public int read(byte[] buf, int off, int len) throws IOException {
return in.read(buf, off, len);
Expand Down Expand Up @@ -142,14 +145,24 @@ public double readDouble() throws IOException {

@Override
public void readFully(byte b[]) throws IOException {
in.read(b, 0, b.length);
readFully(b, 0, b.length);
}

@Override
public void readFully(byte b[], int off, int len) throws IOException {
in.read(b, off, len);
int totalRead = 0;
while (totalRead < len) {
int bytesRead = in.read(b, off + totalRead, len - totalRead);
if (bytesRead < 0) {
throw new EOFException("Reached end of stream before reading fully.");
}
totalRead += bytesRead;
}
}

/**
* Attempts(!) to skip n bytes in the input stream.
*/
@Override
public int skipBytes(int n) throws IOException {
return (int) in.skip(n);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.jme3.audio.AudioKey;
import com.jme3.audio.AudioStream;
import com.jme3.audio.SeekableStream;
import com.jme3.export.binary.ByteUtils;
import com.jme3.util.BufferUtils;
import com.jme3.util.LittleEndien;
import java.io.BufferedInputStream;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void setTime(float time) {
}
InputStream newStream = info.openStream();
try {
newStream.skip(resetOffset);
ByteUtils.skipFully(newStream, resetOffset);
this.in = new BufferedInputStream(newStream);
} catch (IOException ex) {
// Resource could have gotten lost, etc.
Expand Down Expand Up @@ -172,7 +173,7 @@ private void readFormatChunk(int chunkSize, AudioData audioData) throws IOExcept
// Skip any extra parameters in the format chunk (e.g., for non-PCM formats)
int remainingChunkBytes = chunkSize - 16;
if (remainingChunkBytes > 0) {
in.skipBytes(remainingChunkBytes);
ByteUtils.skipFully((InputStream)in, remainingChunkBytes);
}
}

Expand Down
89 changes: 80 additions & 9 deletions jme3-core/src/plugins/java/com/jme3/export/binary/ByteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
package com.jme3.export.binary;

import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
Expand All @@ -50,6 +52,77 @@ public class ByteUtils {
private ByteUtils() {
}


public static void readFully(InputStream in, byte[] b) throws IOException {
int bytesRead = 0;
int read = 0;
while (bytesRead < b.length && (read = in.read(b, bytesRead, b.length - bytesRead)) != -1) {
bytesRead += read;
}
if (bytesRead < b.length) {
throw new IOException(
"End of stream reached prematurely after " + bytesRead + " of " + b.length + " bytes.");
}
}


public static void skipFully(InputStream in, long n) throws IOException {
skipFully(in, n, true);
}

public static void skipFully(InputStream in, long n, boolean throwOnEOF) throws IOException {
while (n > 0) {
long skipped = in.skip(n);
if (skipped > 0 && skipped <= n) { // skipped some bytes
n -= skipped;
} else if (skipped == 0) { // skipped nothing
// distinguish between EOF and no bytes available
if (in.read() == -1) {
if (throwOnEOF) {
throw new EOFException();
} else {
return;
}
} else {
// stream was just hangling
n--;
}
} else {
throw new IOException(
"Unable to skip exactly " + n + " bytes. Only " + skipped + " bytes were skipped.");
}
}
}

public static void skipFully(DataInput in, int n) throws IOException {
skipFully(in, n, true);
}

public static void skipFully(DataInput in, int n, boolean throwOnEOF) throws IOException {
while (n > 0) {
long skipped = in.skipBytes(n);
if (skipped > 0 && skipped <= n) { // skipped some bytes
n -= skipped;
} else if (skipped == 0) { // skipped nothing
// distinguish between EOF and no bytes available
try {
in.readByte();
} catch (EOFException e) {
if (throwOnEOF) {
throw e;
} else {
return;
}
}
n--;
} else {
throw new IOException(
"Unable to skip exactly " + n + " bytes. Only " + skipped + " bytes were skipped.");
}
}
}


/**
* Takes an InputStream and returns the complete byte content of it
*
Expand Down Expand Up @@ -125,7 +198,7 @@ public static short readShort(InputStream inputStream) throws IOException {
byte[] byteArray = new byte[2];

// Read in the next 2 bytes
inputStream.read(byteArray);
readFully(inputStream, byteArray);

short number = convertShortFromBytes(byteArray);

Expand Down Expand Up @@ -187,7 +260,7 @@ public static int readInt(InputStream inputStream) throws IOException {
byte[] byteArray = new byte[4];

// Read in the next 4 bytes
inputStream.read(byteArray);
readFully(inputStream, byteArray);

int number = convertIntFromBytes(byteArray);

Expand Down Expand Up @@ -263,7 +336,7 @@ public static long readLong(InputStream inputStream) throws IOException {
byte[] byteArray = new byte[8];

// Read in the next 8 bytes
inputStream.read(byteArray);
readFully(inputStream, byteArray);

long number = convertLongFromBytes(byteArray);

Expand Down Expand Up @@ -326,7 +399,7 @@ public static double readDouble(InputStream inputStream) throws IOException {
byte[] byteArray = new byte[8];

// Read in the next 8 bytes
inputStream.read(byteArray);
readFully(inputStream, byteArray);

double number = convertDoubleFromBytes(byteArray);

Expand Down Expand Up @@ -382,7 +455,7 @@ public static float readFloat(InputStream inputStream) throws IOException {
byte[] byteArray = new byte[4];

// Read in the next 4 bytes
inputStream.read(byteArray);
readFully(inputStream, byteArray);

float number = convertFloatFromBytes(byteArray);

Expand Down Expand Up @@ -440,7 +513,7 @@ public static boolean readBoolean(InputStream inputStream) throws IOException {
byte[] byteArray = new byte[1];

// Read in the next byte
inputStream.read(byteArray);
readFully(inputStream, byteArray);

return convertBooleanFromBytes(byteArray);
}
Expand Down Expand Up @@ -470,9 +543,7 @@ public static boolean convertBooleanFromBytes(byte[] byteArray, int offset) {
* if bytes greater than the length of the store.
*/
public static byte[] readData(byte[] store, int bytes, InputStream is) throws IOException {
for (int i = 0; i < bytes; i++) {
store[i] = (byte)is.read();
}
readFully(is, store);
return store;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.TextureKey;
import com.jme3.export.binary.ByteUtils;
import com.jme3.texture.Image;
import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture;
Expand Down Expand Up @@ -160,7 +161,7 @@ private void loadDX10Header() throws IOException {
}
}

in.skipBytes(4); // skip reserved value
ByteUtils.skipFully(in, 4); // skip reserved value
}

private void setPixelFormat(int dxgiFormat) throws IOException {
Expand Down Expand Up @@ -227,13 +228,13 @@ private void loadHeader() throws IOException {
pitchOrSize = in.readInt();
depth = in.readInt();
mipMapCount = in.readInt();
in.skipBytes(44);
ByteUtils.skipFully(in, 44);
pixelFormat = null;
directx10 = false;
readPixelFormat();
caps1 = in.readInt();
caps2 = in.readInt();
in.skipBytes(12);
ByteUtils.skipFully(in, 12);
texture3D = false;

if (!directx10) {
Expand Down Expand Up @@ -292,7 +293,7 @@ private void readPixelFormat() throws IOException {
compressed = true;
int fourcc = in.readInt();
int swizzle = in.readInt();
in.skipBytes(16);
ByteUtils.skipFully(in, 16);

switch (fourcc) {
case PF_DXT1:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.TextureKey;
import com.jme3.export.binary.ByteUtils;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import com.jme3.texture.Image.Format;
Expand Down Expand Up @@ -180,16 +181,14 @@ private boolean decodeScanlineRLE(InputStream in, int width) throws IOException{
return true;
}

private boolean decodeScanlineUncompressed(InputStream in, int width) throws IOException{
private void decodeScanlineUncompressed(InputStream in, int width) throws IOException{
byte[] rgbe = new byte[4];

for (int i = 0; i < width; i+=3){
if (in.read(rgbe) < 1)
return false;

ByteUtils.readFully(in, rgbe);
writeRGBE(rgbe);
}
return true;

}

private void decodeScanline(InputStream in, int width) throws IOException{
Expand All @@ -200,7 +199,7 @@ private void decodeScanline(InputStream in, int width) throws IOException{

// check format
byte[] data = new byte[4];
in.read(data);
ByteUtils.readFully(in, data);
if (data[0] != 0x02 || data[1] != 0x02 || (data[2] & 0x80) != 0){
// not RLE data
decodeScanlineUncompressed(in, width-1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.TextureKey;
import com.jme3.export.binary.ByteUtils;
import com.jme3.texture.Image;
import com.jme3.texture.Image.Format;
import com.jme3.texture.image.ColorSpace;
Expand Down Expand Up @@ -115,12 +116,7 @@ private Image load(InputStream in, boolean needYFlip) throws IOException{
if (!needYFlip)
imageData.position(scanLineBytes * y);

int read = 0;
int off = 0;
do {
read = in.read(scanline, off, scanline.length - off);
off += read;
} while (read > 0);
ByteUtils.readFully(in, scanline);

if (needEndianFlip){
flipScanline(scanline);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.TextureKey;
import com.jme3.export.binary.ByteUtils;
import com.jme3.math.FastMath;
import com.jme3.texture.Image;
import com.jme3.texture.Image.Format;
Expand Down Expand Up @@ -158,7 +159,7 @@ public static Image load(InputStream in, boolean flip) throws IOException {

// Skip image ID
if (idLength > 0) {
dis.skip(idLength);
ByteUtils.skipFully((InputStream) dis, idLength);
}

ColorMapEntry[] cMapEntries = null;
Expand All @@ -168,7 +169,7 @@ public static Image load(InputStream in, boolean flip) throws IOException {
int bitsPerColor = Math.min(cMapDepth / 3, 8);

byte[] cMapData = new byte[bytesInColorMap];
dis.read(cMapData);
ByteUtils.readFully((InputStream) dis, cMapData);

// Only go to the trouble of constructing the color map
// table if this is declared a color mapped image.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import com.jme3.asset.AssetInfo;
import com.jme3.asset.AssetLoader;
import com.jme3.asset.TextureKey;
import com.jme3.export.binary.ByteUtils;
import com.jme3.renderer.Caps;
import com.jme3.renderer.opengl.GLImageFormat;
import com.jme3.renderer.opengl.GLImageFormats;
Expand Down Expand Up @@ -95,7 +96,7 @@ private Image load(InputStream stream) {

DataInput in = new DataInputStream(stream);
try {
stream.read(fileId, 0, 12);
ByteUtils.readFully(stream, fileId);
if (!checkFileIdentifier(fileId)) {
throw new IllegalArgumentException("Unrecognized ktx file identifier : " + new String(fileId) + " should be " + new String(fileIdentifier));
}
Expand Down Expand Up @@ -193,13 +194,13 @@ private Image load(InputStream stream) {
}
//cube padding
if (numberOfFaces == 6 && numberOfArrayElements == 0) {
in.skipBytes(3 - ((nbPixelRead + 3) % 4));
ByteUtils.skipFully(in, 3 - ((nbPixelRead + 3) % 4));
}
}
}
//mip padding
log.log(Level.FINE, "skipping {0}", (3 - ((imageSize + 3) % 4)));
in.skipBytes(3 - ((imageSize + 3) % 4));
ByteUtils.skipFully(in, 3 - ((imageSize + 3) % 4));
offset+=imageSize;
}
//there are loaded mip maps we set the sizes
Expand Down Expand Up @@ -305,7 +306,7 @@ private PixelReader parseMetaData(int bytesOfKeyValueData, DataInput in) throws
//padding
int padding = 3 - ((keyAndValueByteSize + 3) % 4);
if (padding > 0) {
in.skipBytes(padding);
ByteUtils.skipFully(in, padding);
}
i += 4 + keyAndValueByteSize + padding;
}
Expand Down
Loading
Loading