// even if you know the content. In the user interface, we will offer the option of inserting as a
// random SSK to take advantage of this.
boolean randomiseSplitfileKeys = randomiseSplitfileKeys(targetURI, ctx, persistent());
if(data == null)
throw new InsertException(InsertExceptionMode.BUCKET_ERROR, "No data to insert", null);
boolean cancel = false;
synchronized(this) {
if(restart) {
clearCountersOnRestart();
if(currentState != null && !finished) {
if(logMINOR) Logger.minor(this, "Can't restart, not finished and currentState != null : "+currentState);
return false;
}
if(finished)
startedStarting = false;
finished = false;
}
if(startedStarting) {
if(logMINOR) Logger.minor(this, "Can't "+(restart?"restart":"start")+" : startedStarting = true");
return false;
}
startedStarting = true;
if(currentState != null) {
if(logMINOR) Logger.minor(this, "Can't "+(restart?"restart":"start")+" : currentState != null : "+currentState);
return false;
}
cancel = this.cancelled;
cryptoKey = null;
if(overrideSplitfileCrypto != null) {
cryptoKey = overrideSplitfileCrypto;
if (cryptoKey.length != 32)
throw new InsertException(InsertExceptionMode.INVALID_URI, "overrideSplitfileCryptoKey must be of length 32", null);
} else if(randomiseSplitfileKeys) {
cryptoKey = new byte[32];
context.random.nextBytes(cryptoKey);
}
if(!cancel) {
if(!binaryBlob) {
ClientMetadata meta = cm;
if(meta != null) meta = persistent() ? meta.clone() : meta;
currentState =
new SingleFileInserter(this, this, new InsertBlock(data, meta, targetURI), isMetadata, ctx, realTimeFlag,
false, false, null, null, false, targetFilename, false, persistent(), 0, 0, null, cryptoAlgorithm, cryptoKey, metadataThreshold);
} else
currentState =
new BinaryBlobInserter(data, this, getClient(), false, priorityClass, ctx, context);
}
}
if(cancel) {
onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
return false;
}
synchronized(this) {
cancel = cancelled;
}
if(cancel) {
onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
return false;
}
if(logMINOR)
Logger.minor(this, "Starting insert: "+currentState);
if(currentState instanceof SingleFileInserter)
((SingleFileInserter)currentState).start(context);
else
currentState.schedule(context);
synchronized(this) {
cancel = cancelled;
}
if(cancel) {
onFailure(new InsertException(InsertExceptionMode.CANCELLED), null, context);
return false;
}
} catch (InsertException e) {
Logger.error(this, "Failed to start insert: "+e, e);
synchronized(this) {
finished = true;
currentState = null;
}
// notify the client that the insert could not even be started
if (this.client!=null) {
this.client.onFailure(e, this);
}
} catch (IOException e) {
Logger.error(this, "Failed to start insert: "+e, e);
synchronized(this) {
finished = true;
currentState = null;
}
// notify the client that the insert could not even be started
if (this.client!=null) {
this.client.onFailure(new InsertException(InsertExceptionMode.BUCKET_ERROR, e, null), this);
}
} catch (BinaryBlobFormatException e) {
Logger.error(this, "Failed to start insert: "+e, e);
synchronized(this) {
finished = true;
currentState = null;
}
// notify the client that the insert could not even be started
if (this.client!=null) {
this.client.onFailure(new InsertException(InsertExceptionMode.BINARY_BLOB_FORMAT_ERROR, e, null), this);
}
}
if(logMINOR)
Logger.minor(this, "Started "+this);
return true;