Java Code Examples for com.badlogic.gdx.files.FileHandle#read()
The following examples show how to use
com.badlogic.gdx.files.FileHandle#read() .
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: Mini2DxOgg.java From mini2Dx with Apache License 2.0 | 6 votes |
public Sound(Mini2DxOpenALAudio audio, FileHandle file) { super(audio); if (audio.noDevice) return; OggInputStream input = null; try { input = new OggInputStream(file.read()); ByteArrayOutputStream output = new ByteArrayOutputStream(4096); byte[] buffer = new byte[2048]; while (!input.atEnd()) { int length = input.read(buffer); if (length == -1) break; output.write(buffer, 0, length); } setup(output.toByteArray(), input.getChannels(), input.getSampleRate()); } finally { StreamUtils.closeQuietly(input); } }
Example 2
Source File: Utils.java From skin-composer with MIT License | 6 votes |
/** * Extracts a zip file specified by the zipFilePath to a directory specified by * destDirectory (will be created if does not exists) * @param zipFile * @param destDirectory * @throws IOException */ public static void unzip(FileHandle zipFile, FileHandle destDirectory) throws IOException { destDirectory.mkdirs(); InputStream is = zipFile.read(); ZipInputStream zis = new ZipInputStream(is); ZipEntry entry = zis.getNextEntry(); // iterates over entries in the zip file while (entry != null) { if (!entry.isDirectory()) { // if the entry is a file, extracts it extractFile(zis, destDirectory.child(entry.getName())); } else { // if the entry is a directory, make the directory destDirectory.child(entry.getName()).mkdirs(); } zis.closeEntry(); entry = zis.getNextEntry(); } is.close(); zis.close(); }
Example 3
Source File: Mini2DxMp3.java From mini2Dx with Apache License 2.0 | 6 votes |
public Music (Mini2DxOpenALAudio audio, FileHandle file) { super(audio, file); if (audio.noDevice) return; bitstream = new Bitstream(file.read()); decoder = new MP3Decoder(); bufferOverhead = 4096; try { Header header = bitstream.readFrame(); if (header == null) throw new GdxRuntimeException("Empty MP3"); int channels = header.mode() == Header.SINGLE_CHANNEL ? 1 : 2; outputBuffer = new OutputBuffer(channels, false); decoder.setOutputBuffer(outputBuffer); setup(channels, header.getSampleRate()); } catch (BitstreamException e) { throw new GdxRuntimeException("error while preloading mp3", e); } }
Example 4
Source File: GDXFacebookMultiPartRequest.java From gdx-facebook with Apache License 2.0 | 6 votes |
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 5
Source File: AndroidModLoader.java From Cubes with MIT License | 6 votes |
public static String hashFile(FileHandle fileHandle) throws Exception { InputStream inputStream = fileHandle.read(); try { MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] bytesBuffer = new byte[1024]; int bytesRead; while ((bytesRead = inputStream.read(bytesBuffer)) != -1) { digest.update(bytesBuffer, 0, bytesRead); } byte[] hashedBytes = digest.digest(); return convertByteArrayToHexString(hashedBytes); } catch (IOException ex) { throw new CubesException("Could not generate hash from file " + fileHandle.path(), ex); } finally { StreamUtils.closeQuietly(inputStream); } }
Example 6
Source File: SaveAreaIO.java From Cubes with MIT License | 6 votes |
public static Area read(Save save, int x, int z) { FileHandle file = file(save, x, z); if (!file.exists()) { //Log.warning("Area does not exist"); return null; } Inflater inflater = inflaterThreadLocal.get(); try { inflater.reset(); InputStream inputStream = file.read(8192); InflaterInputStream inflaterInputStream = new InflaterInputStream(inputStream, inflater); BufferedInputStream bufferedInputStream = new BufferedInputStream(inflaterInputStream); DataInputStream dataInputStream = new DataInputStream(bufferedInputStream); Area area = new Area(x, z); area.read(dataInputStream); dataInputStream.close(); return area; } catch (Exception e) { Log.error("Failed to read area " + x + "," + z, e); return null; } }
Example 7
Source File: Save.java From Cubes with MIT License | 6 votes |
public Cave readCave(AreaReference areaReference) { if (fileHandle == null) return null; FileHandle folder = folderCave(); FileHandle file = folder.child(areaReference.areaX + "_" + areaReference.areaZ); if (!file.exists()) return null; try { InputStream read = file.read(); DataInputStream dataInputStream = new DataInputStream(read); Cave cave = Cave.read(dataInputStream); read.close(); return cave; } catch (IOException e) { Log.warning("Failed to read cave", e); return null; } }
Example 8
Source File: Mini2DxOgg.java From mini2Dx with Apache License 2.0 | 6 votes |
public Sound(Mini2DxOpenALAudio audio, FileHandle file) { super(audio); if (audio.noDevice) return; OggInputStream input = null; try { input = new OggInputStream(file.read()); ByteArrayOutputStream output = new ByteArrayOutputStream(4096); byte[] buffer = new byte[2048]; while (!input.atEnd()) { int length = input.read(buffer); if (length == -1) break; output.write(buffer, 0, length); } setup(output.toByteArray(), input.getChannels(), input.getSampleRate()); } finally { StreamUtils.closeQuietly(input); } }
Example 9
Source File: Mini2DxMp3.java From mini2Dx with Apache License 2.0 | 6 votes |
public Music (Mini2DxOpenALAudio audio, FileHandle file) { super(audio, file); if (audio.noDevice) return; bitstream = new Bitstream(file.read()); decoder = new MP3Decoder(); bufferOverhead = 4096; try { Header header = bitstream.readFrame(); if (header == null) throw new GdxRuntimeException("Empty MP3"); int channels = header.mode() == Header.SINGLE_CHANNEL ? 1 : 2; outputBuffer = new OutputBuffer(channels, false); decoder.setOutputBuffer(outputBuffer); setup(channels, header.getSampleRate()); } catch (BitstreamException e) { throw new GdxRuntimeException("error while preloading mp3", e); } }
Example 10
Source File: Mini2DxWav.java From mini2Dx with Apache License 2.0 | 5 votes |
WavInputStream (FileHandle file) { super(file.read()); try { if (read() != 'R' || read() != 'I' || read() != 'F' || read() != 'F') throw new GdxRuntimeException("RIFF header not found: " + file); skipFully(4); if (read() != 'W' || read() != 'A' || read() != 'V' || read() != 'E') throw new GdxRuntimeException("Invalid wave file header: " + file); int fmtChunkLength = seekToChunk('f', 'm', 't', ' '); int type = read() & 0xff | (read() & 0xff) << 8; if (type != 1) throw new GdxRuntimeException("WAV files must be PCM: " + type); channels = read() & 0xff | (read() & 0xff) << 8; if (channels != 1 && channels != 2) throw new GdxRuntimeException("WAV files must have 1 or 2 channels: " + channels); sampleRate = read() & 0xff | (read() & 0xff) << 8 | (read() & 0xff) << 16 | (read() & 0xff) << 24; skipFully(6); int bitsPerSample = read() & 0xff | (read() & 0xff) << 8; if (bitsPerSample != 16) throw new GdxRuntimeException("WAV files must have 16 bits per sample: " + bitsPerSample); skipFully(fmtChunkLength - 16); dataRemaining = seekToChunk('d', 'a', 't', 'a'); } catch (Throwable ex) { StreamUtils.closeQuietly(this); throw new GdxRuntimeException("Error reading WAV file: " + file, ex); } }
Example 11
Source File: Mini2DxMp3.java From mini2Dx with Apache License 2.0 | 5 votes |
public Sound (Mini2DxOpenALAudio audio, FileHandle file) { super(audio); if (audio.noDevice) return; ByteArrayOutputStream output = new ByteArrayOutputStream(4096); Bitstream bitstream = new Bitstream(file.read()); MP3Decoder decoder = new MP3Decoder(); try { OutputBuffer outputBuffer = null; int sampleRate = -1, channels = -1; while (true) { Header header = bitstream.readFrame(); if (header == null) break; if (outputBuffer == null) { channels = header.mode() == Header.SINGLE_CHANNEL ? 1 : 2; outputBuffer = new OutputBuffer(channels, false); decoder.setOutputBuffer(outputBuffer); sampleRate = header.getSampleRate(); } try { decoder.decodeFrame(header, bitstream); } catch (Exception ignored) { // JLayer's decoder throws ArrayIndexOutOfBoundsException sometimes!? } bitstream.closeFrame(); output.write(outputBuffer.getBuffer(), 0, outputBuffer.reset()); } bitstream.close(); setup(output.toByteArray(), channels, sampleRate); } catch (Throwable ex) { throw new GdxRuntimeException("Error reading audio data.", ex); } }
Example 12
Source File: Mini2DxWav.java From mini2Dx with Apache License 2.0 | 5 votes |
WavInputStream (FileHandle file) { super(file.read()); try { if (read() != 'R' || read() != 'I' || read() != 'F' || read() != 'F') throw new GdxRuntimeException("RIFF header not found: " + file); skipFully(4); if (read() != 'W' || read() != 'A' || read() != 'V' || read() != 'E') throw new GdxRuntimeException("Invalid wave file header: " + file); int fmtChunkLength = seekToChunk('f', 'm', 't', ' '); int type = read() & 0xff | (read() & 0xff) << 8; if (type != 1) throw new GdxRuntimeException("WAV files must be PCM: " + type); channels = read() & 0xff | (read() & 0xff) << 8; if (channels != 1 && channels != 2) throw new GdxRuntimeException("WAV files must have 1 or 2 channels: " + channels); sampleRate = read() & 0xff | (read() & 0xff) << 8 | (read() & 0xff) << 16 | (read() & 0xff) << 24; skipFully(6); int bitsPerSample = read() & 0xff | (read() & 0xff) << 8; if (bitsPerSample != 16) throw new GdxRuntimeException("WAV files must have 16 bits per sample: " + bitsPerSample); skipFully(fmtChunkLength - 16); dataRemaining = seekToChunk('d', 'a', 't', 'a'); } catch (Throwable ex) { StreamUtils.closeQuietly(this); throw new GdxRuntimeException("Error reading WAV file: " + file, ex); } }
Example 13
Source File: Mini2DxMp3.java From mini2Dx with Apache License 2.0 | 5 votes |
public Sound (Mini2DxOpenALAudio audio, FileHandle file) { super(audio); if (audio.noDevice) return; ByteArrayOutputStream output = new ByteArrayOutputStream(4096); Bitstream bitstream = new Bitstream(file.read()); MP3Decoder decoder = new MP3Decoder(); try { OutputBuffer outputBuffer = null; int sampleRate = -1, channels = -1; while (true) { Header header = bitstream.readFrame(); if (header == null) break; if (outputBuffer == null) { channels = header.mode() == Header.SINGLE_CHANNEL ? 1 : 2; outputBuffer = new OutputBuffer(channels, false); decoder.setOutputBuffer(outputBuffer); sampleRate = header.getSampleRate(); } try { decoder.decodeFrame(header, bitstream); } catch (Exception ignored) { // JLayer's decoder throws ArrayIndexOutOfBoundsException sometimes!? } bitstream.closeFrame(); output.write(outputBuffer.getBuffer(), 0, outputBuffer.reset()); } bitstream.close(); setup(output.toByteArray(), channels, sampleRate); } catch (Throwable ex) { throw new GdxRuntimeException("Error reading audio data.", ex); } }
Example 14
Source File: Art.java From uracer-kotd with Apache License 2.0 | 5 votes |
public static Texture getFlag (String countryCode) { String filename = countryCode + ".png"; FileHandle zip = Gdx.files.internal("data/flags.zip"); ZipInputStream zin = new ZipInputStream(zip.read()); ZipEntry ze = null; try { while ((ze = zin.getNextEntry()) != null) { if (ze.getName().equals(filename)) { ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream(); int bytesRead; byte[] tempBuffer = new byte[8192 * 2]; while ((bytesRead = zin.read(tempBuffer)) != -1) { streamBuilder.write(tempBuffer, 0, bytesRead); } Pixmap px = new Pixmap(streamBuilder.toByteArray(), 0, streamBuilder.size()); streamBuilder.close(); zin.close(); boolean mipMap = false; Texture t = new Texture(px); if (mipMap) { t.setFilter(TextureFilter.MipMapLinearNearest, TextureFilter.Nearest); } else { t.setFilter(TextureFilter.Nearest, TextureFilter.Nearest); } px.dispose(); return t; } } } catch (IOException e) { } return null; }
Example 15
Source File: Mini2DxOgg.java From mini2Dx with Apache License 2.0 | 4 votes |
public Music (Mini2DxOpenALAudio audio, FileHandle file) { super(audio, file); if (audio.noDevice) return; input = new OggInputStream(file.read()); setup(input.getChannels(), input.getSampleRate()); }
Example 16
Source File: Replay.java From uracer-kotd with Apache License 2.0 | 4 votes |
public static Replay load (String fullpathFilename) { FileHandle fh = Gdx.files.external(fullpathFilename); if (fh.exists()) { try { GZIPInputStream gzis = new GZIPInputStream(fh.read()); DataInputStream is = new DataInputStream(gzis); Replay r = new Replay(); r.info.completed = true; // replay info data r.info.replayId = is.readUTF(); r.info.userId = is.readUTF(); r.info.trackId = is.readUTF(); r.info.trackTimeTicks = is.readInt(); r.info.eventsCount = is.readInt(); r.info.created = is.readLong(); // car data r.carPositionMt.x = is.readFloat(); r.carPositionMt.y = is.readFloat(); r.carOrientationRads = is.readFloat(); for (int i = 0; i < r.info.eventsCount; i++) { r.forces[i].velocity_x = is.readFloat(); r.forces[i].velocity_y = is.readFloat(); r.forces[i].angularVelocity = is.readFloat(); } is.close(); return r; } catch (Exception e) { Gdx.app.log("Replay", "Couldn't load replay (" + fullpathFilename + "), reason: " + e.getMessage() + " (" + e.toString() + ")"); } } else { Gdx.app.log("Replay", "The specified replay doesn't exist (" + fullpathFilename + ")"); } return null; }
Example 17
Source File: Game.java From shattered-pixel-dungeon-gdx with GNU General Public License v3.0 | 4 votes |
public InputStream openFileInput(String fileName) throws IOException { final FileHandle fh = Gdx.files.external(basePath != null ? basePath + fileName : fileName); if (!fh.exists()) throw new IOException("File " + fileName + " doesn't exist"); return fh.read(); }
Example 18
Source File: MidiSequence.java From gdx-pd with Apache License 2.0 | 4 votes |
public MidiSequence(FileHandle file) { this(file.read()); }
Example 19
Source File: FreeTypeFontGenerator.java From gdx-texture-packer-gui with Apache License 2.0 | 4 votes |
/** 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 20
Source File: Mini2DxOgg.java From mini2Dx with Apache License 2.0 | 4 votes |
public Music (Mini2DxOpenALAudio audio, FileHandle file) { super(audio, file); if (audio.noDevice) return; input = new OggInputStream(file.read()); setup(input.getChannels(), input.getSampleRate()); }