Skip to content

Commit

Permalink
Extract common function.
Browse files Browse the repository at this point in the history
  • Loading branch information
dkocher committed Jan 13, 2025
1 parent 8c9017c commit 03a8bc7
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/main/java/com/github/sardine/impl/SardineImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,7 @@ public void put(String url, byte[] data) throws IOException
@Override
public void put(String url, byte[] data, String contentType) throws IOException
{
ByteArrayEntity entity = new ByteArrayEntity(data, contentType != null ? ContentType.create(contentType) : null);
ByteArrayEntity entity = new ByteArrayEntity(data, createContentType(contentType));
this.put(url, entity, contentType, true);
}

Expand All @@ -868,7 +868,7 @@ public void put(String url, InputStream dataStream, String contentType, boolean
@Override
public void put(String url, InputStream dataStream, String contentType, boolean expectContinue, long contentLength) throws IOException
{
InputStreamEntity entity = new InputStreamEntity(dataStream, contentLength, contentType != null ? ContentType.create(contentType) : null);
InputStreamEntity entity = new InputStreamEntity(dataStream, contentLength, createContentType(contentType));
this.put(url, entity, contentType, expectContinue);
}

Expand All @@ -886,7 +886,7 @@ public void put(String url, InputStream dataStream, String contentType, Map<Stri
public void put(String url, InputStream dataStream, String contentType, List<Header> headers) throws IOException
{
// A length of -1 means "go until end of stream"
InputStreamEntity entity = new InputStreamEntity(dataStream, -1, contentType != null ? ContentType.create(contentType) : null);
InputStreamEntity entity = new InputStreamEntity(dataStream, -1, createContentType(contentType));
this.put(url, entity, headers);
}

Expand Down Expand Up @@ -965,7 +965,7 @@ public void put(String url, File localFile, String contentType) throws IOExcepti
@Override
public void put(String url, File localFile, String contentType, boolean expectContinue) throws IOException
{
FileEntity content = new FileEntity(localFile, contentType != null ? ContentType.create(contentType) : null);
FileEntity content = new FileEntity(localFile, createContentType(contentType));
this.put(url, content, contentType, expectContinue);
}

Expand Down Expand Up @@ -1215,4 +1215,14 @@ private static HttpEntity createEntity(Object jaxbElement) throws IOException
{
return new StringEntity(SardineUtil.toXml(jaxbElement), StandardCharsets.UTF_8);
}

/**
* Create MIME content type from string
*
* @param contentType MIME content type or null
* @return May be null
*/
private static ContentType createContentType(String contentType) {
return contentType != null ? ContentType.create(contentType) : null;
}
}

0 comments on commit 03a8bc7

Please sign in to comment.