Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 19 | import android.util.Slog; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 20 | |
| 21 | import java.io.Closeable; |
Nick Kralevich | 2854254 | 2009-06-18 15:23:17 -0700 | [diff] [blame] | 22 | import java.io.DataOutput; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 23 | import java.io.EOFException; |
| 24 | import java.io.FileInputStream; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 25 | import java.io.IOException; |
| 26 | import java.io.InputStream; |
Nick Kralevich | 2854254 | 2009-06-18 15:23:17 -0700 | [diff] [blame] | 27 | import java.io.RandomAccessFile; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 28 | |
| 29 | /** |
Alex Klyubin | 3c2ae2f | 2013-10-03 13:56:09 -0700 | [diff] [blame] | 30 | * A block of 512 random {@code byte}s. |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 31 | */ |
| 32 | class RandomBlock { |
| 33 | |
| 34 | private static final String TAG = "RandomBlock"; |
Dianne Hackborn | bd0a81f | 2009-10-04 13:30:50 -0700 | [diff] [blame] | 35 | private static final boolean DEBUG = false; |
Alex Klyubin | 3c2ae2f | 2013-10-03 13:56:09 -0700 | [diff] [blame] | 36 | private static final int BLOCK_SIZE = 512; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 37 | private byte[] block = new byte[BLOCK_SIZE]; |
| 38 | |
| 39 | private RandomBlock() { } |
| 40 | |
| 41 | static RandomBlock fromFile(String filename) throws IOException { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 42 | if (DEBUG) Slog.v(TAG, "reading from file " + filename); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 43 | 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 Hughes | 6907891 | 2011-04-04 12:15:34 -0700 | [diff] [blame] | 65 | void toFile(String filename, boolean sync) throws IOException { |
Joe Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 66 | if (DEBUG) Slog.v(TAG, "writing to file " + filename); |
Nick Kralevich | 2854254 | 2009-06-18 15:23:17 -0700 | [diff] [blame] | 67 | RandomAccessFile out = null; |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 68 | try { |
Elliott Hughes | 6907891 | 2011-04-04 12:15:34 -0700 | [diff] [blame] | 69 | out = new RandomAccessFile(filename, sync ? "rws" : "rw"); |
Nick Kralevich | 2854254 | 2009-06-18 15:23:17 -0700 | [diff] [blame] | 70 | toDataOut(out); |
| 71 | truncateIfPossible(out); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 72 | } finally { |
| 73 | close(out); |
| 74 | } |
| 75 | } |
| 76 | |
Nick Kralevich | 2854254 | 2009-06-18 15:23:17 -0700 | [diff] [blame] | 77 | 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 Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 88 | 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 Onorato | 8a9b220 | 2010-02-26 18:56:32 -0800 | [diff] [blame] | 98 | Slog.w(TAG, "IOException thrown while closing Closeable", e); |
Nick Kralevich | 4fb2561 | 2009-06-17 16:03:22 -0700 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | } |