com.badlogic.gdx.utils.StreamUtils Java Examples
The following examples show how to use
com.badlogic.gdx.utils.StreamUtils.
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: 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 #2
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 #3
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 #4
Source File: BehaviorTreeReader.java From gdx-ai with Apache License 2.0 | 6 votes |
/** Parses the given reader. * @param reader the reader * @throws SerializationException if the reader cannot be successfully parsed. */ public void parse (Reader reader) { try { char[] data = new char[1024]; int offset = 0; while (true) { int length = reader.read(data, offset, data.length - offset); if (length == -1) break; if (length == 0) { char[] newData = new char[data.length * 2]; System.arraycopy(data, 0, newData, 0, data.length); data = newData; } else offset += length; } parse(data, 0, offset); } catch (IOException ex) { throw new SerializationException(ex); } finally { StreamUtils.closeQuietly(reader); } }
Example #5
Source File: BehaviorTreeLoader.java From gdx-ai with Apache License 2.0 | 6 votes |
@SuppressWarnings("unchecked") @Override public void loadAsync (AssetManager manager, String fileName, FileHandle file, BehaviorTreeParameter parameter) { this.behaviorTree = null; Object blackboard = null; BehaviorTreeParser parser = null; if (parameter != null) { blackboard = parameter.blackboard; parser = parameter.parser; } if (parser == null) parser = new BehaviorTreeParser(); Reader reader = null; try { reader = file.reader(); this.behaviorTree = parser.parse(reader, blackboard); } finally { StreamUtils.closeQuietly(reader); } }
Example #6
Source File: SemaphoreGuardTest.java From gdx-ai with Apache License 2.0 | 6 votes |
@Override public Actor createActor (Skin skin) { // Create the semaphore NonBlockingSemaphoreRepository.clear(); NonBlockingSemaphoreRepository.addSemaphore("dogSemaphore", 1); Reader reader = null; try { // Parse Buddy's tree reader = Gdx.files.internal("data/dogSemaphore.tree").reader(); BehaviorTreeParser<Dog> parser = new BehaviorTreeParser<Dog>(BehaviorTreeParser.DEBUG_HIGH); BehaviorTree<Dog> buddyTree = parser.parse(reader, new Dog("Buddy")); // Clone Buddy's tree for Snoopy BehaviorTree<Dog> snoopyTree = (BehaviorTree<Dog>)buddyTree.cloneTask(); snoopyTree.setObject(new Dog("Snoopy")); // Create split pane BehaviorTreeViewer<?> buddyTreeViewer = createTreeViewer(buddyTree.getObject().name, buddyTree, false, skin); BehaviorTreeViewer<?> snoopyTreeViewer = createTreeViewer(snoopyTree.getObject().name, snoopyTree, false, skin); return new SplitPane(new ScrollPane(buddyTreeViewer, skin), new ScrollPane(snoopyTreeViewer, skin), true, skin, "default-horizontal"); } finally { StreamUtils.closeQuietly(reader); } }
Example #7
Source File: COFD2.java From riiablo with Apache License 2.0 | 6 votes |
public static COFD2 loadFromStream(InputStream in) { try { int i = 0; Array<Entry> entries = new Array<>(1024); while (in.available() > 0) { Entry entry = new Entry(in); entries.add(entry); if (DEBUG_ENTRIES) Gdx.app.debug(TAG, i++ + ": " + entry.toString()); if (entry.header1 != -1 || entry.header2 != 0 || entry.header3 != -1) { Gdx.app.error(TAG, "Invalid entry headers: " + entry); } } return new COFD2(entries); } catch (Throwable t) { throw new GdxRuntimeException("Couldn't load D2 from stream.", t); } finally { StreamUtils.closeQuietly(in); } }
Example #8
Source File: D2.java From riiablo with Apache License 2.0 | 6 votes |
public static D2 loadFromStream(InputStream in) { try { int numBlocks = Block.MAX_VALUE; Block[] blocks = new Block[numBlocks]; for (int i = 0; i < numBlocks; i++) { blocks[i] = new Block(in); if (DEBUG_BLOCKS) Gdx.app.debug(TAG, i + ": " + blocks[i].toString()); } return new D2(blocks); } catch (Throwable t) { throw new GdxRuntimeException("Couldn't load D2 from stream.", t); } finally { StreamUtils.closeQuietly(in); } }
Example #9
Source File: FileUtils.java From gdx-texture-packer-gui with Apache License 2.0 | 6 votes |
public static void unpackZip(FileHandle input, FileHandle output) throws IOException { ZipFile zipFile = new ZipFile(input.file()); File outputDir = output.file(); try { Enumeration<? extends ZipEntry> entries = zipFile.entries(); while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); File entryDestination = new File(outputDir, entry.getName()); if (entry.isDirectory()) { entryDestination.mkdirs(); } else { entryDestination.getParentFile().mkdirs(); InputStream in = zipFile.getInputStream(entry); OutputStream out = new FileOutputStream(entryDestination); StreamUtils.copyStream(in, out); StreamUtils.closeQuietly(in); out.close(); } } } finally { StreamUtils.closeQuietly(zipFile); } }
Example #10
Source File: GpgsClient.java From gdx-gamesvcs with Apache License 2.0 | 6 votes |
/** * Blocking version of {@link #loadGameState(String, ILoadGameStateResponseListener)} * * @param fileId * @return game state data * @throws IOException */ public byte[] loadGameStateSync(String fileId) throws IOException { InputStream stream = null; byte[] data = null; try { File remoteFile = findFileByNameSync(fileId); if (remoteFile != null) { stream = GApiGateway.drive.files().get(remoteFile.getId()).executeMediaAsInputStream(); data = StreamUtils.copyStreamToByteArray(stream); } } finally { StreamUtils.closeQuietly(stream); } return data; }
Example #11
Source File: DownloadUtil.java From gdx-gamesvcs with Apache License 2.0 | 6 votes |
public static byte[] download(String url) throws IOException { InputStream in = null; try { HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setDoInput(true); conn.setDoOutput(false); conn.setUseCaches(true); conn.connect(); in = conn.getInputStream(); return StreamUtils.copyStreamToByteArray(in); } catch (IOException ex) { throw ex; } finally { StreamUtils.closeQuietly(in); } }
Example #12
Source File: FileUtils.java From gdx-texture-packer-gui with Apache License 2.0 | 6 votes |
/** Synchronously downloads file by URL*/ public static void downloadFile(FileHandle output, String urlString) throws IOException { // ReadableByteChannel rbc = null; // FileOutputStream fos = null; // try { // URL url = new URL(urlString); // rbc = Channels.newChannel(url.openStream()); // fos = new FileOutputStream(output.file()); // fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); // } finally { // StreamUtils.closeQuietly(rbc); // StreamUtils.closeQuietly(fos); // } InputStream in = null; FileOutputStream out = null; try { URL url = new URL(urlString); in = url.openStream(); out = new FileOutputStream(output.file()); StreamUtils.copyStream(in, out); } finally { StreamUtils.closeQuietly(in); StreamUtils.closeQuietly(out); } }
Example #13
Source File: Mini2DxWav.java From mini2Dx with Apache License 2.0 | 5 votes |
public Sound (Mini2DxOpenALAudio audio, FileHandle file) { super(audio); if (audio.noDevice) return; WavInputStream input = null; try { input = new WavInputStream(file); setup(StreamUtils.copyStreamToByteArray(input, input.dataRemaining), input.channels, input.sampleRate); } catch (IOException ex) { throw new GdxRuntimeException("Error reading WAV file: " + file, ex); } finally { StreamUtils.closeQuietly(input); } }
Example #14
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 #15
Source File: Mini2DxWav.java From mini2Dx with Apache License 2.0 | 5 votes |
public Sound (Mini2DxOpenALAudio audio, FileHandle file) { super(audio); if (audio.noDevice) return; WavInputStream input = null; try { input = new WavInputStream(file); setup(StreamUtils.copyStreamToByteArray(input, input.dataRemaining), input.channels, input.sampleRate); } catch (IOException ex) { throw new GdxRuntimeException("Error reading WAV file: " + file, ex); } finally { StreamUtils.closeQuietly(input); } }
Example #16
Source File: BehaviorTreeReader.java From gdx-ai with Apache License 2.0 | 5 votes |
/** Parses the given input stream. * @param input the input stream * @throws SerializationException if the input stream cannot be successfully parsed. */ public void parse (InputStream input) { try { parse(new InputStreamReader(input, "UTF-8")); } catch (IOException ex) { throw new SerializationException(ex); } finally { StreamUtils.closeQuietly(input); } }
Example #17
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 #18
Source File: ParseTreeTest.java From gdx-ai with Apache License 2.0 | 5 votes |
@Override public Actor createActor (Skin skin) { Reader reader = null; try { reader = Gdx.files.internal("data/dog.tree").reader(); BehaviorTreeParser<Dog> parser = new BehaviorTreeParser<Dog>(BehaviorTreeParser.DEBUG_HIGH); BehaviorTree<Dog> tree = parser.parse(reader, new Dog("Buddy")); treeViewer = createTreeViewer(tree.getObject().name, tree, true, skin); return new ScrollPane(treeViewer, skin); } finally { StreamUtils.closeQuietly(reader); } }
Example #19
Source File: SafeHttpResponseListener.java From gdx-gltf with Apache License 2.0 | 5 votes |
@Override public void handleHttpResponse(HttpResponse httpResponse) { try { final byte [] bytes = StreamUtils.copyStreamToByteArray(httpResponse.getResultAsStream()); Gdx.app.postRunnable(new Runnable() { @Override public void run() { handleData(bytes); handleEnd(); } }); } catch (IOException e) { failed(e); } }
Example #20
Source File: StringTBL.java From riiablo with Apache License 2.0 | 5 votes |
public static StringTBL loadFromStream(InputStream in) { try { Header header = new Header(in); if (DEBUG) Gdx.app.debug(TAG, header.toString()); if (header.version != 1) throw new GdxRuntimeException("Unsupported version: " + (header.version & 0xFF)); short[] indexes = new short[header.numElements]; ByteBuffer.wrap(IOUtils.readFully(in, header.numElements * 2)) .order(ByteOrder.LITTLE_ENDIAN) .asShortBuffer() .get(indexes); if (DEBUG) Gdx.app.debug(TAG, "indexes = " + Arrays.toString(indexes)); HashTable hashTable = new HashTable(header, in); if (DEBUG) Gdx.app.debug(TAG, hashTable.toString()); // TODO: Support other languages final int dataSize = header.endIndex - header.startIndex; char[] text = new char[dataSize]; Reader reader = new InputStreamReader(in, "US-ASCII"); IOUtils.readFully(reader, text); return new StringTBL(header, indexes, hashTable, text); } catch (Throwable t) { throw new GdxRuntimeException("Couldn't load StringTBL from stream.", t); } finally { StreamUtils.closeQuietly(in); } }
Example #21
Source File: PL2.java From riiablo with Apache License 2.0 | 5 votes |
public static PL2 loadFromStream(InputStream in) { try { // Skip the internal palette data which was loaded in the corresponding .dat IOUtils.skipFully(in, 0x400); // TODO: It may be faster to implement using single dimension using offsets byte[][] colormaps = new byte[COLORMAPS + TINTS][Palette.COLORS]; for (int i = 0; i < COLORMAPS; i++) { IOUtils.readFully(in, colormaps[i]); } int available = in.available(); if (available % TINT_SIZE > 0) { throw new GdxRuntimeException("Remaining bytes does not match an expected tint count."); } final int tintsUsed = available / TINT_SIZE; int[] tints = new int[tintsUsed]; for (int i = 0, r, g, b; i < tintsUsed; i++) { r = in.read(); g = in.read(); b = in.read(); tints[i] = (r << 16) | (g << 8) | b; } // NOTE: LOADING PL2 is missing a tint, so readFully will not work here :/ for (int i = 0; i < tintsUsed; i++) { IOUtils.readFully(in, colormaps[COLORMAPS + i]); } return new PL2(colormaps, tints); } catch (Throwable t) { throw new GdxRuntimeException("Couldn't load PL2 from stream.", t); } finally { StreamUtils.closeQuietly(in); } }
Example #22
Source File: Index.java From riiablo with Apache License 2.0 | 5 votes |
public static Index loadFromStream(InputStream in) { try { byte[] data = new byte[(INDEXES - 1) * Palette.COLORS]; IOUtils.readFully(in, data); return loadFromArray(null, data); } catch (Throwable t) { throw new GdxRuntimeException("Couldn't load palette from stream.", t); } finally { StreamUtils.closeQuietly(in); } }
Example #23
Source File: Palette.java From riiablo with Apache License 2.0 | 5 votes |
public static Palette loadFromStream(InputStream in) { try { byte[] data = new byte[COLORS * 3]; IOUtils.readFully(in, data); return loadFromArray(data); } catch (Throwable t) { throw new GdxRuntimeException("Couldn't load palette from stream.", t); } finally { StreamUtils.closeQuietly(in); } }
Example #24
Source File: Mini2DxOgg.java From mini2Dx with Apache License 2.0 | 4 votes |
public void reset () { StreamUtils.closeQuietly(input); previousInput = null; input = null; }
Example #25
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 #26
Source File: Mini2DxOgg.java From mini2Dx with Apache License 2.0 | 4 votes |
@Override protected void loop () { StreamUtils.closeQuietly(input); previousInput = input; input = null; }
Example #27
Source File: Mini2DxWav.java From mini2Dx with Apache License 2.0 | 4 votes |
public void reset () { StreamUtils.closeQuietly(input); input = null; }
Example #28
Source File: Mini2DxOgg.java From mini2Dx with Apache License 2.0 | 4 votes |
public void reset () { StreamUtils.closeQuietly(input); previousInput = null; input = null; }
Example #29
Source File: Mini2DxOgg.java From mini2Dx with Apache License 2.0 | 4 votes |
@Override protected void loop () { StreamUtils.closeQuietly(input); previousInput = input; input = null; }
Example #30
Source File: MPQViewer.java From riiablo with Apache License 2.0 | 4 votes |
private void readMPQs() { if (fileTreeNodes == null) { fileTreeNodes = new PatriciaTrie<>(); fileTreeCofNodes = new PatriciaTrie<>(); } else { fileTreeNodes.clear(); fileTreeCofNodes.clear(); } BufferedReader reader = null; try { //if (options_useExternalList.isChecked()) { reader = Gdx.files.internal(ASSETS + "(listfile)").reader(4096); //} else { // try { // reader = new BufferedReader(new InputStreamReader((new ByteArrayInputStream(mpq.readBytes("(listfile)"))))); // } catch (Throwable t) { // reader = Gdx.files.internal(ASSETS + "(listfile)").reader(4096); // } //} Node<Node, Object, Actor> root = new BaseNode(new VisLabel("root")); final boolean checkExisting = options_checkExisting.isChecked(); String fileName; while ((fileName = reader.readLine()) != null) { if (checkExisting && !Riiablo.mpqs.contains(fileName)) { continue; } String path = FilenameUtils.getPathNoEndSeparator(fileName).toLowerCase(); treeify(fileTreeNodes, root, path); final MPQFileHandle handle = (MPQFileHandle) Riiablo.mpqs.resolve(fileName); VisLabel label = new VisLabel(FilenameUtils.getName(fileName)); final Node node = new BaseNode(label); node.setValue(handle); label.addListener(new ClickListener(Input.Buttons.RIGHT) { @Override public void clicked(InputEvent event, float x, float y) { showPopmenu(node, handle); } }); String key = fileName.toLowerCase(); fileTreeNodes.put(key, node); if (FilenameUtils.isExtension(key, "cof")) { key = FilenameUtils.getBaseName(key); fileTreeCofNodes.put(key, node); } if (path.isEmpty()) { root.add(node); } else { fileTreeNodes.get(path + "\\").add(node); } } sort(root); fileTree.clearChildren(); for (Node child : root.getChildren()) { fileTree.add(child); } fileTree.layout(); fileTreeFilter.clearText(); } catch (IOException e) { throw new GdxRuntimeException("Failed to read list file.", e); } finally { StreamUtils.closeQuietly(reader); } }