Java Code Examples for com.badlogic.gdx.files.FileHandle#length()

The following examples show how to use com.badlogic.gdx.files.FileHandle#length() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileSizeMetadataProcessor.java    From gdx-texture-packer-gui with Apache License 2.0 6 votes vote down vote up
@Override
public void processPackage(PackProcessingNode node) throws Exception {
    PackModel pack = node.getPack();
    ProjectModel project = node.getProject();

    FileHandle packFileHandle = Gdx.files.absolute(pack.getOutputDir()).child(pack.getCanonicalFilename());
    FileHandle imagesDirFileHandle = Gdx.files.absolute(pack.getOutputDir());
    TextureAtlas.TextureAtlasData atlasData = new TextureAtlas.TextureAtlasData(
            packFileHandle,
            imagesDirFileHandle,
            false);

    long totalSize = 0; // Bytes
    totalSize += packFileHandle.length();
    for (TextureAtlas.TextureAtlasData.Page page : atlasData.getPages()) {
        long pageSize = page.textureFile.length();
        totalSize += pageSize;
    }
    node.addMetadata(PackProcessingNode.META_FILE_SIZE, totalSize);

    System.out.println("Total pack files size is " + totalSize + " bytes");
}
 
Example 2
Source File: GDXFacebookMultiPartRequest.java    From gdx-facebook with Apache License 2.0 6 votes vote down vote up
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");
    }
    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;
}
 
Example 3
Source File: D2S.java    From riiablo with Apache License 2.0 5 votes vote down vote up
public static D2S loadFromFile(FileHandle file) {
  ByteBuffer buffer = file.map().order(ByteOrder.LITTLE_ENDIAN);
  Header header = Header.obtain(buffer);
  if (DEBUG_HEADER) Gdx.app.debug(TAG, header.toString());
  if (header.magicNumber != MAGIC_NUMBER) throw new GdxRuntimeException("Magic number doesn't match " + String.format("0x%08X", MAGIC_NUMBER) + ": " + String.format("0x%08X", header.magicNumber));
  if (header.version != VERSION_110) throw new GdxRuntimeException("Unsupported D2S version: " + header.version + " -- Only supports " + header.getVersionString(VERSION_110));
  if (header.size != file.length()) Gdx.app.error(TAG, "Save file size doesn't match encoded size for character " + header.name + ". Should be: " + header.size);
  return new D2S(file, header);
}
 
Example 4
Source File: FileHandleMetadata.java    From vis-ui with Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 5
Source File: FreeTypeFontGenerator.java    From gdx-texture-packer-gui with Apache License 2.0 4 votes vote down vote up
/** 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 FreeTypeFontGenerator (FileHandle fontFile, int faceIndex) {
	name = fontFile.pathWithoutExtension();
	int fileSize = (int)fontFile.length();

	library = FreeType.initFreeType();
	if (library == null) throw new GdxRuntimeException("Couldn't initialize FreeType");

	ByteBuffer buffer = null;

	try {
		buffer = fontFile.map();
	} catch (GdxRuntimeException e) {
		// Silently error, certain platforms do not support file mapping.
	}

	if (buffer == null) {
		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, faceIndex);
	if (face == null) throw new GdxRuntimeException("Couldn't create face for font: " + fontFile);

	if (checkForBitmapFont()) return;
	setPixelSizes(0, 15);
}
 
Example 6
Source File: FileUtils.java    From vis-ui with Apache License 2.0 4 votes vote down vote up
@Override
public int compare (FileHandle f1, FileHandle f2) {
	long l1 = f1.length();
	long l2 = f2.length();
	return l1 > l2 ? -1 : (l1 == l2 ? FILE_NAME_COMPARATOR.compare(f1, f2) : 1);
}