Example usage for com.badlogic.gdx.files FileHandle length

List of usage examples for com.badlogic.gdx.files FileHandle length

Introduction

In this page you can find the example usage for com.badlogic.gdx.files FileHandle length.

Prototype

public long length() 

Source Link

Document

Returns the length in bytes of this file, or 0 if this file is a directory, does not exist, or the size cannot otherwise be determined.

Usage

From source file:com.kotcrab.vis.editor.module.project.SceneIOModule.java

License:Apache License

public EditorScene load(FileHandle fullPathFile) {
    try {//w ww.j  a va 2  s.c  o  m
        if (fullPathFile.length() == 0)
            throw new EditorRuntimeException("Scene file does not contain any data");

        BufferedReader reader = new BufferedReader(new FileReader(fullPathFile.file()));
        EditorScene scene = gson.fromJson(reader, EditorScene.class);
        scene.path = fileAccessModule.relativizeToAssetsFolder(fullPathFile);
        reader.close();

        scene.onDeserialize();

        return scene;
    } catch (IOException e) {
        throw new IllegalStateException("There was an IO error during scene loading", e);
    }
}

From source file:com.kotcrab.vis.ui.widget.file.internal.FileHandleMetadata.java

License:Apache License

private FileHandleMetadata(FileHandle file) {
    this.name = file.name();
    this.directory = file.isDirectory();
    this.lastModified = file.lastModified();
    this.length = file.length();
    this.readableFileSize = FileUtils.readableFileSize(length);
}

From source file:com.strongjoshua.libgdx_utils.utils.io.ReadWriteObjects.java

License:Apache License

/**
 * Reads a specific value from memory.//from   w  w w.ja  v a2 s  .c  om
 * 
 * @param file The file to read from (done for efficiency).
 * @param token The token assigned to the value.
 * @return The value.
 */
public static Object readValue(String file, Object token) {
    FileHandle fh = Gdx.files.local(file);

    if (fh.length() == 0)
        return null;

    try {
        ObjectInputStream oi = new ObjectInputStream(fh.read());

        while (true) {
            Object o = oi.readObject();
            if (o.equals(endObject))
                break;
            Object v = oi.readObject();
            if (o.equals(token)) {
                oi.close();
                return v;
            }
        }
        oi.close();
        return null;
    } catch (Exception e) {
        System.out.println("Could not read.\n");
        e.printStackTrace();
        return null;
    }
}

From source file:com.strongjoshua.libgdx_utils.utils.io.ReadWriteObjects.java

License:Apache License

/**
 * Writes a specific value to memory./* w w w. j  av  a 2 s .  co  m*/
 * 
 * @param file The file to write the value to (done for efficiency).
 * @param token The token the value is to be assigned to.
 * @param val The value to be written.
 */
public static void writeValue(String file, Object token, Object val) {
    FileHandle fh = Gdx.files.local(file);
    FileHandle tmp = FileHandle.tempFile("tmp");

    boolean exists = false;

    ObjectOutputStream oo = null;
    ObjectInputStream oi = null;

    try {
        oo = new ObjectOutputStream(tmp.write(false));

        if (fh.length() != 0) {
            oi = new ObjectInputStream(fh.read());

            while (true) {
                Object o = oi.readObject();
                if (o.equals(endObject)) {
                    if (exists)
                        oo.writeObject(endObject);
                    break;
                }
                Object v = oi.readObject();
                if (o.equals(token)) {
                    exists = true;
                    v = val;
                }
                oo.writeObject(o);
                oo.writeObject(v);
            }
            oi.close();
        }

        if (!exists) {
            oo.writeObject(token);
            oo.writeObject(val);
            oo.writeObject(endObject);
        }

        oo.close();

        tmp.copyTo(fh);
    } catch (Exception e) {
        System.out.println("Could not save.");
        System.out.println();
        e.printStackTrace();
    }
}

From source file:com.strongjoshua.libgdx_utils.utils.io.ReadWriteObjects.java

License:Apache License

/**
 * Removes a value from the specified memory file.
 * //from   w ww.  j a va  2s  . c  om
 * @param file The file to be accessed.
 * @param token The token for the value to be removed (is removed as well).
 */
public static void removeValue(String file, Object token) {
    FileHandle fh = Gdx.files.local(file);
    FileHandle tmp = FileHandle.tempFile("tmp");

    ObjectOutputStream oo = null;
    ObjectInputStream oi = null;

    try {
        oo = new ObjectOutputStream(tmp.write(false));

        if (fh.length() != 0) {
            oi = new ObjectInputStream(fh.read());

            while (true) {
                Object o = oi.readObject();
                if (o.equals(endObject)) {
                    oo.writeObject(endObject);
                    break;
                }
                if (!o.equals(token)) {
                    oo.writeObject(o);
                    oo.writeObject(oi.readObject());
                }
            }
            oi.close();
        }
        oo.close();
        tmp.copyTo(fh);
    } catch (Exception e) {
        System.out.println("Could not remove.\n");
        e.printStackTrace();
    }
}

From source file:darkyenus.resourcepacker.util.FreeTypePacker.java

License:Apache License

/** Creates a new generator from the given font file. Uses {@link FileHandle#length()} to determine the file size. If the file
 * length could not be determined (it was 0), an extra copy of the font bytes is performed. Throws a
 * {@link GdxRuntimeException} if loading did not succeed. */
public FreeTypePacker(FileHandle fontFile) {
    this.fontFile = fontFile;
    int fileSize = (int) fontFile.length();

    library = FreeType.initFreeType();/*from  ww w.j ava 2 s  . c  o m*/
    if (library == null)
        throw new GdxRuntimeException("Couldn't initialize FreeType");

    ByteBuffer buffer;
    InputStream input = fontFile.read();
    try {
        if (fileSize == 0) {
            // Copy to a byte[] to get the file size, then copy to the buffer.
            byte[] data = StreamUtils.copyStreamToByteArray(input, 1024 * 16);
            buffer = BufferUtils.newUnsafeByteBuffer(data.length);
            BufferUtils.copy(data, 0, buffer, data.length);
        } else {
            // Trust the specified file size.
            buffer = BufferUtils.newUnsafeByteBuffer(fileSize);
            StreamUtils.copyStream(input, buffer);
        }
    } catch (IOException ex) {
        throw new GdxRuntimeException(ex);
    } finally {
        StreamUtils.closeQuietly(input);
    }

    face = library.newMemoryFace(buffer, 0);
    if (face == null)
        throw new GdxRuntimeException("Couldn't create face for font: " + fontFile);
}

From source file:de.tomgrill.gdxfacebook.core.GDXFacebookMultiPartRequest.java

License:Apache License

private static byte[] loadFile(FileHandle fileHandle) throws IOException {
    InputStream is = fileHandle.read();

    long length = fileHandle.length();
    if (length > Integer.MAX_VALUE) {
        throw new IOException("File size to large");
    }//from   ww  w .  j a  va  2  s  .  c  o  m
    byte[] bytes = new byte[(int) length];

    int offset = 0;
    int numRead;
    while (offset < bytes.length && (numRead = is.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
    }

    if (offset < bytes.length) {
        throw new IOException("Could not completely read file " + fileHandle.name());
    }

    is.close();
    return bytes;
}

From source file:es.eucm.ead.repobuilder.RepoLibraryBuilder.java

License:Open Source License

public void exportByLibrary(String outputDir) {
    setCommonProperty(OUTPUT, outputDir);

    createOutputFolder();/*w  ww  .  j  a  va  2s  .  c  om*/
    // Copy

    doBuild();
    escapeRepoElements();

    // Update version code in entities
    for (ModelEntity modelEntity : repoEntities) {
        for (ModelComponent modelComponent : modelEntity.getComponents()) {
            if (modelComponent instanceof RepoElement) {
                ((RepoElement) modelComponent).setVersion(properties.get(VERSION));
            }
        }
    }

    // Save entities.json
    FileHandle fh = rootFolder.child(ENTITIES_JSON);
    Gdx.app.debug(LOG_TAG, "Saving " + ENTITIES_JSON + " to: " + fh.file().getAbsolutePath());
    gameAssets.toJson(repoEntities, null, fh);

    // Zip entities.json (+resources & thumbnails) to output folder
    FileHandle outputZip = new FileHandle(outputDir);
    outputZip.mkdirs();
    outputZip = outputZip.child(root + ".zip");
    ZipUtils.zip(rootFolder, outputZip);

    // Update Mb
    float sizeInMb = outputZip.length() / 1048576F;
    if (sizeInMb > 0) {
        lastLibrary.setSize(sizeInMb);
    } else {
        lastLibrary.setSize(-1F);
    }

    // Update version for library
    lastLibrary.setVersion(properties.get(VERSION));

    // Create json for library
    FileHandle outputJson = new FileHandle(outputDir);
    outputJson = outputJson.child(root + ".json");
    gameAssets.toJson(lastLibrary, null, outputJson);
}

From source file:net.spookygames.gdx.sfx.desktop.DesktopAudioDurationResolver.java

License:Open Source License

private float mp3Duration(FileHandle musicFile) throws BitstreamException {
    Bitstream bitstream = new Bitstream(musicFile.read());
    int length = (int) musicFile.length();
    int streamPos = bitstream.header_pos();
    Header header = bitstream.readFrame();
    if ((streamPos > 0) && (length != AudioSystem.NOT_SPECIFIED) && (streamPos < length))
        length -= streamPos;//w  w w . j  av a2s  .  c  om
    float totalMilliseconds = header.total_ms(length);
    float durationInSeconds = totalMilliseconds / 1000f;
    return durationInSeconds;
}