use of java.io.EOFException in project bazel by bazelbuild.
the class FileUtils method readSegment.
/**
* Reads a portion of a file to memory.
*
* @param file the file to read data from
* @param start the offset in the file to start reading
* @param length the number of bytes to read
* @return the bytes read
* @throws Exception failed to read the file
*/
@NonNull
public static byte[] readSegment(@NonNull File file, long start, int length) throws Exception {
Preconditions.checkArgument(start >= 0, "start < 0");
Preconditions.checkArgument(length >= 0, "length < 0");
byte[] data;
boolean threw = true;
RandomAccessFile raf = new RandomAccessFile(file, "r");
try {
raf.seek(start);
data = new byte[length];
int tot = 0;
while (tot < length) {
int r = raf.read(data, tot, length - tot);
if (r < 0) {
throw new EOFException();
}
tot += r;
}
threw = false;
} finally {
Closeables.close(raf, threw);
}
return data;
}
use of java.io.EOFException in project platform_frameworks_base by android.
the class OMAConstants method deserializeString.
public static String deserializeString(InputStream in) throws IOException {
StringBuilder prefix = new StringBuilder();
for (; ; ) {
byte b = (byte) in.read();
if (b == '.')
return null;
else if (b == ':')
break;
else if (b > ' ')
prefix.append((char) b);
}
int length = Integer.parseInt(prefix.toString(), 16);
byte[] octets = new byte[length];
int offset = 0;
while (offset < octets.length) {
int amount = in.read(octets, offset, octets.length - offset);
if (amount <= 0)
throw new EOFException();
offset += amount;
}
return new String(octets, StandardCharsets.UTF_8);
}
use of java.io.EOFException in project platform_frameworks_base by android.
the class PackageManagerBackupAgent method parseStateFile.
// Util: parse out an existing state file into a usable structure
private void parseStateFile(ParcelFileDescriptor stateFile) {
mExisting.clear();
mStateVersions.clear();
mStoredSdkVersion = 0;
mStoredIncrementalVersion = null;
mStoredHomeComponent = null;
mStoredHomeVersion = 0;
mStoredHomeSigHashes = null;
// The state file is just the list of app names we have stored signatures for
// with the exception of the metadata block, to which is also appended the
// version numbers corresponding with the last time we wrote this PM block.
// If they mismatch the current system, we'll re-store the metadata key.
FileInputStream instream = new FileInputStream(stateFile.getFileDescriptor());
BufferedInputStream inbuffer = new BufferedInputStream(instream);
DataInputStream in = new DataInputStream(inbuffer);
try {
boolean ignoreExisting = false;
String pkg = in.readUTF();
// Validate the state file version is sensical to us
if (pkg.equals(STATE_FILE_HEADER)) {
int stateVersion = in.readInt();
if (stateVersion > STATE_FILE_VERSION) {
Slog.w(TAG, "Unsupported state file version " + stateVersion + ", redoing from start");
return;
}
pkg = in.readUTF();
} else {
// This is an older version of the state file in which the lead element
// is not a STATE_FILE_VERSION string. If that's the case, we want to
// make sure to write our full backup dataset when given an opportunity.
// We trigger that by simply not marking the restored package metadata
// as known-to-exist-in-archive.
Slog.i(TAG, "Older version of saved state - rewriting");
ignoreExisting = true;
}
// First comes the preferred home app data, if any, headed by the DEFAULT_HOME_KEY tag
if (pkg.equals(DEFAULT_HOME_KEY)) {
// flattened component name, version, signature of the home app
mStoredHomeComponent = ComponentName.unflattenFromString(in.readUTF());
mStoredHomeVersion = in.readLong();
mStoredHomeSigHashes = readSignatureHashArray(in);
// set up for the next block of state
pkg = in.readUTF();
} else {
// else no preferred home app on the ancestral device - fall through to the rest
}
// After (possible) home app data comes the global metadata block
if (pkg.equals(GLOBAL_METADATA_KEY)) {
mStoredSdkVersion = in.readInt();
mStoredIncrementalVersion = in.readUTF();
if (!ignoreExisting) {
mExisting.add(GLOBAL_METADATA_KEY);
}
} else {
Slog.e(TAG, "No global metadata in state file!");
return;
}
// The global metadata was last; now read all the apps
while (true) {
pkg = in.readUTF();
int versionCode = in.readInt();
if (!ignoreExisting) {
mExisting.add(pkg);
}
mStateVersions.put(pkg, new Metadata(versionCode, null));
}
} catch (EOFException eof) {
// safe; we're done
} catch (IOException e) {
// whoops, bad state file. abort.
Slog.e(TAG, "Unable to read Package Manager state file: " + e);
}
}
use of java.io.EOFException in project platform_frameworks_base by android.
the class BlobBackupHelper method readOldState.
// Internal implementation
/*
* State on-disk format:
* [Int] : overall blob version number
* [Int=N] : number of keys represented in the state blob
* N* :
* [String] key
* [Long] blob checksum, calculated after compression
*/
@SuppressWarnings("resource")
private ArrayMap<String, Long> readOldState(ParcelFileDescriptor oldStateFd) {
final ArrayMap<String, Long> state = new ArrayMap<String, Long>();
FileInputStream fis = new FileInputStream(oldStateFd.getFileDescriptor());
DataInputStream in = new DataInputStream(fis);
try {
int version = in.readInt();
if (version <= mCurrentBlobVersion) {
final int numKeys = in.readInt();
if (DEBUG) {
Log.i(TAG, " " + numKeys + " keys in state record");
}
for (int i = 0; i < numKeys; i++) {
String key = in.readUTF();
long checksum = in.readLong();
if (DEBUG) {
Log.i(TAG, " key '" + key + "' checksum is " + checksum);
}
state.put(key, checksum);
}
} else {
Log.w(TAG, "Prior state from unrecognized version " + version);
}
} catch (EOFException e) {
// is truncated we just treat it the same way.
if (DEBUG) {
Log.i(TAG, "Hit EOF reading prior state");
}
state.clear();
} catch (Exception e) {
Log.e(TAG, "Error examining prior backup state " + e.getMessage());
state.clear();
}
return state;
}
use of java.io.EOFException in project platform_frameworks_base by android.
the class BackupManagerService method initPackageTracking.
private void initPackageTracking() {
if (MORE_DEBUG)
Slog.v(TAG, "` tracking");
// Remember our ancestral dataset
mTokenFile = new File(mBaseStateDir, "ancestral");
try {
RandomAccessFile tf = new RandomAccessFile(mTokenFile, "r");
int version = tf.readInt();
if (version == CURRENT_ANCESTRAL_RECORD_VERSION) {
mAncestralToken = tf.readLong();
mCurrentToken = tf.readLong();
int numPackages = tf.readInt();
if (numPackages >= 0) {
mAncestralPackages = new HashSet<String>();
for (int i = 0; i < numPackages; i++) {
String pkgName = tf.readUTF();
mAncestralPackages.add(pkgName);
}
}
}
tf.close();
} catch (FileNotFoundException fnf) {
// Probably innocuous
Slog.v(TAG, "No ancestral data");
} catch (IOException e) {
Slog.w(TAG, "Unable to read token file", e);
}
// Keep a log of what apps we've ever backed up. Because we might have
// rebooted in the middle of an operation that was removing something from
// this log, we sanity-check its contents here and reconstruct it.
mEverStored = new File(mBaseStateDir, "processed");
File tempProcessedFile = new File(mBaseStateDir, "processed.new");
// Ignore it -- we'll validate "processed" against the current package set.
if (tempProcessedFile.exists()) {
tempProcessedFile.delete();
}
// file to continue the recordkeeping.
if (mEverStored.exists()) {
RandomAccessFile temp = null;
RandomAccessFile in = null;
try {
temp = new RandomAccessFile(tempProcessedFile, "rws");
in = new RandomAccessFile(mEverStored, "r");
// Loop until we hit EOF
while (true) {
String pkg = in.readUTF();
try {
// is this package still present?
mPackageManager.getPackageInfo(pkg, 0);
// if we get here then yes it is; remember it
mEverStoredApps.add(pkg);
temp.writeUTF(pkg);
if (MORE_DEBUG)
Slog.v(TAG, " + " + pkg);
} catch (NameNotFoundException e) {
// nope, this package was uninstalled; don't include it
if (MORE_DEBUG)
Slog.v(TAG, " - " + pkg);
}
}
} catch (EOFException e) {
// old one with the new one then reopen the file for continuing use.
if (!tempProcessedFile.renameTo(mEverStored)) {
Slog.e(TAG, "Error renaming " + tempProcessedFile + " to " + mEverStored);
}
} catch (IOException e) {
Slog.e(TAG, "Error in processed file", e);
} finally {
try {
if (temp != null)
temp.close();
} catch (IOException e) {
}
try {
if (in != null)
in.close();
} catch (IOException e) {
}
}
}
synchronized (mQueueLock) {
// Resume the full-data backup queue
mFullBackupQueue = readFullBackupSchedule();
}
// Register for broadcasts about package install, etc., so we can
// update the provider list.
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
filter.addDataScheme("package");
mContext.registerReceiver(mBroadcastReceiver, filter);
// Register for events related to sdcard installation.
IntentFilter sdFilter = new IntentFilter();
sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_AVAILABLE);
sdFilter.addAction(Intent.ACTION_EXTERNAL_APPLICATIONS_UNAVAILABLE);
mContext.registerReceiver(mBroadcastReceiver, sdFilter);
}
Aggregations