Search in sources :

Example 51 with FileOutputStream

use of java.io.FileOutputStream in project VirtualApp by asLody.

the class VUserManagerService method writeUserListLocked.

/*
     * Writes the user list file in this format:
     *
     * <users nextSerialNumber="3">
     *   <user id="0"></user>
     *   <user id="2"></user>
     * </users>
     */
private void writeUserListLocked() {
    FileOutputStream fos = null;
    AtomicFile userListFile = new AtomicFile(mUserListFile);
    try {
        fos = userListFile.startWrite();
        final BufferedOutputStream bos = new BufferedOutputStream(fos);
        // XmlSerializer serializer = XmlUtils.serializerInstance();
        final XmlSerializer serializer = new FastXmlSerializer();
        serializer.setOutput(bos, "utf-8");
        serializer.startDocument(null, true);
        serializer.setFeature("http://xmlpull.org/v1/doc/features.html#indent-output", true);
        serializer.startTag(null, TAG_USERS);
        serializer.attribute(null, ATTR_NEXT_SERIAL_NO, Integer.toString(mNextSerialNumber));
        serializer.attribute(null, ATTR_USER_VERSION, Integer.toString(mUserVersion));
        for (int i = 0; i < mUsers.size(); i++) {
            VUserInfo user = mUsers.valueAt(i);
            serializer.startTag(null, TAG_USER);
            serializer.attribute(null, ATTR_ID, Integer.toString(user.id));
            serializer.endTag(null, TAG_USER);
        }
        serializer.endTag(null, TAG_USERS);
        serializer.endDocument();
        userListFile.finishWrite(fos);
    } catch (Exception e) {
        userListFile.failWrite(fos);
        VLog.e(LOG_TAG, "Error writing user list");
    }
}
Also used : FastXmlSerializer(com.lody.virtual.helper.utils.FastXmlSerializer) AtomicFile(com.lody.virtual.helper.utils.AtomicFile) FileOutputStream(java.io.FileOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) FastXmlSerializer(com.lody.virtual.helper.utils.FastXmlSerializer) XmlSerializer(org.xmlpull.v1.XmlSerializer) VUserInfo(com.lody.virtual.os.VUserInfo)

Example 52 with FileOutputStream

use of java.io.FileOutputStream in project VirtualApp by asLody.

the class VUserManagerService method writeBitmapLocked.

private void writeBitmapLocked(VUserInfo info, Bitmap bitmap) {
    try {
        File dir = new File(mUsersDir, Integer.toString(info.id));
        File file = new File(dir, USER_PHOTO_FILENAME);
        if (!dir.exists()) {
            dir.mkdir();
        //                FileUtils.setPermissions(
        //                        dir.getPath(),
        //                        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
        //                        -1, -1);
        }
        FileOutputStream os;
        if (bitmap.compress(Bitmap.CompressFormat.PNG, 100, os = new FileOutputStream(file))) {
            info.iconPath = file.getAbsolutePath();
        }
        try {
            os.close();
        } catch (IOException ioe) {
        // What the ... !
        }
    } catch (FileNotFoundException e) {
        VLog.w(LOG_TAG, "Error setting photo for user ", e);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) AtomicFile(com.lody.virtual.helper.utils.AtomicFile) File(java.io.File)

Example 53 with FileOutputStream

use of java.io.FileOutputStream in project VirtualApp by asLody.

the class PackageParserEx method savePackageCache.

public static void savePackageCache(VPackage pkg) {
    final String packageName = pkg.packageName;
    Parcel p = Parcel.obtain();
    try {
        p.writeInt(4);
        pkg.writeToParcel(p, 0);
        FileOutputStream fos = new FileOutputStream(VEnvironment.getPackageCacheFile(packageName));
        fos.write(p.marshall());
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        p.recycle();
    }
    Signature[] signatures = pkg.mSignatures;
    if (signatures != null) {
        File signatureFile = VEnvironment.getSignatureFile(packageName);
        if (signatureFile.exists() && !signatureFile.delete()) {
            VLog.w(TAG, "Unable to delete the signatures of " + packageName);
        }
        p = Parcel.obtain();
        try {
            p.writeTypedArray(signatures, 0);
            FileUtils.writeParcelToFile(p, signatureFile);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            p.recycle();
        }
    }
}
Also used : Parcel(android.os.Parcel) FileOutputStream(java.io.FileOutputStream) Signature(android.content.pm.Signature) IOException(java.io.IOException) File(java.io.File) IOException(java.io.IOException)

Example 54 with FileOutputStream

use of java.io.FileOutputStream in project bazel by bazelbuild.

the class ZipReaderTest method testZip64_NumFiles.

@Test
public void testZip64_NumFiles() throws IOException {
    try (ZipWriter writer = new ZipWriter(new FileOutputStream(test), UTF_8, true)) {
        ZipFileEntry template = new ZipFileEntry("template");
        template.setSize(0);
        template.setCompressedSize(0);
        template.setCrc(0);
        template.setTime(ZipUtil.DOS_EPOCH);
        for (int i = 0; i < 0x100ff; i++) {
            ZipFileEntry entry = new ZipFileEntry(template);
            entry.setName("entry" + i);
            writer.putNextEntry(entry);
        }
    }
    try (ZipReader reader = new ZipReader(test, UTF_8, true)) {
        Collection<ZipFileEntry> entries = reader.entries();
        assertThat(entries).hasSize(0x100ff);
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) Test(org.junit.Test)

Example 55 with FileOutputStream

use of java.io.FileOutputStream in project bazel by bazelbuild.

the class ZipReaderTest method testZip64_Zip64SizeFile.

@Test
public void testZip64_Zip64SizeFile() throws IOException {
    File biggerFile = tmp.newFile("big");
    try (RandomAccessFile biggerOut = new RandomAccessFile(biggerFile, "rw")) {
        biggerOut.setLength(0x1000000ffL);
    }
    try (ZipWriter writer = new ZipWriter(new FileOutputStream(test), UTF_8, true)) {
        ZipFileEntry bigEntry = new ZipFileEntry(biggerFile.getName());
        bigEntry.setSize(0x1000000ffL);
        bigEntry.setCompressedSize(0x1000000ffL);
        bigEntry.setCrc(0);
        bigEntry.setTime(ZipUtil.DOS_EPOCH);
        writer.putNextEntry(bigEntry);
        ByteStreams.copy(new BufferedInputStream(new FileInputStream(biggerFile)), writer);
    }
    try (ZipReader reader = new ZipReader(test, UTF_8)) {
        Collection<ZipFileEntry> entries = reader.entries();
        assertThat(entries).hasSize(1);
        ZipFileEntry bigEntry = reader.getEntry(biggerFile.getName());
        assertThat(bigEntry.getSize()).isEqualTo(0x1000000ffL);
    }
}
Also used : RandomAccessFile(java.io.RandomAccessFile) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Aggregations

FileOutputStream (java.io.FileOutputStream)6586 File (java.io.File)3769 IOException (java.io.IOException)2864 FileInputStream (java.io.FileInputStream)1189 OutputStream (java.io.OutputStream)1071 BufferedOutputStream (java.io.BufferedOutputStream)907 InputStream (java.io.InputStream)864 FileNotFoundException (java.io.FileNotFoundException)682 OutputStreamWriter (java.io.OutputStreamWriter)644 Test (org.junit.Test)528 BufferedWriter (java.io.BufferedWriter)313 PrintWriter (java.io.PrintWriter)311 ZipEntry (java.util.zip.ZipEntry)311 BufferedInputStream (java.io.BufferedInputStream)287 DataOutputStream (java.io.DataOutputStream)275 ZipOutputStream (java.util.zip.ZipOutputStream)273 ByteArrayOutputStream (java.io.ByteArrayOutputStream)267 ArrayList (java.util.ArrayList)247 Writer (java.io.Writer)215 URL (java.net.URL)206