is = (InputStream) obj;
} else if (obj instanceof DirContext) {
createSubcontext(name, attrs);
return;
}
if (is == null) throw new NamingException(smgr.getString("resources.bindFailed", name));
// Try to get Quota
long quotaLeft = -1;
boolean withQuotaCheck = false;
VFSContainer parentContainer = file.getParentContainer();
if (parentContainer != null) {
quotaLeft = VFSManager.getQuotaLeftKB(parentContainer);
if (quotaLeft != Quota.UNLIMITED) {
quotaLeft = quotaLeft * 1024; // convert from kB
withQuotaCheck = true;
} else {
withQuotaCheck = false;
}
}
// Open os
OutputStream os = null;
byte buffer[] = new byte[bufferSize];
int len = -1;
try {
os = file.getOutputStream(false);
while (true) {
len = is.read(buffer);
if (len == -1) break;
if (withQuotaCheck) {
// re-calculate quota and check
quotaLeft = quotaLeft - len;
if (quotaLeft < 0) throw new NamingException("Quota exceeded.");
}
os.write(buffer, 0, len);
}
} catch (Exception e) {
FileUtils.closeSafely(os); // close first, in order to be able to delete any reamins of the file
file.delete();
if (e instanceof NamingException) throw (NamingException)e;
throw new NamingException(smgr.getString("resources.bindFailed"));
} finally {
FileUtils.closeSafely(os);
FileUtils.closeSafely(is);
}
}