来自File.renameTo()的损坏文件

时间:2013-07-26 00:28:52

标签: java android itext

这是此问题的后续内容:here,涉及iText。我创建一个具有不同旋转角度的新Pdf,然后删除旧的Pdf并将新的重命名为旧的名称。我已经确定我的问题实际发生了(只有当rotation == null,wtf时)才能调用

outFile.renameTo(inFile)

奇怪的是,renameTo()返回true,但文件不等于原始文件,outFile将不再在Windows上的Adobe Reader中打开。我尝试在桌面Pdf修复程序中分析损坏的Pdf文件,我得到的结果是:

The end-of-file marker was not found.
The ‘startxref’ keyword or the xref position was not found.
The end-of-file marker was not found.

如果我省略了对delete()和renameTo()的调用,我会留下两个文件,这两个文件都没有损坏。我也尝试用byte []复制文件内容,结果相同。我试过outFile.renameTo(new File(inFile.toString()),因为inFile实际上是File的子类,结果相同。我尝试了新的FileDescriptor()。sync(),结果相同。我试过添加这个在每个文件操作之间广播,结果相同:

PdfRotateService.appContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
            .parse("file://")));

我试过用同样的结果睡觉线程。我已经验证了路径是否正确。不抛出任何异常,delele()和renameTo()返回true。我还尝试保留对FileOutputStream的引用,并在finally块中手动关闭它。

我开始认为Android操作系统中存在一个错误或者某些东西(但也许我忽略了一些简单的东西),请帮忙!我想要一个与原始文件名相同的旋转Pdf。

static boolean rotatePdf(LocalFile inFile, int angle)
{
    PdfReader reader = null;
    PdfStamper stamper = null;

    LocalFile outFile = getGoodFile(inFile, ROTATE_SUFFIX);

    boolean worked = true;

    try
    {
        reader = new PdfReader(inFile.toString());
        stamper = new PdfStamper(reader, new FileOutputStream(outFile));

        int i = FIRST_PAGE;
        int l = reader.getNumberOfPages();

        for (; i <= l; ++i)
        {
            int desiredRot = angle;
            PdfDictionary pageDict = reader.getPageN(i);

            PdfNumber rotation = pageDict.getAsNumber(PdfName.ROTATE);

            if (rotation != null)
            {
                desiredRot += rotation.intValue();
                desiredRot %= 360;
            }
            // else
            // worked = false;

            pageDict.put(PdfName.ROTATE, new PdfNumber(desiredRot));
        }
    } catch (IOException e)
    {
        worked = false;
        Log.w("Rotate", "Caught IOException in rotate");
        e.printStackTrace();
    } catch (DocumentException e)
    {
        worked = false;
        Log.w("Rotate", "Caught DocumentException in rotate");
        e.printStackTrace();
    } finally
    {

        boolean z = closeQuietly(stamper);
        boolean y = closeQuietly(reader);

        if (!(y && z))
            worked = false;
    }

    if (worked)
    {
        if (!inFile.delete())
            worked = false;

        if (!outFile.renameTo(inFile))
            worked = false;
    }
    else
    {
        outFile.delete();
    }

    return worked;
}

static boolean closeQuietly(Object resource)
{
    try
    {
        if (resource != null)
        {
            if (resource instanceof PdfReader)
                ((PdfReader) resource).close();
            else if (resource instanceof PdfStamper)
                ((PdfStamper) resource).close();
            else
                ((Closeable) resource).close();
            return true;
        }
    } catch (Exception ex)
    {
        Log.w("Exception during Resource.close()", ex);
    }
    return false;
}

public static LocalFile getGoodFile(LocalFile inFile, String suffix)
{
    @SuppressWarnings("unused")
    String outString = inFile.getParent() + DIRECTORY_SEPARATOR +
            removeExtension(inFile.getName()) + suffix + getExtension(inFile.getName());

    LocalFile outFile = new LocalFile(inFile.getParent() + DIRECTORY_SEPARATOR +
            removeExtension(inFile.getName()) + suffix + getExtension(inFile.getName()));

    int n = 1;
    while (outFile.isFile())
    {
        outFile = new LocalFile(inFile.getParent() + DIRECTORY_SEPARATOR +
                removeExtension(inFile.getName()) + suffix + n + getExtension(inFile.getName()));
        ++n;
    }
    return outFile;
}

0 个答案:

没有答案