public void upload(final Handler handler, final Item item) throws CloudsyncException, NoSuchFileException {
initService(handler);
String title = handler.getLocalEncryptedTitle(item);
File parentDriveItem = null;
File driveItem;
int retryCount = 0;
do {
try {
refreshCredential();
parentDriveItem = _getDriveItem(item.getParent());
final ParentReference parentReference = new ParentReference();
parentReference.setId(parentDriveItem.getId());
driveItem = new File();
driveItem.setTitle(title);
driveItem.setParents(Arrays.asList(parentReference));
final byte[] data = _prepareDriveItem(driveItem, item, handler, true);
if (data == null) {
driveItem = service.files().insert(driveItem).execute();
} else {
final InputStreamContent params = new InputStreamContent(FILE, new ByteArrayInputStream(data));
params.setLength(data.length);
Insert inserter = service.files().insert(driveItem, params);
MediaHttpUploader uploader = inserter.getMediaHttpUploader();
prepareUploader(uploader, data);
driveItem = inserter.execute();
}
if (driveItem == null) {
throw new CloudsyncException("Could not create item '" + item.getPath() + "'");
}
_addToCache(driveItem, null);
item.setRemoteIdentifier(driveItem.getId());
return;
} catch (final NoSuchFileException e) {
throw e;
} catch (final IOException e) {
if (parentDriveItem != null) {
for (int i = 0; i < MIN_SEARCH_RETRIES; i++) {
driveItem = _searchDriveItem(item.getParent(), title);
if (driveItem != null) {
LOGGER.log(Level.WARNING, getExceptionMessage(e) + "found uploaded item - try to update");
item.setRemoteIdentifier(driveItem.getId());
update(handler, item, true);
return;
}
LOGGER.log(Level.WARNING, getExceptionMessage(e) + "item not uploaded - retry " + (i + 1) + "/" + MIN_SEARCH_RETRIES + " - wait " + MIN_SEARCH_BREAK + " ms");
sleep(MIN_SEARCH_BREAK);