/services/java/com/android/server/RandomBlock.java

https://github.com/aizuzi/platform_frameworks_base · Java · 101 lines · 68 code · 12 blank · 21 comment · 6 complexity · 2f74142cfd210f8cfafc42f541d1bb63 MD5 · raw file

  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. package com.android.server;
  17. import android.util.Slog;
  18. import java.io.Closeable;
  19. import java.io.DataOutput;
  20. import java.io.EOFException;
  21. import java.io.FileInputStream;
  22. import java.io.IOException;
  23. import java.io.InputStream;
  24. import java.io.RandomAccessFile;
  25. /**
  26. * A block of 512 random {@code byte}s.
  27. */
  28. class RandomBlock {
  29. private static final String TAG = "RandomBlock";
  30. private static final boolean DEBUG = false;
  31. private static final int BLOCK_SIZE = 512;
  32. private byte[] block = new byte[BLOCK_SIZE];
  33. private RandomBlock() { }
  34. static RandomBlock fromFile(String filename) throws IOException {
  35. if (DEBUG) Slog.v(TAG, "reading from file " + filename);
  36. InputStream stream = null;
  37. try {
  38. stream = new FileInputStream(filename);
  39. return fromStream(stream);
  40. } finally {
  41. close(stream);
  42. }
  43. }
  44. private static RandomBlock fromStream(InputStream in) throws IOException {
  45. RandomBlock retval = new RandomBlock();
  46. int total = 0;
  47. while(total < BLOCK_SIZE) {
  48. int result = in.read(retval.block, total, BLOCK_SIZE - total);
  49. if (result == -1) {
  50. throw new EOFException();
  51. }
  52. total += result;
  53. }
  54. return retval;
  55. }
  56. void toFile(String filename, boolean sync) throws IOException {
  57. if (DEBUG) Slog.v(TAG, "writing to file " + filename);
  58. RandomAccessFile out = null;
  59. try {
  60. out = new RandomAccessFile(filename, sync ? "rws" : "rw");
  61. toDataOut(out);
  62. truncateIfPossible(out);
  63. } finally {
  64. close(out);
  65. }
  66. }
  67. private static void truncateIfPossible(RandomAccessFile f) {
  68. try {
  69. f.setLength(BLOCK_SIZE);
  70. } catch (IOException e) {
  71. // ignore this exception. Sometimes, the file we're trying to
  72. // write is a character device, such as /dev/urandom, and
  73. // these character devices do not support setting the length.
  74. }
  75. }
  76. private void toDataOut(DataOutput out) throws IOException {
  77. out.write(block);
  78. }
  79. private static void close(Closeable c) {
  80. try {
  81. if (c == null) {
  82. return;
  83. }
  84. c.close();
  85. } catch (IOException e) {
  86. Slog.w(TAG, "IOException thrown while closing Closeable", e);
  87. }
  88. }
  89. }