blob: 4ac1c6e21b3fae401340f6686f55946b25a404ae [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
19import android.util.Log;
20
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/**
30 * A 4k block of random {@code byte}s.
31 */
32class RandomBlock {
33
34 private static final String TAG = "RandomBlock";
35 private static final int BLOCK_SIZE = 4096;
36 private byte[] block = new byte[BLOCK_SIZE];
37
38 private RandomBlock() { }
39
40 static RandomBlock fromFile(String filename) throws IOException {
41 Log.v(TAG, "reading from file " + filename);
42 InputStream stream = null;
43 try {
44 stream = new FileInputStream(filename);
45 return fromStream(stream);
46 } finally {
47 close(stream);
48 }
49 }
50
51 private static RandomBlock fromStream(InputStream in) throws IOException {
52 RandomBlock retval = new RandomBlock();
53 int total = 0;
54 while(total < BLOCK_SIZE) {
55 int result = in.read(retval.block, total, BLOCK_SIZE - total);
56 if (result == -1) {
57 throw new EOFException();
58 }
59 total += result;
60 }
61 return retval;
62 }
63
64 void toFile(String filename) throws IOException {
65 Log.v(TAG, "writing to file " + filename);
Nick Kralevich28542542009-06-18 15:23:17 -070066 RandomAccessFile out = null;
Nick Kralevich4fb25612009-06-17 16:03:22 -070067 try {
Nick Kralevich28542542009-06-18 15:23:17 -070068 out = new RandomAccessFile(filename, "rws");
69 toDataOut(out);
70 truncateIfPossible(out);
Nick Kralevich4fb25612009-06-17 16:03:22 -070071 } finally {
72 close(out);
73 }
74 }
75
Nick Kralevich28542542009-06-18 15:23:17 -070076 private static void truncateIfPossible(RandomAccessFile f) {
77 try {
78 f.setLength(BLOCK_SIZE);
79 } catch (IOException e) {
80 // ignore this exception. Sometimes, the file we're trying to
81 // write is a character device, such as /dev/urandom, and
82 // these character devices do not support setting the length.
83 }
84 }
85
86 private void toDataOut(DataOutput out) throws IOException {
Nick Kralevich4fb25612009-06-17 16:03:22 -070087 out.write(block);
88 }
89
90 private static void close(Closeable c) {
91 try {
92 if (c == null) {
93 return;
94 }
95 c.close();
96 } catch (IOException e) {
97 Log.w(TAG, "IOException thrown while closing Closeable", e);
98 }
99 }
100}