use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.
the class FileWrapper method readString.
/** Reads the entire file into a string using the specified charset.
* @throw GdxRuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */
public String readString(String charset) {
int fileLength = (int) length();
if (fileLength == 0)
fileLength = 512;
StringBuilder output = new StringBuilder(fileLength);
InputStreamReader reader = null;
try {
if (charset == null)
reader = new InputStreamReader(read());
else
reader = new InputStreamReader(read(), charset);
char[] buffer = new char[256];
while (true) {
int length = reader.read(buffer);
if (length == -1)
break;
output.append(buffer, 0, length);
}
} catch (IOException ex) {
throw new GdxRuntimeException("Error reading layout file: " + this, ex);
} finally {
StreamUtils.closeQuietly(reader);
}
return output.toString();
}
use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.
the class FileWrapper method list.
/** Returns the paths to the children of this directory. Returns an empty list if this file handle represents a file and not a
* directory. On the desktop, an {@link FileType#Internal} handle to a directory on the classpath will return a zero length
* array.
* @throw GdxRuntimeException if this file is an {@link FileType#Classpath} file. */
public FileWrapper[] list() {
if (type == FileType.Classpath)
throw new GdxRuntimeException("Cannot list a classpath directory: " + file);
String[] relativePaths = file().list();
if (relativePaths == null)
return new FileWrapper[0];
FileWrapper[] handles = new FileWrapper[relativePaths.length];
for (int i = 0, n = relativePaths.length; i < n; i++) handles[i] = child(relativePaths[i]);
return handles;
}
use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.
the class FileWrapper method write.
/** Reads the remaining bytes from the specified stream and writes them to this file. The stream is closed. Parent directories
* will be created if necessary.
* @param append If false, this file will be overwritten if it exists, otherwise it will be appended.
* @throw GdxRuntimeException if this file handle represents a directory, if it is a {@link FileType#Classpath} or
* {@link FileType#Internal} file, or if it could not be written. */
public void write(InputStream input, boolean append) {
OutputStream output = null;
try {
output = write(append);
byte[] buffer = new byte[4096];
while (true) {
int length = input.read(buffer);
if (length == -1)
break;
output.write(buffer, 0, length);
}
} catch (Exception ex) {
throw new GdxRuntimeException("Error stream writing to file: " + file + " (" + type + ")", ex);
} finally {
try {
if (input != null)
input.close();
} catch (Exception ignored) {
}
try {
if (output != null)
output.close();
} catch (Exception ignored) {
}
}
}
use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.
the class FileWrapper method readBytes.
/** Reads the entire file into a byte array.
* @throw GdxRuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */
public byte[] readBytes() {
int length = (int) length();
if (length == 0)
length = 512;
byte[] buffer = new byte[length];
int position = 0;
InputStream input = read();
try {
while (true) {
int count = input.read(buffer, position, buffer.length - position);
if (count == -1)
break;
position += count;
if (position == buffer.length) {
// Grow buffer.
byte[] newBuffer = new byte[buffer.length * 2];
System.arraycopy(buffer, 0, newBuffer, 0, position);
buffer = newBuffer;
}
}
} catch (IOException ex) {
throw new GdxRuntimeException("Error reading file: " + this, ex);
} finally {
try {
if (input != null)
input.close();
} catch (IOException ignored) {
}
}
if (position < buffer.length) {
// Shrink buffer.
byte[] newBuffer = new byte[position];
System.arraycopy(buffer, 0, newBuffer, 0, position);
buffer = newBuffer;
}
return buffer;
}
use of com.badlogic.gdx.utils.GdxRuntimeException in project libgdx by libgdx.
the class FileWrapper method readBytes.
/** Reads the entire file into the byte array. The byte array must be big enough to hold the file's data.
* @param bytes the array to load the file into
* @param offset the offset to start writing bytes
* @param size the number of bytes to read, see {@link #length()}
* @return the number of read bytes */
public int readBytes(byte[] bytes, int offset, int size) {
InputStream input = read();
int position = 0;
try {
while (true) {
int count = input.read(bytes, offset + position, size - position);
if (count <= 0)
break;
position += count;
}
} catch (IOException ex) {
throw new GdxRuntimeException("Error reading file: " + this, ex);
} finally {
try {
if (input != null)
input.close();
} catch (IOException ignored) {
}
}
return position - offset;
}
Aggregations