Search in sources :

Example 46 with GdxRuntimeException

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();
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) InputStreamReader(java.io.InputStreamReader) IOException(java.io.IOException)

Example 47 with GdxRuntimeException

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;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException)

Example 48 with GdxRuntimeException

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) {
        }
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 49 with GdxRuntimeException

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;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Example 50 with GdxRuntimeException

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;
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException)

Aggregations

GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)202 IOException (java.io.IOException)40 FileHandle (com.badlogic.gdx.files.FileHandle)14 Array (com.badlogic.gdx.utils.Array)13 Texture (com.badlogic.gdx.graphics.Texture)12 TextureRegion (com.badlogic.gdx.graphics.g2d.TextureRegion)11 AtlasRegion (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion)9 InputStream (java.io.InputStream)9 Pixmap (com.badlogic.gdx.graphics.Pixmap)8 Sprite (com.badlogic.gdx.graphics.g2d.Sprite)8 TextureAtlas (com.badlogic.gdx.graphics.g2d.TextureAtlas)7 AtlasSprite (com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasSprite)7 BufferedInputStream (java.io.BufferedInputStream)7 File (java.io.File)7 OutputStream (java.io.OutputStream)7 VertexAttribute (com.badlogic.gdx.graphics.VertexAttribute)6 LifecycleListener (com.badlogic.gdx.LifecycleListener)5 ByteBuffer (java.nio.ByteBuffer)5 BitmapFont (com.badlogic.gdx.graphics.g2d.BitmapFont)4 NinePatch (com.badlogic.gdx.graphics.g2d.NinePatch)4