private void createReferenceFile(final ObjectStreamClass osc) {
            final File referenceDir = context.getReferenceBaseDir();
            final File referenceFile = new File(new File(referenceDir, osc.getName()), Long
                    .toString(osc.getSerialVersionUID())
                    + ".ser");
            if (!referenceFile.isFile()) {
                if (!referenceDir.isDirectory()) {
                    throw new AssertionFailedError("Missing serialization reference directory: "
                            + referenceDir.getAbsolutePath());
                }
                if (!referenceFile.getParentFile().isDirectory()) {
                    referenceFile.getParentFile().mkdir();
                }
                try {
                    try {
                        final FileOutputStream out = new FileOutputStream(referenceFile);
                        try {
                            out.write(baos.toByteArray());
                        } catch (final IOException e) {
                            throw new AssertionFailedError("Cannot write missing reference file "
                                    + referenceFile.getAbsolutePath()
                                    + ". "
                                    + e.getMessage());
                        } finally {
                            out.close();
                        }
                    } catch (final FileNotFoundException e) {
                        throw new AssertionFailedError("Cannot create missing reference file "
                                + referenceFile.getAbsolutePath()
                                + ". "
                                + e.getMessage());
                    } catch (final IOException e) {
                        throw new AssertionFailedError("Cannot close missing reference file "
                                + referenceFile.getAbsolutePath()
                                + ". "
                                + e.getMessage());
                    }

                } catch (final AssertionFailedError e) {
                    if (referenceFile.exists()) {
                        referenceFile.delete();
                    }
                    throw e;
                }
                throw new AssertionFailedError("Missing reference file "
                        + referenceFile.getAbsolutePath()
                        + " created.");
            }
        }