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

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

Introduction

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

Prototype

public OutputStream write(boolean append) 

Source Link

Document

Returns a stream for writing to this file.

Usage

From source file:at.tugraz.ist.catroid.stage.StageListener.java

License:Open Source License

private boolean saveScreenshot(byte[] screenshot) {
    int length = screenshot.length;
    int[] colors = new int[length / 4];

    for (int i = 0; i < length; i += 4) {
        colors[i / 4] = Color.argb(255, screenshot[i + 0] & 0xFF, screenshot[i + 1] & 0xFF,
                screenshot[i + 2] & 0xFF);
    }/*from   w w  w.  ja v a2s  .  com*/

    Bitmap bitmap = Bitmap.createBitmap(colors, 0, screenshotWidth, screenshotWidth, screenshotHeight,
            Config.ARGB_8888);

    FileHandle image = Gdx.files.absolute(pathForScreenshot + SCREENSHOT_FILE_NAME);
    OutputStream stream = image.write(false);
    try {
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
        stream.close();
    } catch (IOException e) {
        return false;
    }
    return true;
}

From source file:br.com.questingsoftware.libgdx.g3db.G3DBConverter.java

License:Apache License

/** <p>
 * Convert a text JSON file into binary JSON. The new file will be saved in the same folder as the original one with the
 * {@code gd3b} extension.//from w  w  w .java 2 s  .c  om
 * </p>
 * 
 * @param g3djFile Handle to the original G3DJ file.
 * @param overwrite If {@code true} the new file will overwrite any previous file with the same name. Otherwise append a
 *           counter at the end of the file name to make it unique.
 * @throws IOException If there's an exception while reading the input file or writing the output file. */
public void convert(FileHandle g3djFile, boolean overwrite) throws IOException {
    FileHandle newFile = new FileHandle(g3djFile.pathWithoutExtension() + ".g3db");
    int noOverwriteCounter = 0;
    while (!overwrite && newFile.exists()) {
        newFile = new FileHandle(g3djFile.pathWithoutExtension() + "(" + (++noOverwriteCounter) + ").g3db");
    }

    OutputStream fileOutputStream = newFile.write(false);
    UBJsonWriter writer = new UBJsonWriter(fileOutputStream);
    JsonReader reader = new JsonReader();

    try {
        JsonValue root = reader.parse(g3djFile);
        writeObject(root, writer);

    } finally {
        writer.close();
    }
}

From source file:com.andgate.ikou.io.LevelService.java

License:Open Source License

public static void write(final Level level) throws IOException {
    // Shrinking causes json to write output forever.
    // FIXME: Compress the level :)
    //level.shrink();

    String levelFileName = level.getName() + Constants.LEVEL_EXTENSION;
    FileHandle levelFile = Gdx.files.external(Constants.LEVELS_EXTERNAL_PATH + levelFileName);

    OutputStream levelOut = new GZIPOutputStream(levelFile.write(false));
    Writer tmpWriter = new BufferedWriter(new OutputStreamWriter(levelOut));
    JsonWriter jsonWriter = new JsonWriter(tmpWriter);

    try {//from  w w  w  . j a va2 s . c om
        levelOut.write(level.getFloors().length - 1);
        Gson gson = new Gson();
        gson.toJson(level, jsonWriter);
    } finally {
        jsonWriter.close();
    }
}

From source file:com.crashinvaders.texturepackergui.utils.PNG8.java

License:Open Source License

/**
 * Writes the given Pixmap to the requested FileHandle, optionally computing an 8-bit palette from the most common
 * colors in pixmap. When computePalette is true, if there are 256 or less colors and none are transparent, this
 * will use 256 colors in its palette exactly with no transparent entry, but if there are more than 256 colors or
 * any are transparent, then one color will be used for "fully transparent" and 255 opaque colors will be used. When
 * computePalette is false, this uses the last palette this had computed, or a 253-color bold palette with one
 * fully-transparent color if no palette had been computed yet.
 * @param file a FileHandle that must be writable, and will have the given Pixmap written as a PNG-8 image
 * @param pixmap a Pixmap to write to the given file
 * @param computePalette if true, this will analyze the Pixmap and use the most common colors
 * @throws IOException if file writing fails for any reason
 *///ww  w .  jav a  2 s. co m
public void write(FileHandle file, Pixmap pixmap, boolean computePalette) throws IOException {
    OutputStream output = file.write(false);
    try {
        write(output, pixmap, computePalette);
    } finally {
        StreamUtils.closeQuietly(output);
    }
}

From source file:com.crashinvaders.texturepackergui.utils.PNG8.java

License:Open Source License

/**
 * Writes the pixmap to the stream without closing the stream, optionally computing an 8-bit palette from the given
 * Pixmap. If {@link #palette} is null (the default unless it has been assigned a PaletteReducer value), this will
 * compute a palette from the given Pixmap regardless of computePalette. Optionally dithers the result if
 * {@code dither} is true./*from  w w w  . jav  a  2  s  . com*/
 * @param file a FileHandle that must be writable, and will have the given Pixmap written as a PNG-8 image
 * @param pixmap a Pixmap to write to the given output stream
 * @param computePalette if true, this will analyze the Pixmap and use the most common colors
 * @param dither true if this should dither colors that can't be represented exactly
 * @throws IOException if file writing fails for any reason
 */
public void write(FileHandle file, Pixmap pixmap, boolean computePalette, boolean dither) throws IOException {
    OutputStream output = file.write(false);
    try {
        write(output, pixmap, computePalette, dither);
    } finally {
        StreamUtils.closeQuietly(output);
    }
}

From source file:com.crashinvaders.texturepackergui.utils.PNG8.java

License:Open Source License

/**
 * Writes the pixmap to the stream without closing the stream, optionally computing an 8-bit palette from the given
 * Pixmap. If {@link #palette} is null (the default unless it has been assigned a PaletteReducer value), this will
 * compute a palette from the given Pixmap regardless of computePalette. Uses the given threshold while analyzing
 * the palette if this needs to compute a palette; threshold values can be as low as 0 to try to use as many colors
 * as possible (prefer {@link  #writePrecisely(FileHandle, Pixmap, boolean, int)} for that, though) and can range up
 * to very high numbers if very few colors should be used; usually threshold is from 100 to 800. Optionally dithers
 * the result if {@code dither} is true.
 * @param file a FileHandle that must be writable, and will have the given Pixmap written as a PNG-8 image
 * @param pixmap a Pixmap to write to the given output stream
 * @param computePalette if true, this will analyze the Pixmap and use the most common colors
 * @param dither true if this should dither colors that can't be represented exactly
 * @param threshold the analysis threshold to use if computePalette is true (min 0, practical max is over 100000)
 * @throws IOException if file writing fails for any reason
 *//*from w  w w.j a v  a2  s  . c om*/
public void write(FileHandle file, Pixmap pixmap, boolean computePalette, boolean dither, int threshold)
        throws IOException {
    OutputStream output = file.write(false);
    try {
        write(output, pixmap, computePalette, dither, threshold);
    } finally {
        StreamUtils.closeQuietly(output);
    }
}

From source file:com.crashinvaders.texturepackergui.utils.PNG8.java

License:Open Source License

/**
 * Attempts to write the given Pixmap exactly as a PNG-8 image to file; this attempt will only succeed if there
 * are no more than 256 colors in the Pixmap (treating all partially transparent colors as fully transparent).
 * If the attempt fails, this falls back to calling {@link #write(FileHandle, Pixmap, boolean, boolean)}, which
 * can dither the image to use no more than 255 colors (plus fully transparent) based on ditherFallback and will
 * always analyze the Pixmap to get an accurate-enough palette, using the given threshold for analysis (which is
 * typically between 1 and 1000, and most often near 200-400). All other write() methods in this class will
 * reduce the color depth somewhat, but as long as the color count stays at 256 or less, this will keep the
 * non-alpha components of colors exactly.
 * @param file a FileHandle that must be writable, and will have the given Pixmap written as a PNG-8 image
 * @param pixmap a Pixmap to write to the given output stream
 * @param ditherFallback if the Pixmap contains too many colors, this determines whether it will dither the output
 * @param threshold the analysis threshold to use if there are too many colors (min 0, practical max is over 100000)
 * @throws IOException if file writing fails for any reason
 *//*from  ww w. j av a2  s.  c  o m*/
public void writePrecisely(FileHandle file, Pixmap pixmap, boolean ditherFallback, int threshold)
        throws IOException {
    OutputStream output = file.write(false);
    try {
        writePrecisely(output, pixmap, ditherFallback, threshold);
    } finally {
        StreamUtils.closeQuietly(output);
    }
}

From source file:com.idp.engine.base.IdpFiles.java

/**
 * Writes given object to a file.//from w w  w  . j a  v a2s .c  om
 * @param fileHandle file to write to
 * @param object object to write
 */
public void writeFile(FileHandle fileHandle, Object object) {

    OutputStream fileStream = null;
    ObjectOutputStream objStream = null;

    try {
        fileStream = fileHandle.write(false);
        objStream = new ObjectOutputStream(fileStream);
        objStream.writeObject(object);
    } catch (Exception ex) {
        if (LOG_EXCEPTIONS)
            Idp.logger.log(ex);
        throw new RuntimeException("cannot save to file: " + fileHandle.path(), ex);
    } finally {
        try {
            if (objStream != null)
                objStream.close();
            if (fileStream != null)
                fileStream.close();
        } catch (IOException ex) {
            if (LOG_EXCEPTIONS)
                Idp.logger.log(ex);
        }
    }
}

From source file:com.lyeeedar.Roguelike3D.Game.SaveGame.java

License:Open Source License

public static void save(SaveGame save) {
    FileHandle file = Gdx.files.local(SAVE_FILE);

    ObjectOutputStream out = null;
    try {/*from  w  w  w.  j ava2s.  co m*/
        out = new ObjectOutputStream(file.write(false));
    } catch (IOException e2) {
        e2.printStackTrace();
    }
    try {
        out.writeObject(save);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        out.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.mangecailloux.pebble.constant.ConstantManager.java

License:Apache License

/**
 * <p>Save the constants, used the external file path.</p>
 * @return true if the save occurred, false if it failed. Logging will give more infos.
 *///from ww  w. ja  va 2s.c  o m
public boolean saveConstants() {
    // We check if the external storage is available.
    if (!Gdx.files.isExternalStorageAvailable()) {
        if (logEnabled)
            Gdx.app.log("ConstantManager", "No external storage available, save failed.");
        return false;
    }

    FileHandle handle = Gdx.files.external(externalFilePath);
    OutputStreamWriter out = null;
    try {
        OutputStream outStream = handle.write(false);
        if (outStream != null) {
            // clean the constants before saving them to have only the latest sorted data.
            clean();
            String xml = root.toXML();
            out = new OutputStreamWriter(outStream);
            out.write(xml);
        }
    } catch (IOException e) {
        if (logEnabled)
            Gdx.app.log("ConstantManager", "Cannot write file : " + externalFilePath + ", save failed.");
    } finally {
        if (out != null) {
            try {
                out.close();
                return true;
            } catch (IOException e) {
                if (logEnabled)
                    Gdx.app.log("ConstantManager",
                            "Cannot close file : " + externalFilePath + ", save failed.");
            }
        }
    }
    return false;
}