Example usage for java.io RandomAccessFile write

List of usage examples for java.io RandomAccessFile write

Introduction

In this page you can find the example usage for java.io RandomAccessFile write.

Prototype

public void write(byte b[]) throws IOException 

Source Link

Document

Writes b.length bytes from the specified byte array to this file, starting at the current file pointer.

Usage

From source file:au.org.ala.layers.intersect.Grid.java

/**
 * for grid cutter/*w  w  w. j  av  a  2  s  . c  o m*/
 * <p/>
 * writes out a list of double (same as getGrid() returns) to a file
 * <p/>
 * byteorderlsb
 * data type, FLOAT
 *
 * @param newfilename
 * @param dfiltered
 */
public void writeGrid(String newfilename, double[] dfiltered, double xmin, double ymin, double xmax,
        double ymax, double xres, double yres, int nrows, int ncols) {
    int size, i, length = dfiltered.length;
    double maxvalue = Double.MAX_VALUE * -1;
    double minvalue = Double.MAX_VALUE;

    //write data as whole file
    RandomAccessFile afile = null;
    try { //read of random access file can throw an exception
        afile = new RandomAccessFile(newfilename + ".gri", "rw");

        size = 4;
        byte[] b = new byte[size * length];
        ByteBuffer bb = ByteBuffer.wrap(b);

        if (byteorderLSB) {
            bb.order(ByteOrder.LITTLE_ENDIAN);
        } else {
            bb.order(ByteOrder.BIG_ENDIAN);
        }
        for (i = 0; i < length; i++) {
            if (Double.isNaN(dfiltered[i])) {
                bb.putFloat((float) noDataValueDefault);
            } else {
                if (minvalue > dfiltered[i]) {
                    minvalue = dfiltered[i];
                }
                if (maxvalue < dfiltered[i]) {
                    maxvalue = dfiltered[i];
                }
                bb.putFloat((float) dfiltered[i]);
            }
        }

        afile.write(b);
    } catch (Exception e) {
        logger.error("error writing grid file", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    writeHeader(newfilename, xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols,
            minvalue, maxvalue, "FLT4BYTES", String.valueOf(noDataValueDefault));
}

From source file:au.org.ala.layers.intersect.Grid.java

public void writeGrid(String newfilename, float[] dfiltered, double xmin, double ymin, double xmax, double ymax,
        double xres, double yres, int nrows, int ncols) {
    int size, i, length = dfiltered.length;
    double maxvalue = Double.MAX_VALUE * -1;
    double minvalue = Double.MAX_VALUE;

    //write data as whole file
    RandomAccessFile afile = null;
    try { //read of random access file can throw an exception
        afile = new RandomAccessFile(newfilename + ".gri", "rw");

        size = 4;/*from   www . j a va 2s .  co m*/
        byte[] b = new byte[size * length];
        ByteBuffer bb = ByteBuffer.wrap(b);

        if (byteorderLSB) {
            bb.order(ByteOrder.LITTLE_ENDIAN);
        } else {
            bb.order(ByteOrder.BIG_ENDIAN);
        }
        for (i = 0; i < length; i++) {
            if (Double.isNaN(dfiltered[i])) {
                bb.putFloat((float) noDataValueDefault);
            } else {
                if (minvalue > dfiltered[i]) {
                    minvalue = dfiltered[i];
                }
                if (maxvalue < dfiltered[i]) {
                    maxvalue = dfiltered[i];
                }
                bb.putFloat((float) dfiltered[i]);
            }
        }

        afile.write(b);
    } catch (Exception e) {
        logger.error("error writing grid file", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    writeHeader(newfilename, xmin, ymin, xmin + xres * ncols, ymin + yres * nrows, xres, yres, nrows, ncols,
            minvalue, maxvalue, "FLT4BYTES", String.valueOf(noDataValueDefault));

}

From source file:com.av.remusic.service.MediaService.java

private void saveQueue(final boolean full) {
    if (!mQueueIsSaveable) {
        return;//from w  ww  . j a v  a  2  s.  c o  m
    }

    final SharedPreferences.Editor editor = mPreferences.edit();
    if (full) {
        mPlaybackStateStore.saveState(mPlaylist, mShuffleMode != SHUFFLE_NONE ? mHistory : null);
        if (mPlaylistInfo.size() > 0) {
            String temp = MainApplication.gsonInstance().toJson(mPlaylistInfo);
            try {
                File file = new File(getCacheDir().getAbsolutePath() + "playlist");
                RandomAccessFile ra = new RandomAccessFile(file, "rws");
                ra.write(temp.getBytes());
                ra.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        editor.putInt("cardid", mCardId);

    }
    editor.putInt("curpos", mPlayPos);
    if (mPlayer.isInitialized()) {
        editor.putLong("seekpos", mPlayer.position());
    }
    editor.putInt("repeatmode", mRepeatMode);
    editor.putInt("shufflemode", mShuffleMode);
    editor.apply();
}

From source file:org.apache.hadoop.hive.service.HSSessionItem.java

public boolean uploadModule(String user, String moduleName, byte[] module) throws HiveServerException {
    boolean res = true;
    String fname = getHome() + "/pl/lib/" + user + "/" + moduleName;
    RandomAccessFile raf;
    File f;//w  ww . j av a  2 s  .  c om

    String dname = getHome() + "/pl/lib/" + user;
    File d = new File(dname);

    if (!d.exists()) {
        if (!d.mkdir()) {
            l4j.error(getSessionName() + " try to mkdir " + dname + " failed.");
            throw new HiveServerException("Create user library failed.");
        }
    }
    File i = new File(getHome() + "/pl/lib/" + user + "/__init__.py");

    if (!i.exists()) {
        try {
            i.createNewFile();
        } catch (java.io.IOException oe) {
        }
    }

    try {
        f = new File(fname);
        if (!f.exists()) {
            if (!f.createNewFile()) {
                l4j.error("Try to create file " + fname + " failed.");
                throw new HiveServerException("Create user package failed.");
            }
        }
    } catch (java.io.IOException ex) {
        l4j.error(getSessionName() + " try to create file " + fname + " failed w/ " + ex);
        return false;
    }
    if (module.length == 0) {
        if (!f.delete()) {
            l4j.error("Try to delete file " + fname + " failed.");
            throw new HiveServerException("Delete user package " + fname + " failed.");
        } else {
            return true;
        }
    }

    try {
        raf = new RandomAccessFile(f, "rw");
    } catch (java.io.FileNotFoundException ex) {
        l4j.error(getSessionName() + " try to open file " + fname + " failed, not found.");
        return false;
    }
    try {
        raf.setLength(0);
        raf.seek(0);
        raf.write(module);
    } catch (java.io.IOException ex) {
        l4j.error(getSessionName() + " try to truncate/write file " + fname + "failed w/ " + ex);
        return false;
    }
    return res;
}

From source file:au.org.ala.layers.intersect.Grid.java

public void mergeMissingValues(Grid sourceOfMissingValues, boolean hideMissing) {
    float[] cells = sourceOfMissingValues.getGrid();

    float[] actual = getGrid();

    int length = actual.length;

    int i;//  w  w w . j  av  a  2  s . c o  m
    RandomAccessFile afile = null;
    File f2 = new File(filename + ".GRI");

    try { //read of random access file can throw an exception
        if (!f2.exists()) {
            afile = new RandomAccessFile(filename + ".gri", "rw");
        } else {
            afile = new RandomAccessFile(filename + ".GRI", "rw");
        }

        byte[] b = new byte[(int) afile.length()];

        ByteBuffer bb = ByteBuffer.wrap(b);

        if (byteorderLSB) {
            bb.order(ByteOrder.LITTLE_ENDIAN);
        }

        afile.seek(0);

        if (datatype.equalsIgnoreCase("UBYTE")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    if (nodatavalue >= 128) {
                        bb.put((byte) (nodatavalue - 256));
                    } else {
                        bb.put((byte) nodatavalue);
                    }
                } else {
                    if (actual[i] >= 128) {
                        bb.put((byte) (actual[i] - 256));
                    } else {
                        bb.put((byte) actual[i]);
                    }
                }
            }
        } else if (datatype.equalsIgnoreCase("BYTE")) {
            for (i = 0; i < length; i++) {
                bb.put((byte) actual[i]);
            }
        } else if (datatype.equalsIgnoreCase("SHORT")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putShort((short) nodatavalue);
                } else {
                    bb.putShort((short) actual[i]);
                }
            }
        } else if (datatype.equalsIgnoreCase("INT")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putInt((int) nodatavalue);
                } else {
                    bb.putInt((int) actual[i]);
                }
            }
        } else if (datatype.equalsIgnoreCase("LONG")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putLong((long) nodatavalue);
                } else {
                    bb.putLong((long) actual[i]);
                }
            }
        } else if (datatype.equalsIgnoreCase("FLOAT")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putFloat((float) nodatavalue);
                } else {
                    bb.putFloat(actual[i]);
                }
            }
        } else if (datatype.equalsIgnoreCase("DOUBLE")) {
            for (i = 0; i < length; i++) {
                if (hideMissing == Float.isNaN(cells[i])) {
                    bb.putDouble((double) nodatavalue);
                } else {
                    bb.putDouble((double) actual[i]);
                }
            }
        } else {
            // should not happen
            logger.error("unsupported grid data type: " + datatype);
        }

        afile.write(bb.array());
    } catch (Exception e) {
        logger.error("error getting grid file values", e);
    } finally {
        if (afile != null) {
            try {
                afile.close();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
    }
}

From source file:org.telegram.ui.PassportActivity.java

private EncryptionResult createSecureDocument(String path) {
    File file = new File(path);
    int length = (int) file.length();
    byte[] b = new byte[length];
    RandomAccessFile f = null;
    try {//w w  w . j a  v  a2 s.c o  m
        f = new RandomAccessFile(path, "rws");
        f.readFully(b);
    } catch (Exception ignore) {

    }
    EncryptionResult result = encryptData(b);
    try {
        f.seek(0);
        f.write(result.encryptedData);
        f.close();
    } catch (Exception ignore) {

    }
    return result;
}