use of java.io.RandomAccessFile in project hadoop by apache.
the class FsVolumeImpl method loadLastPartialChunkChecksum.
@Override
public byte[] loadLastPartialChunkChecksum(File blockFile, File metaFile) throws IOException {
// readHeader closes the temporary FileInputStream.
DataChecksum dcs;
try (FileInputStream fis = fileIoProvider.getFileInputStream(this, metaFile)) {
dcs = BlockMetadataHeader.readHeader(fis).getChecksum();
}
final int checksumSize = dcs.getChecksumSize();
final long onDiskLen = blockFile.length();
final int bytesPerChecksum = dcs.getBytesPerChecksum();
if (onDiskLen % bytesPerChecksum == 0) {
// because it will not be modified.
return null;
}
long offsetInChecksum = BlockMetadataHeader.getHeaderSize() + (onDiskLen / bytesPerChecksum) * checksumSize;
byte[] lastChecksum = new byte[checksumSize];
try (RandomAccessFile raf = fileIoProvider.getRandomAccessFile(this, metaFile, "r")) {
raf.seek(offsetInChecksum);
int readBytes = raf.read(lastChecksum, 0, checksumSize);
if (readBytes == -1) {
throw new IOException("Expected to read " + checksumSize + " bytes from offset " + offsetInChecksum + " but reached end of file.");
} else if (readBytes != checksumSize) {
throw new IOException("Expected to read " + checksumSize + " bytes from offset " + offsetInChecksum + " but read " + readBytes + " bytes.");
}
}
return lastChecksum;
}
use of java.io.RandomAccessFile in project hadoop by apache.
the class TestFsck method testCorruptBlock.
@Test
public void testCorruptBlock() throws Exception {
conf.setLong(DFSConfigKeys.DFS_BLOCKREPORT_INTERVAL_MSEC_KEY, 1000);
// Set short retry timeouts so this test runs faster
conf.setInt(HdfsClientConfigKeys.Retry.WINDOW_BASE_KEY, 10);
FileSystem fs = null;
DFSClient dfsClient = null;
LocatedBlocks blocks = null;
int replicaCount = 0;
Random random = new Random();
String outStr = null;
short factor = 1;
cluster = new MiniDFSCluster.Builder(conf).numDataNodes(1).build();
cluster.waitActive();
fs = cluster.getFileSystem();
Path file1 = new Path("/testCorruptBlock");
DFSTestUtil.createFile(fs, file1, 1024, factor, 0);
// Wait until file replication has completed
DFSTestUtil.waitReplication(fs, file1, factor);
ExtendedBlock block = DFSTestUtil.getFirstBlock(fs, file1);
// Make sure filesystem is in healthy state
outStr = runFsck(conf, 0, true, "/");
System.out.println(outStr);
assertTrue(outStr.contains(NamenodeFsck.HEALTHY_STATUS));
// corrupt replicas
File blockFile = cluster.getBlockFile(0, block);
if (blockFile != null && blockFile.exists()) {
RandomAccessFile raFile = new RandomAccessFile(blockFile, "rw");
FileChannel channel = raFile.getChannel();
String badString = "BADBAD";
int rand = random.nextInt((int) channel.size() / 2);
raFile.seek(rand);
raFile.write(badString.getBytes());
raFile.close();
}
// Read the file to trigger reportBadBlocks
try {
IOUtils.copyBytes(fs.open(file1), new IOUtils.NullOutputStream(), conf, true);
} catch (IOException ie) {
assertTrue(ie instanceof ChecksumException);
}
dfsClient = new DFSClient(new InetSocketAddress("localhost", cluster.getNameNodePort()), conf);
blocks = dfsClient.getNamenode().getBlockLocations(file1.toString(), 0, Long.MAX_VALUE);
replicaCount = blocks.get(0).getLocations().length;
while (replicaCount != factor) {
try {
Thread.sleep(100);
} catch (InterruptedException ignore) {
}
blocks = dfsClient.getNamenode().getBlockLocations(file1.toString(), 0, Long.MAX_VALUE);
replicaCount = blocks.get(0).getLocations().length;
}
assertTrue(blocks.get(0).isCorrupt());
// Check if fsck reports the same
outStr = runFsck(conf, 1, true, "/");
System.out.println(outStr);
assertTrue(outStr.contains(NamenodeFsck.CORRUPT_STATUS));
assertTrue(outStr.contains("testCorruptBlock"));
}
use of java.io.RandomAccessFile in project jphp by jphp-compiler.
the class FileStream method __construct.
@Override
@Signature({ @Arg("path"), @Arg(value = "mode", optional = @Reflection.Optional("r")) })
public Memory __construct(Environment env, Memory... args) throws IOException {
super.__construct(env, args);
try {
if (getMode().equals("r")) {
accessFile = new RandomAccessFile(getPath(), "r");
position = 0;
} else if (getMode().equals("r+")) {
if (!new File(getPath()).getAbsoluteFile().exists())
throwFileNotFound(env);
accessFile = new RandomAccessFile(getPath(), "rw");
} else if (getMode().equals("w")) {
accessFile = new RandomAccessFile(getPath(), "rw");
accessFile.setLength(0);
canRead = false;
} else if (getMode().equals("w+")) {
accessFile = new RandomAccessFile(getPath(), "rw");
accessFile.setLength(0);
} else if (getMode().equals("a")) {
accessFile = new RandomAccessFile(getPath(), "rw");
File file = new File(getPath());
if (file.getAbsoluteFile().exists()) {
accessFile.seek(file.length());
position = file.length();
}
canRead = false;
} else if (getMode().equals("a+")) {
accessFile = new RandomAccessFile(getPath(), "rw");
File file = new File(getPath());
if (file.getAbsoluteFile().exists()) {
accessFile.seek(file.length());
position = file.length();
}
} else if (getMode().equals("x") || getMode().equals("x+")) {
File file = new File(getPath());
if (file.getAbsoluteFile().exists())
env.exception(WrapIOException.class, "File '%s' already exists (mode: %s)", getMode());
accessFile = new RandomAccessFile(getPath(), "rw");
if (getMode().equals("x"))
canRead = false;
} else if (getMode().equals("c") || getMode().equals("c+")) {
accessFile = new RandomAccessFile(getPath(), "rw");
if (getMode().equals("c"))
canRead = false;
} else
env.exception(WrapIOException.class, "Unsupported mode - '%s'", getMode());
} catch (FileNotFoundException e) {
throwFileNotFound(env);
} catch (IOException e) {
env.exception(WrapIOException.class, e.getMessage());
}
return Memory.NULL;
}
use of java.io.RandomAccessFile in project XobotOS by xamarin.
the class RecoverySystem method verifyPackage.
/**
* Verify the cryptographic signature of a system update package
* before installing it. Note that the package is also verified
* separately by the installer once the device is rebooted into
* the recovery system. This function will return only if the
* package was successfully verified; otherwise it will throw an
* exception.
*
* Verification of a package can take significant time, so this
* function should not be called from a UI thread. Interrupting
* the thread while this function is in progress will result in a
* SecurityException being thrown (and the thread's interrupt flag
* will be cleared).
*
* @param packageFile the package to be verified
* @param listener an object to receive periodic progress
* updates as verification proceeds. May be null.
* @param deviceCertsZipFile the zip file of certificates whose
* public keys we will accept. Verification succeeds if the
* package is signed by the private key corresponding to any
* public key in this file. May be null to use the system default
* file (currently "/system/etc/security/otacerts.zip").
*
* @throws IOException if there were any errors reading the
* package or certs files.
* @throws GeneralSecurityException if verification failed
*/
public static void verifyPackage(File packageFile, ProgressListener listener, File deviceCertsZipFile) throws IOException, GeneralSecurityException {
long fileLen = packageFile.length();
RandomAccessFile raf = new RandomAccessFile(packageFile, "r");
try {
int lastPercent = 0;
long lastPublishTime = System.currentTimeMillis();
if (listener != null) {
listener.onProgress(lastPercent);
}
raf.seek(fileLen - 6);
byte[] footer = new byte[6];
raf.readFully(footer);
if (footer[2] != (byte) 0xff || footer[3] != (byte) 0xff) {
throw new SignatureException("no signature in file (no footer)");
}
int commentSize = (footer[4] & 0xff) | ((footer[5] & 0xff) << 8);
int signatureStart = (footer[0] & 0xff) | ((footer[1] & 0xff) << 8);
Log.v(TAG, String.format("comment size %d; signature start %d", commentSize, signatureStart));
byte[] eocd = new byte[commentSize + 22];
raf.seek(fileLen - (commentSize + 22));
raf.readFully(eocd);
// end-of-central-directory record.
if (eocd[0] != (byte) 0x50 || eocd[1] != (byte) 0x4b || eocd[2] != (byte) 0x05 || eocd[3] != (byte) 0x06) {
throw new SignatureException("no signature in file (bad footer)");
}
for (int i = 4; i < eocd.length - 3; ++i) {
if (eocd[i] == (byte) 0x50 && eocd[i + 1] == (byte) 0x4b && eocd[i + 2] == (byte) 0x05 && eocd[i + 3] == (byte) 0x06) {
throw new SignatureException("EOCD marker found after start of EOCD");
}
}
// The following code is largely copied from
// JarUtils.verifySignature(). We could just *call* that
// method here if that function didn't read the entire
// input (ie, the whole OTA package) into memory just to
// compute its message digest.
BerInputStream bis = new BerInputStream(new ByteArrayInputStream(eocd, commentSize + 22 - signatureStart, signatureStart));
ContentInfo info = (ContentInfo) ContentInfo.ASN1.decode(bis);
SignedData signedData = info.getSignedData();
if (signedData == null) {
throw new IOException("signedData is null");
}
Collection encCerts = signedData.getCertificates();
if (encCerts.isEmpty()) {
throw new IOException("encCerts is empty");
}
// Take the first certificate from the signature (packages
// should contain only one).
Iterator it = encCerts.iterator();
X509Certificate cert = null;
if (it.hasNext()) {
cert = new X509CertImpl((org.apache.harmony.security.x509.Certificate) it.next());
} else {
throw new SignatureException("signature contains no certificates");
}
List sigInfos = signedData.getSignerInfos();
SignerInfo sigInfo;
if (!sigInfos.isEmpty()) {
sigInfo = (SignerInfo) sigInfos.get(0);
} else {
throw new IOException("no signer infos!");
}
// Check that the public key of the certificate contained
// in the package equals one of our trusted public keys.
HashSet<Certificate> trusted = getTrustedCerts(deviceCertsZipFile == null ? DEFAULT_KEYSTORE : deviceCertsZipFile);
PublicKey signatureKey = cert.getPublicKey();
boolean verified = false;
for (Certificate c : trusted) {
if (c.getPublicKey().equals(signatureKey)) {
verified = true;
break;
}
}
if (!verified) {
throw new SignatureException("signature doesn't match any trusted key");
}
// The signature cert matches a trusted key. Now verify that
// the digest in the cert matches the actual file data.
// The verifier in recovery *only* handles SHA1withRSA
// signatures. SignApk.java always uses SHA1withRSA, no
// matter what the cert says to use. Ignore
// cert.getSigAlgName(), and instead use whatever
// algorithm is used by the signature (which should be
// SHA1withRSA).
String da = sigInfo.getDigestAlgorithm();
String dea = sigInfo.getDigestEncryptionAlgorithm();
String alg = null;
if (da == null || dea == null) {
// fall back to the cert algorithm if the sig one
// doesn't look right.
alg = cert.getSigAlgName();
} else {
alg = da + "with" + dea;
}
Signature sig = Signature.getInstance(alg);
sig.initVerify(cert);
// The signature covers all of the OTA package except the
// archive comment and its 2-byte length.
long toRead = fileLen - commentSize - 2;
long soFar = 0;
raf.seek(0);
byte[] buffer = new byte[4096];
boolean interrupted = false;
while (soFar < toRead) {
interrupted = Thread.interrupted();
if (interrupted)
break;
int size = buffer.length;
if (soFar + size > toRead) {
size = (int) (toRead - soFar);
}
int read = raf.read(buffer, 0, size);
sig.update(buffer, 0, read);
soFar += read;
if (listener != null) {
long now = System.currentTimeMillis();
int p = (int) (soFar * 100 / toRead);
if (p > lastPercent && now - lastPublishTime > PUBLISH_PROGRESS_INTERVAL_MS) {
lastPercent = p;
lastPublishTime = now;
listener.onProgress(lastPercent);
}
}
}
if (listener != null) {
listener.onProgress(100);
}
if (interrupted) {
throw new SignatureException("verification was interrupted");
}
if (!sig.verify(sigInfo.getEncryptedDigest())) {
throw new SignatureException("signature digest verification failed");
}
} finally {
raf.close();
}
}
use of java.io.RandomAccessFile in project NoHttp by yanzhenjie.
the class Downloader method download.
public void download(int what, DownloadRequest downloadRequest, DownloadListener downloadListener) {
validateParam(downloadRequest, downloadListener);
Connection connection = null;
RandomAccessFile randomAccessFile = null;
String savePathDir = downloadRequest.getFileDir();
String fileName = downloadRequest.getFileName();
try {
if (TextUtils.isEmpty(savePathDir))
savePathDir = NoHttp.getContext().getFilesDir().getAbsolutePath();
validateDevice(savePathDir);
if (// auto named.
TextUtils.isEmpty(fileName))
fileName = Long.toString(System.currentTimeMillis());
File tempFile = new File(savePathDir, fileName + ".nohttp");
// 断点开始处。
long rangeSize = handleRange(tempFile, downloadRequest);
// 连接服务器。
connection = mHttpConnection.getConnection(downloadRequest);
Exception exception = connection.exception();
if (exception != null)
throw exception;
Logger.i("----------Response Start----------");
Headers responseHeaders = connection.responseHeaders();
int responseCode = responseHeaders.getResponseCode();
// getList filename from server.
if (downloadRequest.autoNameByHead()) {
String contentDisposition = responseHeaders.getContentDisposition();
if (!TextUtils.isEmpty(contentDisposition)) {
fileName = HeaderUtil.parseHeadValue(contentDisposition, "filename", null);
if (!TextUtils.isEmpty(fileName)) {
try {
fileName = URLDecoder.decode(fileName, downloadRequest.getParamsEncoding());
} catch (UnsupportedEncodingException e) {
// Do nothing.
}
if (fileName.startsWith("\"") && fileName.endsWith("\"")) {
fileName = fileName.substring(1, fileName.length() - 1);
}
}
}
// From url.
if (TextUtils.isEmpty(fileName)) {
String tempUrl = downloadRequest.url();
String[] slash = tempUrl.split("/");
fileName = slash[slash.length - 1];
int paramIndex = fileName.indexOf("?");
if (paramIndex > 0) {
fileName = fileName.substring(0, paramIndex);
}
}
}
InputStream serverStream = connection.serverStream();
if (responseCode >= 400) {
ServerError error = new ServerError("Download failed, the server response code is " + responseCode + ": " + downloadRequest.url());
error.setErrorBody(IOUtils.toString(serverStream));
throw error;
} else {
long contentLength;
// 文件总大小
if (responseCode == 206) {
// Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]。
// Sample:Accept-Range:bytes 1024-2047/2048。
String range = responseHeaders.getContentRange();
try {
// 截取'/'之后的总大小。
contentLength = Long.parseLong(range.substring(range.indexOf('/') + 1));
} catch (Throwable e) {
throw new ServerError("ResponseCode is 206, but content-Range error in Server HTTP header " + "information: " + range + ".");
}
} else if (responseCode == 304) {
int httpContentLength = responseHeaders.getContentLength();
downloadListener.onStart(what, true, httpContentLength, responseHeaders, httpContentLength);
downloadListener.onProgress(what, 100, httpContentLength, 0);
Logger.d("-------Download finish-------");
downloadListener.onFinish(what, savePathDir + File.separator + fileName);
return;
} else {
// such as: 200.
// 服务器不支持Range。
rangeSize = 0L;
// 直接下载。
contentLength = responseHeaders.getContentLength();
}
// 验证文件已经存在。
File lastFile = new File(savePathDir, fileName);
if (lastFile.exists()) {
if (downloadRequest.isDeleteOld())
IOUtils.delFileOrFolder(lastFile);
else {
downloadListener.onStart(what, true, lastFile.length(), responseHeaders, lastFile.length());
downloadListener.onProgress(what, 100, lastFile.length(), 0);
Logger.d("-------Download finish-------");
downloadListener.onFinish(what, lastFile.getAbsolutePath());
return;
}
}
if (IOUtils.getDirSize(savePathDir) < contentLength)
throw new StorageSpaceNotEnoughError("The folder is not enough space to save the downloaded file:" + " " + savePathDir + ".");
// 需要重新下载,生成临时文件。
if (responseCode != 206 && !IOUtils.createNewFile(tempFile))
throw new StorageReadWriteError("SD isn't available, please check SD card and permission: " + "WRITE_EXTERNAL_STORAGE, and you must pay attention to Android6.0 RunTime " + "Permissions: https://github.com/yanzhenjie/AndPermission.");
if (downloadRequest.isCanceled()) {
Log.w("NoHttpDownloader", "Download request is canceled.");
downloadListener.onCancel(what);
return;
}
// 通知开始下载了。
Logger.d("-------Download start-------");
downloadListener.onStart(what, rangeSize > 0, rangeSize, responseHeaders, contentLength);
randomAccessFile = new RandomAccessFile(tempFile, "rws");
randomAccessFile.seek(rangeSize);
byte[] buffer = new byte[8096];
int len;
// 旧的进度记录,防止重复通知。
int oldProgress = 0;
// 追加目前已经下载的进度。
long count = rangeSize;
long startTime = System.currentTimeMillis();
long speedCount = 0;
long oldSpeed = 0;
while (((len = serverStream.read(buffer)) != -1)) {
if (downloadRequest.isCanceled()) {
Log.i("NoHttpDownloader", "Download request is canceled.");
downloadListener.onCancel(what);
break;
} else {
randomAccessFile.write(buffer, 0, len);
count += len;
speedCount += len;
long time = System.currentTimeMillis() - startTime;
long speed = speedCount * 1000 / time;
boolean speedChanged = oldSpeed != speed && time >= 300;
Logger.i("speedCount: " + speedCount + "; time: " + time + "; speed: " + speed + "; changed: " + "" + speedChanged);
if (contentLength != 0) {
int progress = (int) (count * 100 / contentLength);
if (progress != oldProgress && speedChanged) {
downloadListener.onProgress(what, progress, count, speed);
speedCount = 0;
oldSpeed = speed;
startTime = System.currentTimeMillis();
} else if (speedChanged) {
downloadListener.onProgress(what, oldProgress, count, speed);
speedCount = 0;
oldSpeed = speed;
startTime = System.currentTimeMillis();
} else if (progress != oldProgress) {
downloadListener.onProgress(what, progress, count, oldSpeed);
}
oldProgress = progress;
} else if (speedChanged) {
downloadListener.onProgress(what, 0, count, speed);
speedCount = 0;
oldSpeed = speed;
startTime = System.currentTimeMillis();
} else {
downloadListener.onProgress(what, 0, count, oldSpeed);
}
}
}
if (!downloadRequest.isCanceled()) {
//noinspection ResultOfMethodCallIgnored
tempFile.renameTo(lastFile);
Logger.d("-------Download finish-------");
downloadListener.onFinish(what, lastFile.getAbsolutePath());
}
}
} catch (MalformedURLException e) {
Logger.e(e);
downloadListener.onDownloadError(what, new URLError(e.getMessage()));
} catch (UnknownHostException e) {
Logger.e(e);
downloadListener.onDownloadError(what, new UnKnownHostError(e.getMessage()));
} catch (SocketTimeoutException e) {
Logger.e(e);
downloadListener.onDownloadError(what, new TimeoutError(e.getMessage()));
} catch (IOException e) {
Exception newException = e;
if (!IOUtils.canWrite(savePathDir))
newException = new StorageReadWriteError("This folder cannot be written to the file: " + savePathDir + ".");
else if (IOUtils.getDirSize(savePathDir) < 1024)
newException = new StorageSpaceNotEnoughError("The folder is not enough space to save the downloaded " + "file: " + savePathDir + ".");
Logger.e(newException);
downloadListener.onDownloadError(what, newException);
} catch (Exception e) {
// NetworkError | ServerError | StorageCantWriteError | StorageSpaceNotEnoughError
if (!NetUtil.isNetworkAvailable())
e = new NetworkError("The network is not available.");
Logger.e(e);
downloadListener.onDownloadError(what, e);
} finally {
Logger.i("----------Response End----------");
IOUtils.closeQuietly(randomAccessFile);
IOUtils.closeQuietly(connection);
}
}
Aggregations