Skip to content

Commit 9633151

Browse files
anu3990ehennum
authored andcommitted
Added the suggestions and javadoc.
1 parent 550130b commit 9633151

File tree

2 files changed

+35
-26
lines changed

2 files changed

+35
-26
lines changed

marklogic-client-api/src/main/java/com/marklogic/client/impl/OkHttpServices.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@
124124
import java.io.File;
125125
import java.io.IOException;
126126
import java.io.InputStream;
127-
import java.io.OutputStream;
128127
import java.io.PrintStream;
129128
import java.io.Reader;
130129
import java.io.UnsupportedEncodingException;
@@ -5389,14 +5388,11 @@ private <T> T getEntity(ResponseBody body, Class<T> as) {
53895388
}
53905389
Path path = Files.createTempFile("tmp", suffix);
53915390
if ( isBinary == true ) {
5392-
Utilities.write(body.byteStream(), (OutputStream) path);
5391+
Files.copy(body.byteStream(), path);
53935392
} else {
5394-
Writer out = Files.newBufferedWriter(path, Charset.forName("UTF-8"));
5395-
try {
5396-
out.write(body.string());
5397-
} finally {
5398-
out.close();
5399-
}
5393+
try(Writer out = Files.newBufferedWriter(path, Charset.forName("UTF-8"))) {
5394+
Utilities.write(body.charStream(), out);
5395+
}
54005396
}
54015397
return (T) path.toFile();
54025398
} else {

marklogic-client-api/src/main/java/com/marklogic/client/impl/Utilities.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
import java.io.OutputStreamWriter;
2828
import java.io.Reader;
2929
import java.io.StringReader;
30+
import java.io.Writer;
3031
import java.nio.charset.Charset;
31-
import java.nio.charset.StandardCharsets;
3232
import java.util.ArrayList;
3333
import java.util.List;
3434

@@ -673,38 +673,51 @@ static public DatatypeFactory getDatatypeFactory() {
673673
return datatypeFactory;
674674
}
675675

676+
/**
677+
* Writes bytes from the input stream to the output stream.
678+
* @param in - the input stream passed in.
679+
* @param outStream - output stream where the bytes are written.
680+
*/
676681
static public void write(InputStream in, OutputStream outStream) throws IOException {
682+
if(in == null || outStream == null)
683+
return;
677684
try {
678685
byte[] byteArray = new byte[BUFFER_SIZE * 2];
679686

680-
while (in.read(byteArray) != -1) {
681-
outStream.write(byteArray);
687+
int byteCount = 0;
688+
while ((byteCount = in.read(byteArray)) != -1) {
689+
outStream.write(byteArray, 0, byteCount);
682690
}
683691
} finally {
684692
in.close();
685693
}
686694
}
687-
static public void write(Reader in, OutputStreamWriter out) throws IOException {
695+
696+
/**
697+
* Writes bytes from the input Reader to the Writer stream.
698+
* @param in - the Reader passed in.
699+
* @param out - Writer stream where the bytes are written.
700+
*/
701+
static public void write(Reader in, Writer out) throws IOException {
702+
if(in == null || out == null)
703+
return;
688704
try {
689705
char[] charArray = new char[BUFFER_SIZE * 2];
690-
while (in.read(charArray) != -1) {
691-
out.write(charArray);
692-
}
706+
int charCount = 0;
707+
while ((charCount = in.read(charArray)) != -1) {
708+
out.write(charArray, 0, charCount);
709+
}
693710
} finally {
694711
in.close();
695712
}
696713
}
714+
715+
/**
716+
* Writes bytes from the input Reader to the output stream.
717+
* @param in - the Reader passed in.
718+
* @param out - OutputStream where the bytes are written.
719+
*/
697720
static public void write(Reader in, OutputStream out) throws IOException {
698-
try {
699-
char[] charArray = new char[BUFFER_SIZE * 2];
700-
byte[] byteArray;
701-
702-
while (in.read(charArray) != -1) {
703-
byteArray = new String(charArray).getBytes(StandardCharsets.UTF_8);
704-
out.write(byteArray);
705-
}
706-
} finally {
707-
in.close();
708-
}
721+
write(in, new OutputStreamWriter(out));
709722
}
710723
}

0 commit comments

Comments
 (0)