@Override
    public StatusOutputStream<ObjStat> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException {
        try {
            try {
                final IRODSFileSystemAO fs = session.getClient();
                final IRODSFileOutputStream out = fs.getIRODSFileFactory().instanceIRODSFileOutputStream(
                        file.getAbsolute(), status.isAppend() ? DataObjInp.OpenFlags.READ_WRITE : DataObjInp.OpenFlags.WRITE_TRUNCATE);
                return new StatusOutputStream<ObjStat>(new PackingIrodsOutputStream(out)) {
                    @Override
                    public ObjStat getStatus() throws BackgroundException {
                        // No remote attributes from server returned after upload
                        try {
                            return fs.getObjStat(file.getAbsolute());
                        }
                        catch(JargonException e) {
                            throw new IRODSExceptionMappingService().map("Failure to read attributes of {0}", e, file);
                        }
                    }
                };
            }
            catch(JargonRuntimeException e) {
                if(e.getCause() instanceof JargonException) {
                    throw (JargonException) e.getCause();
                }
                throw new DefaultExceptionMappingService().map(e);
            }
        }
        catch(JargonException e) {
            throw new IRODSExceptionMappingService().map("Uploading {0} failed", e, file);
        }
    }
@Test
    public void testWriteAppend() throws Exception {
        final ProtocolFactory factory = new ProtocolFactory(new HashSet<>(Collections.singleton(new IRODSProtocol())));
        final Profile profile = new ProfilePlistReader(factory).read(
                this.getClass().getResourceAsStream("/iRODS (iPlant Collaborative).cyberduckprofile"));
        final Host host = new Host(profile, profile.getDefaultHostname(), new Credentials(
                PROPERTIES.get("irods.key"), PROPERTIES.get("irods.secret")
        ));

        final IRODSSession session = new IRODSSession(host);
        session.open(new DisabledProxyFinder(), new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
        session.login(new DisabledLoginCallback(), new DisabledCancelCallback());

        final Path test = new Path(new IRODSHomeFinderService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
        assertFalse(session.getFeature(Find.class).find(test));

        final byte[] content = RandomUtils.nextBytes(100);

        final TransferStatus status = new TransferStatus();
        status.setAppend(true);
        status.setLength(content.length);

        final IRODSWriteFeature feature = new IRODSWriteFeature(session);
        assertEquals(0L, new IRODSUploadFeature(session).append(test, status).offset, 0L);

        final OutputStream out = feature.write(test, status, new DisabledConnectionCallback());
        assertNotNull(out);

        new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
        assertTrue(session.getFeature(Find.class).find(test));

        final PathAttributes attributes = new IRODSAttributesFinderFeature(session).find(test);
        assertEquals(content.length, attributes.getSize());

        final InputStream in = session.getFeature(Read.class).read(test, new TransferStatus(), new DisabledConnectionCallback());
        final byte[] buffer = new byte[content.length];
        IOUtils.readFully(in, buffer);
        in.close();
        assertArrayEquals(content, buffer);

        // Append

        final byte[] content_append = RandomUtils.nextBytes(100);

        final TransferStatus status_append = new TransferStatus();
        status_append.setAppend(true);
        status_append.setLength(content_append.length);
        status_append.setRemote(new IRODSAttributesFinderFeature(session).find(test));

        assertTrue(new IRODSUploadFeature(session).append(test, status_append).append);
        assertEquals(status.getLength(), new IRODSUploadFeature(session).append(test, status_append).offset, 0L);

        final OutputStream out_append = feature.write(test, status_append, new DisabledConnectionCallback());
        assertNotNull(out_append);

        new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content_append), out_append);
        assertTrue(session.getFeature(Find.class).find(test));

        final PathAttributes attributes_complete = new IRODSAttributesFinderFeature(session).find(test);
        assertEquals(content.length + content_append.length, attributes_complete.getSize());

        final InputStream in_append = session.getFeature(Read.class).read(test, new TransferStatus(), new DisabledConnectionCallback());
        final byte[] buffer_complete = new byte[content.length + content_append.length];
        IOUtils.readFully(in_append, buffer_complete);
        in_append.close();

        byte[] complete = new byte[content.length + content_append.length];
        System.arraycopy(content, 0, complete, 0, content.length);
        System.arraycopy(content_append, 0, complete, content.length, content_append.length);
        assertArrayEquals(complete, buffer_complete);

        session.getFeature(Delete.class).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback());
        assertFalse(session.getFeature(Find.class).find(test));
        session.close();
    }