@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 testWriteThreaded() 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 session1 = new IRODSSession(host);
        session1.open(new DisabledProxyFinder(), new DisabledHostKeyCallback(), new DisabledLoginCallback(), new DisabledCancelCallback());
        session1.login(new DisabledLoginCallback(), new DisabledCancelCallback());

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

        final CountDownLatch cw1 = new CountDownLatch(1);
        final CountDownLatch cw2 = new CountDownLatch(1);

        final Path test1 = new Path(new IRODSHomeFinderService(session1).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
        final Path test2 = new Path(new IRODSHomeFinderService(session2).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));

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

        final OutputStream out1 = new IRODSWriteFeature(session1).write(test1, new TransferStatus().append(false).withLength(content.length), new DisabledConnectionCallback());
        final OutputStream out2 = new IRODSWriteFeature(session2).write(test2, new TransferStatus().append(false).withLength(content.length), new DisabledConnectionCallback());
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out2);
                }
                catch(BackgroundException e) {
                    fail();
                }
                finally {
                    cw1.countDown();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out1);
                }
                catch(BackgroundException e) {
                    fail();
                }
                finally {
                    cw2.countDown();
                }
            }
        }).start();

        cw1.await();
        cw2.await();

        final CountDownLatch cr1 = new CountDownLatch(1);
        final CountDownLatch cr2 = new CountDownLatch(1);

        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final InputStream in1 = session1.getFeature(Read.class).read(test1, new TransferStatus(), new DisabledConnectionCallback());
                    final byte[] buffer1 = new byte[content.length];
                    IOUtils.readFully(in1, buffer1);
                    in1.close();
                    assertArrayEquals(content, buffer1);
                }
                catch(Exception e) {
                    fail();
                }
                finally {
                    cr1.countDown();
                }
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final InputStream in2 = session2.getFeature(Read.class).read(test2, new TransferStatus(), new DisabledConnectionCallback());
                    final byte[] buffer2 = new byte[content.length];
                    IOUtils.readFully(in2, buffer2);
                    in2.close();
                    assertArrayEquals(content, buffer2);
                }
                catch(Exception e) {
                    fail();
                }
                finally {
                    cr2.countDown();
                }
            }
        }).start();

        cr1.await();
        cr2.await();

        session1.close();
        session2.close();
    }