Skip to content

Commit 550130b

Browse files
anu3990ehennum
authored andcommitted
Changing the implementation as per the morning discussion.
1 parent c350f21 commit 550130b

File tree

3 files changed

+35
-45
lines changed

3 files changed

+35
-45
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@
124124
import java.io.File;
125125
import java.io.IOException;
126126
import java.io.InputStream;
127+
import java.io.OutputStream;
127128
import java.io.PrintStream;
128129
import java.io.Reader;
129130
import java.io.UnsupportedEncodingException;
@@ -5388,7 +5389,7 @@ private <T> T getEntity(ResponseBody body, Class<T> as) {
53885389
}
53895390
Path path = Files.createTempFile("tmp", suffix);
53905391
if ( isBinary == true ) {
5391-
Utilities.write(body.byteStream(), path);
5392+
Utilities.write(body.byteStream(), (OutputStream) path);
53925393
} else {
53935394
Writer out = Files.newBufferedWriter(path, Charset.forName("UTF-8"));
53945395
try {

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

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,10 @@
2525
import java.io.InputStream;
2626
import java.io.OutputStream;
2727
import java.io.OutputStreamWriter;
28-
import java.io.RandomAccessFile;
2928
import java.io.Reader;
3029
import java.io.StringReader;
31-
import java.nio.ByteBuffer;
32-
import java.nio.channels.FileChannel;
3330
import java.nio.charset.Charset;
34-
import java.nio.file.Path;
31+
import java.nio.charset.StandardCharsets;
3532
import java.util.ArrayList;
3633
import java.util.List;
3734

@@ -676,25 +673,38 @@ static public DatatypeFactory getDatatypeFactory() {
676673
return datatypeFactory;
677674
}
678675

679-
static public void write(InputStream in, Path path) {
676+
static public void write(InputStream in, OutputStream outStream) throws IOException {
680677
try {
681-
RandomAccessFile outFile = new RandomAccessFile(path.toString(), "rw");
682-
FileChannel inChannel = outFile.getChannel();
683-
byte[] buf = new byte[BUFFER_SIZE * 2];
684-
ByteBuffer byteBuf;
678+
byte[] byteArray = new byte[BUFFER_SIZE * 2];
685679

686-
while (in.read(buf) != -1) {
687-
byteBuf = ByteBuffer.wrap(buf);
688-
byteBuf.flip();
689-
inChannel.write(byteBuf);
690-
byteBuf.clear();
680+
while (in.read(byteArray) != -1) {
681+
outStream.write(byteArray);
682+
}
683+
} finally {
684+
in.close();
685+
}
686+
}
687+
static public void write(Reader in, OutputStreamWriter out) throws IOException {
688+
try {
689+
char[] charArray = new char[BUFFER_SIZE * 2];
690+
while (in.read(charArray) != -1) {
691+
out.write(charArray);
692+
}
693+
} finally {
694+
in.close();
695+
}
696+
}
697+
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();
691708
}
692-
outFile.close();
693-
} catch (IOException e) {
694-
throw new MarkLogicIOException("Internal Exception occured.");
695-
}
696709
}
697-
698-
static public void write(Reader in, OutputStreamWriter out) { }
699-
static public void write(Reader in, OutputStream out) {}
700710
}

marklogic-client-api/src/main/java/com/marklogic/client/io/ReaderHandle.java

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.slf4j.LoggerFactory;
3535

3636
import com.marklogic.client.MarkLogicIOException;
37+
import com.marklogic.client.impl.Utilities;
3738
import com.marklogic.client.io.marker.BufferableHandle;
3839
import com.marklogic.client.io.marker.ContentHandle;
3940
import com.marklogic.client.io.marker.ContentHandleFactory;
@@ -70,8 +71,6 @@ public class ReaderHandle
7071
{
7172
static final private Logger logger = LoggerFactory.getLogger(InputStreamHandle.class);
7273

73-
final static private int BUFFER_SIZE = 8192;
74-
7574
private Reader content;
7675

7776
/**
@@ -227,27 +226,7 @@ protected ReaderHandle sendContent() {
227226

228227
@Override
229228
public void write(OutputStream out) throws IOException {
230-
Charset charset = Charset.forName("UTF-8");
231-
CharsetEncoder encoder = charset.newEncoder();
232-
233-
CharBuffer charBuf = CharBuffer.allocate(BUFFER_SIZE);
234-
byte[] buf = new byte[BUFFER_SIZE * 2];
235-
ByteBuffer byteBuf = ByteBuffer.wrap(buf);
236-
237-
while (content.read(charBuf) != -1) {
238-
encoder.reset();
239-
charBuf.flip();
240-
byteBuf.clear();
241-
CoderResult result = encoder.encode(charBuf, byteBuf, false);
242-
if (result.isError()) {
243-
throw new IOException("Failed during UTF-8 encoding - " + result.toString());
244-
}
245-
byteBuf.flip();
246-
out.write(buf, 0, byteBuf.limit());
247-
charBuf.clear();
248-
}
249-
250-
out.flush();
229+
Utilities.write(content, out);
251230
}
252231

253232
/** Either call close() or get().close() when finished with this handle to close the underlying Reader.

0 commit comments

Comments
 (0)