Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions WebHDFS/WebHDFSClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,15 @@ public async Task<bool> DownloadFile(
throw new InvalidOperationException(string.Format("File {0} already exists.", pathname));
}

using (var fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
return await ReadStream(fileStream, path, offset, length, buffersize);
}
return await ReadStream(filePath, path, offset, length, buffersize);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't make this change. This is designed to stream to the file. You basically do the same thing with File.Open later for the stream to write to.

}

/// <summary>
/// https://hadoop.apache.org/docs/r2.8.0/hadoop-project-dist/hadoop-hdfs/WebHDFS.html#Open_and_Read_a_File
/// Note: You are responsible for closing the stream you send in.
/// </summary>
public async Task<bool> ReadStream(
Stream stream,
string filePath,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please leave this as Stream stream. This breaks the public contract of the method.

string path,
long? offset = null,
long? length = null,
Expand All @@ -184,12 +181,13 @@ public async Task<bool> ReadStream(
var response = await _httpClient.GetAsync(requestPath);
if (response.StatusCode.Equals(HttpStatusCode.TemporaryRedirect))
{
var response2 = await _httpClient.GetAsync(response.Headers.Location);
response2.EnsureSuccessStatusCode();
if (response2.IsSuccessStatusCode)
var response2 = await _httpClient.GetAsync(response.Headers.Location, HttpCompletionOption.ResponseHeadersRead);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the logic here can be cleaned up to accomplish the same thing without moving the File stream around.

using (var streamToRead = await response2.Content.ReadAsStreamAsync())
using (Stream streamToWriteTo = File.Open(filePath, FileMode.Create))
{
await response2.Content.CopyToAsync(stream);
await streamToRead.CopyToAsync(streamToWriteTo);
}
response2.EnsureSuccessStatusCode();
return response2.IsSuccessStatusCode;
}
throw new InvalidOperationException("Should get a 307. Instead we got: " +
Expand Down