Program: 54839
Project: 54839
Buggy source:
/** 
 * Executes a direct media upload or resumable media upload conforming to the specifications listed <a href='http://code.google.com/apis/gdata/docs/resumable_upload.html'>here.</a> <p> This method is not reentrant. A new instance of   {@link MediaHttpUploader} must be instantiatedbefore upload called be called again. </p> <p> If an error is encountered during the request execution the caller is responsible for parsing the response correctly. For example for JSON errors: </p> <pre> if (!response.isSuccessStatusCode()) { throw GoogleJsonResponseException.from(jsonFactory, response); } </pre> <p> Callers should call  {@link HttpResponse#disconnect} when the returned HTTP response object isno longer needed. However,  {@link HttpResponse#disconnect} does not have to be called if theresponse stream is properly closed. Example usage: </p> <pre> HttpResponse response = batch.upload(initiationRequestUrl); try { // process the HTTP response object } finally { response.disconnect(); } </pre>
 * @param initiationRequestUrl The request URL where the initiation request will be sent
 * @return HTTP response
 */
public HttpResponse upload(GenericUrl initiationRequestUrl) throws IOException {
  Preconditions.checkArgument(uploadState == UploadState.NOT_STARTED);
  if (directUploadEnabled) {
    updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);
    HttpContent content=mediaContent;
    if (metadata != null) {
      content=new MultipartContent().setContentParts(Arrays.asList(metadata,mediaContent));
      initiationRequestUrl.put("String_Node_Str","String_Node_Str");
    }
 else {
      initiationRequestUrl.put("String_Node_Str","String_Node_Str");
    }
    HttpRequest request=requestFactory.buildRequest(initiationRequestMethod,initiationRequestUrl,content);
    request.getHeaders().putAll(initiationHeaders);
    HttpResponse response=executeCurrentRequestWithBackOffAndGZip(request);
    boolean responseProcessed=false;
    try {
      if (getMediaContentLength() >= 0) {
        bytesUploaded=getMediaContentLength();
      }
      updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
      responseProcessed=true;
    }
  finally {
      if (!responseProcessed) {
        response.disconnect();
      }
    }
    return response;
  }
  HttpResponse initialResponse=executeUploadInitiation(initiationRequestUrl);
  if (!initialResponse.isSuccessStatusCode()) {
    return initialResponse;
  }
  GenericUrl uploadUrl;
  try {
    uploadUrl=new GenericUrl(initialResponse.getHeaders().getLocation());
  }
  finally {
    initialResponse.disconnect();
  }
  contentInputStream=mediaContent.getInputStream();
  if (!contentInputStream.markSupported() && getMediaContentLength() >= 0) {
    contentInputStream=new BufferedInputStream(contentInputStream);
  }
  HttpResponse response;
  while (true) {
    currentRequest=requestFactory.buildPutRequest(uploadUrl,null);
    setContentAndHeadersOnCurrentRequest(bytesUploaded);
    if (backOffPolicyEnabled) {
      currentRequest.setBackOffPolicy(new MediaUploadExponentialBackOffPolicy(this));
    }
 else {
      new MediaUploadErrorHandler(this,currentRequest);
    }
    if (getMediaContentLength() >= 0) {
    }
 else     if (!disableGZipContent) {
      currentRequest.setEncoding(new GZipEncoding());
    }
    response=executeCurrentRequest(currentRequest);
    boolean returningResponse=false;
    try {
      if (response.isSuccessStatusCode()) {
        bytesUploaded=getMediaContentLength();
        if (mediaContent.getCloseInputStream()) {
          contentInputStream.close();
        }
        updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
        returningResponse=true;
        return response;
      }
      if (response.getStatusCode() != 308) {
        returningResponse=true;
        return response;
      }
      String updatedUploadUrl=response.getHeaders().getLocation();
      if (updatedUploadUrl != null) {
        uploadUrl=new GenericUrl(updatedUploadUrl);
      }
      bytesUploaded=getNextByteIndex(response.getHeaders().getRange());
      currentRequestContentBuffer=null;
      updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);
    }
  finally {
      if (!returningResponse) {
        response.disconnect();
      }
    }
  }
}

Fixed version (ground truth):
/** 
 * Executes a direct media upload or resumable media upload conforming to the specifications listed <a href='http://code.google.com/apis/gdata/docs/resumable_upload.html'>here.</a> <p> This method is not reentrant. A new instance of   {@link MediaHttpUploader} must be instantiatedbefore upload called be called again. </p> <p> If an error is encountered during the request execution the caller is responsible for parsing the response correctly. For example for JSON errors: </p> <pre> if (!response.isSuccessStatusCode()) { throw GoogleJsonResponseException.from(jsonFactory, response); } </pre> <p> Callers should call  {@link HttpResponse#disconnect} when the returned HTTP response object isno longer needed. However,  {@link HttpResponse#disconnect} does not have to be called if theresponse stream is properly closed. Example usage: </p> <pre> HttpResponse response = batch.upload(initiationRequestUrl); try { // process the HTTP response object } finally { response.disconnect(); } </pre>
 * @param initiationRequestUrl The request URL where the initiation request will be sent
 * @return HTTP response
 */
public HttpResponse upload(GenericUrl initiationRequestUrl) throws IOException {
  Preconditions.checkArgument(uploadState == UploadState.NOT_STARTED);
  if (directUploadEnabled) {
    updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);
    HttpContent content=mediaContent;
    if (metadata != null) {
      content=new MultipartContent().setContentParts(Arrays.asList(metadata,mediaContent));
      initiationRequestUrl.put("String_Node_Str","String_Node_Str");
    }
 else {
      initiationRequestUrl.put("String_Node_Str","String_Node_Str");
    }
    HttpRequest request=requestFactory.buildRequest(initiationRequestMethod,initiationRequestUrl,content);
    request.getHeaders().putAll(initiationHeaders);
    HttpResponse response=executeCurrentRequestWithBackOffAndGZip(request);
    boolean responseProcessed=false;
    try {
      if (getMediaContentLength() >= 0) {
        bytesUploaded=getMediaContentLength();
      }
      updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
      responseProcessed=true;
    }
  finally {
      if (!responseProcessed) {
        response.disconnect();
      }
    }
    return response;
  }
  HttpResponse initialResponse=executeUploadInitiation(initiationRequestUrl);
  if (!initialResponse.isSuccessStatusCode()) {
    return initialResponse;
  }
  GenericUrl uploadUrl;
  try {
    uploadUrl=new GenericUrl(initialResponse.getHeaders().getLocation());
  }
  finally {
    initialResponse.disconnect();
  }
  contentInputStream=mediaContent.getInputStream();
  if (!contentInputStream.markSupported() && getMediaContentLength() >= 0) {
    contentInputStream=new BufferedInputStream(contentInputStream);
  }
  HttpResponse response;
  while (true) {
    currentRequest=requestFactory.buildPutRequest(uploadUrl,null);
    setContentAndHeadersOnCurrentRequest(bytesUploaded);
    if (backOffPolicyEnabled) {
      currentRequest.setBackOffPolicy(new MediaUploadExponentialBackOffPolicy(this));
    }
 else {
    }
    if (getMediaContentLength() >= 0) {
    }
 else     if (!disableGZipContent) {
      currentRequest.setEncoding(new GZipEncoding());
    }
    response=executeCurrentRequest(currentRequest);
    boolean returningResponse=false;
    try {
      if (response.isSuccessStatusCode()) {
        bytesUploaded=getMediaContentLength();
        if (mediaContent.getCloseInputStream()) {
          contentInputStream.close();
        }
        updateStateAndNotifyListener(UploadState.MEDIA_COMPLETE);
        returningResponse=true;
        return response;
      }
      if (response.getStatusCode() != 308) {
        returningResponse=true;
        return response;
      }
      String updatedUploadUrl=response.getHeaders().getLocation();
      if (updatedUploadUrl != null) {
        uploadUrl=new GenericUrl(updatedUploadUrl);
      }
      bytesUploaded=getNextByteIndex(response.getHeaders().getRange());
      currentRequestContentBuffer=null;
      updateStateAndNotifyListener(UploadState.MEDIA_IN_PROGRESS);
    }
  finally {
      if (!returningResponse) {
        response.disconnect();
      }
    }
  }
}