public synchronized BinaryDocument addBinaryResource(DBBroker broker,
public BinaryDocument addBinaryResource(DBBroker broker,
	public synchronized BinaryDocument addBinaryResource(DBBroker broker,			String name, byte[] data) throws EXistException,			PermissionDeniedException, LockException {		if (broker.isReadOnly())			throw new PermissionDeniedException("Database is read-only");		BinaryDocument blob = null;		Lock lock = db.getLock();		try {			lock.acquire(Lock.WRITE_LOCK);			DocumentImpl oldDoc = getDocument(getName() + '/' + name);			if (oldDoc != null) {				if(oldDoc.isLockedForWrite())					throw new PermissionDeniedException("Document " + name +							" is already locked for write");				// check if the document is locked by another user				User lockUser = oldDoc.getUserLock();				if(lockUser != null && !lockUser.equals(broker.getUser()))					throw new PermissionDeniedException("The document is locked by user " +							lockUser.getName());				// do we have permissions for update?				if (!oldDoc.getPermissions().validate(broker.getUser(),						Permission.UPDATE))					throw new PermissionDeniedException(							"document exists and update " + "is not allowed");				// no: do we have write permissions?			} else if (!getPermissions().validate(broker.getUser(),					Permission.WRITE))				throw new PermissionDeniedException(						"not allowed to write to collection " + getName());			blob = new BinaryDocument(broker, getName() + '/'					+ name, this);			if (oldDoc != null) {				blob.setCreated(oldDoc.getCreated());				blob.setLastModified(System.currentTimeMillis());				blob.setPermissions(oldDoc.getPermissions());				LOG.debug("removing old document " + oldDoc.getFileName());				if (oldDoc instanceof BinaryDocument)					broker.removeBinaryResource((BinaryDocument) oldDoc);				else					broker.removeDocument(oldDoc.getFileName());			} else {				blob.setCreated(System.currentTimeMillis());				blob.getPermissions().setOwner(broker.getUser());				blob.getPermissions().setGroup(						broker.getUser().getPrimaryGroup());			}			broker.storeBinaryResource(blob, data);			addDocument(broker, blob);			broker.addDocument(this, blob);			broker.closeDocument();			return blob;		} finally {			if(blob != null)				blob.setWriteLock(false);			lock.release();		}	}