Search in sources :

Example 91 with RandomAccessFile

use of java.io.RandomAccessFile in project intellij-community by JetBrains.

the class PatchApplyingRevertingTest method testRevertedWhenFileToDeleteIsProcessLocked.

@Test
public void testRevertedWhenFileToDeleteIsProcessLocked() throws Exception {
    assumeTrue(UtilsTest.IS_WINDOWS);
    Patch patch = PatchFileCreator.create(myPatchSpec, myFile, TEST_UI);
    try (RandomAccessFile raf = new RandomAccessFile(new File(myOlderDir, "bin/idea.bat"), "rw")) {
        // Lock the file. FileLock is not good here, because we need to prevent deletion.
        int b = raf.read();
        raf.seek(0);
        raf.write(b);
        PatchFileCreator.PreparationResult preparationResult = PatchFileCreator.prepareAndValidate(myFile, myOlderDir, TEST_UI);
        Map<String, Long> original = patch.digestFiles(myOlderDir, Collections.emptyList(), false, TEST_UI);
        File backup = getTempFile("backup");
        PatchFileCreator.apply(preparationResult, new HashMap<>(), backup, TEST_UI);
        assertEquals(original, patch.digestFiles(myOlderDir, Collections.emptyList(), false, TEST_UI));
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 92 with RandomAccessFile

use of java.io.RandomAccessFile in project intellij-community by JetBrains.

the class PatchApplyingRevertingTest method testApplyingWithModifiedCriticalFilesAndDifferentRoot.

@Test
public void testApplyingWithModifiedCriticalFilesAndDifferentRoot() throws Exception {
    myPatchSpec.setStrict(true);
    myPatchSpec.setRoot("lib/");
    myPatchSpec.setCriticalFiles(Collections.singletonList("lib/annotations.jar"));
    Patch patch = PatchFileCreator.create(myPatchSpec, myFile, TEST_UI);
    try (RandomAccessFile raf = new RandomAccessFile(new File(myOlderDir, "lib/annotations.jar"), "rw")) {
        raf.seek(20);
        raf.write(42);
    }
    File toDir = new File(myOlderDir, "lib/");
    assertAppliedAndRevertedCorrectly(patch, PatchFileCreator.prepareAndValidate(myFile, toDir, TEST_UI));
}
Also used : RandomAccessFile(java.io.RandomAccessFile) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Example 93 with RandomAccessFile

use of java.io.RandomAccessFile in project async-http-client by AsyncHttpClient.

the class ResumableRandomAccessFileListenerTest method testOnBytesReceivedBufferHasArray.

@Test
public void testOnBytesReceivedBufferHasArray() throws IOException {
    RandomAccessFile file = PowerMockito.mock(RandomAccessFile.class);
    ResumableRandomAccessFileListener listener = new ResumableRandomAccessFileListener(file);
    byte[] array = new byte[] { 1, 2, 23, 33 };
    ByteBuffer buf = ByteBuffer.wrap(array);
    listener.onBytesReceived(buf);
    verify(file).write(array, 0, 4);
}
Also used : RandomAccessFile(java.io.RandomAccessFile) ByteBuffer(java.nio.ByteBuffer) Test(org.testng.annotations.Test)

Example 94 with RandomAccessFile

use of java.io.RandomAccessFile in project android-bootstrap by AndroidBootstrap.

the class ImageUtils method getBitmap.

/**
     * Get a bitmap from the image path
     *
     * @param imagePath
     * @param sampleSize
     * @return bitmap or null if read fails
     */
public static Bitmap getBitmap(final String imagePath, final int sampleSize) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inDither = false;
    options.inSampleSize = sampleSize;
    RandomAccessFile file = null;
    try {
        file = new RandomAccessFile(imagePath, "r");
        return BitmapFactory.decodeFileDescriptor(file.getFD(), null, options);
    } catch (IOException e) {
        Timber.d(e, "Could not get cached bitmap.");
        return null;
    } finally {
        if (file != null)
            try {
                file.close();
            } catch (IOException e) {
                Timber.d(e, "Could not get cached bitmap.");
            }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) BitmapFactory(android.graphics.BitmapFactory) IOException(java.io.IOException)

Example 95 with RandomAccessFile

use of java.io.RandomAccessFile in project android-bootstrap by AndroidBootstrap.

the class ImageUtils method getSize.

/**
     * Get size of image
     *
     * @param imagePath
     * @return size
     */
public static Point getSize(final String imagePath) {
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    RandomAccessFile file = null;
    try {
        file = new RandomAccessFile(imagePath, "r");
        BitmapFactory.decodeFileDescriptor(file.getFD(), null, options);
        return new Point(options.outWidth, options.outHeight);
    } catch (final IOException e) {
        Timber.d(e, "Could not get size.");
        return null;
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (final IOException e) {
                Timber.d(e, "Could not get size.");
            }
        }
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) BitmapFactory(android.graphics.BitmapFactory) Point(android.graphics.Point) IOException(java.io.IOException)

Aggregations

RandomAccessFile (java.io.RandomAccessFile)1607 IOException (java.io.IOException)761 File (java.io.File)691 FileChannel (java.nio.channels.FileChannel)284 ByteBuffer (java.nio.ByteBuffer)174 FileNotFoundException (java.io.FileNotFoundException)167 Test (org.junit.Test)152 FileOutputStream (java.io.FileOutputStream)91 FileLock (java.nio.channels.FileLock)91 MappedByteBuffer (java.nio.MappedByteBuffer)83 InputStream (java.io.InputStream)68 FileInputStream (java.io.FileInputStream)63 EOFException (java.io.EOFException)55 ArrayList (java.util.ArrayList)45 ByteArrayOutputStream (java.io.ByteArrayOutputStream)39 BufferedInputStream (java.io.BufferedInputStream)35 ByteArrayInputStream (java.io.ByteArrayInputStream)35 Random (java.util.Random)33 HashMap (java.util.HashMap)24 BinaryMapIndexReader (net.osmand.binary.BinaryMapIndexReader)23