blob: 6d6d9010a8d98c11eeeb312e96fa2cb1a2293804 [file] [log] [blame]
Nick Kralevich4fb25612009-06-17 16:03:22 -07001/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server;
18
Joe Onorato8a9b2202010-02-26 18:56:32 -080019import android.util.Slog;
Nick Kralevich4fb25612009-06-17 16:03:22 -070020
21import java.io.Closeable;
Nick Kralevich28542542009-06-18 15:23:17 -070022import java.io.DataOutput;
Nick Kralevich4fb25612009-06-17 16:03:22 -070023import java.io.EOFException;
24import java.io.FileInputStream;
Nick Kralevich4fb25612009-06-17 16:03:22 -070025import java.io.IOException;
26import java.io.InputStream;
Nick Kralevich28542542009-06-18 15:23:17 -070027import java.io.RandomAccessFile;
Nick Kralevich4fb25612009-06-17 16:03:22 -070028
29/**
Alex Klyubin3c2ae2f2013-10-03 13:56:09 -070030 * A block of 512 random {@code byte}s.
Nick Kralevich4fb25612009-06-17 16:03:22 -070031 */
32class RandomBlock {
33
34 private static final String TAG = "RandomBlock";
Dianne Hackbornbd0a81f2009-10-04 13:30:50 -070035 private static final boolean DEBUG = false;
Alex Klyubin3c2ae2f2013-10-03 13:56:09 -070036 private static final int BLOCK_SIZE = 512;
Nick Kralevich4fb25612009-06-17 16:03:22 -070037 private byte[] block = new byte[BLOCK_SIZE];
38
39 private RandomBlock() { }
40
41 static RandomBlock fromFile(String filename) throws IOException {
Joe Onorato8a9b2202010-02-26 18:56:32 -080042 if (DEBUG) Slog.v(TAG, "reading from file " + filename);
Nick Kralevich4fb25612009-06-17 16:03:22 -070043 InputStream stream = null;
44 try {
45 stream = new FileInputStream(filename);
46 return fromStream(stream);
47 } finally {
48 close(stream);
49 }
50 }
51
52 private static RandomBlock fromStream(InputStream in) throws IOException {
53 RandomBlock retval = new RandomBlock();
54 int total = 0;
55 while(total < BLOCK_SIZE) {
56 int result = in.read(retval.block, total, BLOCK_SIZE - total);
57 if (result == -1) {
58 throw new EOFException();
59 }
60 total += result;
61 }
62 return retval;
63 }
64
Elliott Hughes69078912011-04-04 12:15:34 -070065 void toFile(String filename, boolean sync) throws IOException {
Joe Onorato8a9b2202010-02-26 18:56:32 -080066 if (DEBUG) Slog.v(TAG, "writing to file " + filename);
Nick Kralevich28542542009-06-18 15:23:17 -070067 RandomAccessFile out = null;
Nick Kralevich4fb25612009-06-17 16:03:22 -070068 try {
Elliott Hughes69078912011-04-04 12:15:34 -070069 out = new RandomAccessFile(filename, sync ? "rws" : "rw");
Nick Kralevich28542542009-06-18 15:23:17 -070070 toDataOut(out);
71 truncateIfPossible(out);
Nick Kralevich4fb25612009-06-17 16:03:22 -070072 } finally {
73 close(out);
74 }
75 }
76
Nick Kralevich28542542009-06-18 15:23:17 -070077 private static void truncateIfPossible(RandomAccessFile f) {
78 try {
79 f.setLength(BLOCK_SIZE);
80 } catch (IOException e) {
81 // ignore this exception. Sometimes, the file we're trying to
82 // write is a character device, such as /dev/urandom, and
83 // these character devices do not support setting the length.
84 }
85 }
86
87 private void toDataOut(DataOutput out) throws IOException {
Nick Kralevich4fb25612009-06-17 16:03:22 -070088 out.write(block);
89 }
90
91 private static void close(Closeable c) {
92 try {
93 if (c == null) {
94 return;
95 }
96 c.close();
97 } catch (IOException e) {
Joe Onorato8a9b2202010-02-26 18:56:32 -080098 Slog.w(TAG, "IOException thrown while closing Closeable", e);
Nick Kralevich4fb25612009-06-17 16:03:22 -070099 }
100 }
101}