Source:
<code>private <T> T getEntity(ResponseBody body, Class<T> as) { try { if ( as == InputStream.class ) { return (T) body.byteStream(); } else if ( as == byte[].class ) { return (T) body.bytes(); } else if ( as == Reader.class ) { return (T) body.charStream(); } else if ( as == String.class ) { return (T) body.string(); } else if ( as == MimeMultipart.class ) { ByteArrayDataSource dataSource = new ByteArrayDataSource(body.byteStream(), body.contentType().toString()); return (T) new MimeMultipart(dataSource); } else if ( as == File.class ) { String suffix = ".unknown"; boolean isBinary = true; MediaType mediaType = body.contentType(); if ( mediaType != null ) { suffix = "." + mediaType.subtype(); for ( String type : new String[] {"json", "xml", "xquery", "sjs", "javascript", "html"} ) { if ( type.equalsIgnoreCase(mediaType.subtype()) ) { isBinary = false; } } if ( isBinary == true && "text".equalsIgnoreCase(mediaType.type()) ) { suffix = ".txt"; isBinary = false; } } Path path = Files.createTempFile("tmp", suffix); if ( isBinary == true ) { <START> Utilities.write(body.byteStream(), (OutputStream) path); <END> } else { Writer out = Files.newBufferedWriter(path, Charset.forName("UTF-8")); try { out.write(body.string()); } finally { out.close(); } } return (T) path.toFile(); } else { throw new IllegalArgumentException( "Handle recieveAs returned " + as + " which is not a supported type. " + "Try InputStream, Reader, String, byte[], File."); } } catch (IOException e) { throw new MarkLogicIOException(e); } catch (MessagingException e) { throw new MarkLogicIOException(e); } }</code><technical_language>I Path extends OutputStream. open a FileOutputStream() path, I spotted alternative for this case: <LINK_0> in: Files.copy(body.byteStream(), path, StandardOpenOption.CREATE, StandardOpenOption.WRITE); do think</technical_language>

Target:
private <T> T getEntity(ResponseBody body, Class<T> as) { try { if ( as == InputStream.class ) { return (T) body.byteStream(); } else if ( as == byte[].class ) { return (T) body.bytes(); } else if ( as == Reader.class ) { return (T) body.charStream(); } else if ( as == String.class ) { return (T) body.string(); } else if ( as == MimeMultipart.class ) { ByteArrayDataSource dataSource = new ByteArrayDataSource(body.byteStream(), body.contentType().toString()); return (T) new MimeMultipart(dataSource); } else if ( as == File.class ) { String suffix = ".unknown"; boolean isBinary = true; MediaType mediaType = body.contentType(); if ( mediaType != null ) { suffix = "." + mediaType.subtype(); for ( String type : new String[] {"json", "xml", "xquery", "sjs", "javascript", "html"} ) { if ( type.equalsIgnoreCase(mediaType.subtype()) ) { isBinary = false; } } if ( isBinary == true && "text".equalsIgnoreCase(mediaType.type()) ) { suffix = ".txt"; isBinary = false; } } Path path = Files.createTempFile("tmp", suffix); if ( isBinary == true ) { Files.copy(body.byteStream(), path); } else { try(Writer out = Files.newBufferedWriter(path, Charset.forName("UTF-8"))) { Utilities.write(body.charStream(), out); } } return (T) path.toFile(); } else { throw new IllegalArgumentException( "Handle recieveAs returned " + as + " which is not a supported type. " + "Try InputStream, Reader, String, byte[], File."); } } catch (IOException e) { throw new MarkLogicIOException(e); } catch (MessagingException e) { throw new MarkLogicIOException(e); } }