FileDocCategorySizeDatePackage
RandomBlock.javaAPI DocAndroid 5.1 API3073Thu Mar 12 22:22:42 GMT 2015com.android.server

RandomBlock

public class RandomBlock extends Object
A block of 512 random {@code byte}s.

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final int
BLOCK_SIZE
private byte[]
block
Constructors Summary
private RandomBlock()


       
Methods Summary
private static voidclose(java.io.Closeable c)

        try {
            if (c == null) {
                return;
            }
            c.close();
        } catch (IOException e) {
            Slog.w(TAG, "IOException thrown while closing Closeable", e);
        }
    
static com.android.server.RandomBlockfromFile(java.lang.String filename)

        if (DEBUG) Slog.v(TAG, "reading from file " + filename);
        InputStream stream = null;
        try {
            stream = new FileInputStream(filename);
            return fromStream(stream);
        } finally {
            close(stream);
        }
    
private static com.android.server.RandomBlockfromStream(java.io.InputStream in)

        RandomBlock retval = new RandomBlock();
        int total = 0;
        while(total < BLOCK_SIZE) {
            int result = in.read(retval.block, total, BLOCK_SIZE - total);
            if (result == -1) {
                throw new EOFException();
            }
            total += result;
        }
        return retval;
    
private voidtoDataOut(java.io.DataOutput out)

        out.write(block);
    
voidtoFile(java.lang.String filename, boolean sync)

        if (DEBUG) Slog.v(TAG, "writing to file " + filename);
        RandomAccessFile out = null;
        try {
            out = new RandomAccessFile(filename, sync ? "rws" : "rw");
            toDataOut(out);
            truncateIfPossible(out);
        } finally {
            close(out);
        }
    
private static voidtruncateIfPossible(java.io.RandomAccessFile f)

        try {
            f.setLength(BLOCK_SIZE);
        } catch (IOException e) {
            // ignore this exception.  Sometimes, the file we're trying to
            // write is a character device, such as /dev/urandom, and
            // these character devices do not support setting the length.
        }