protected void writeDocumentToFile(DocumentRecord documentRecord, File file) throws IOException { file.getParentFile().mkdirs(); FileOutputStream fos = new FileOutputStream(file); try { InputStream in = documentRecord.getContent(new InputStreamHandle()).get(); byte[] buffer = new byte[4096]; int bytesRead = -1; while ((bytesRead = in.read(buffer)) != -1) { fos.write(buffer, 0, bytesRead); } fos.flush(); } finally { fos.close(); } }
/** * Specifies the format of the content and returns the handle * as a fluent convenience. * @param format the format of the content * @return this handle */ public InputStreamHandle withFormat(Format format) { setFormat(format); return this; } /**
/** * Buffers the input stream and returns the buffer as a string * with the assumption that the stream is encoded in UTF-8. If * the stream has a different encoding, use InputStreamReader * instead of calling this method. */ @Override public String toString() { try { return new String(toBuffer(),"UTF-8"); } catch (UnsupportedEncodingException e) { throw new MarkLogicIOException(e); } }
@Override public AbstractWriteHandle getContent() { if (modifiedContent != null) { StringHandle h = new StringHandle(modifiedContent); return format != null ? h.withFormat(format) : h; } InputStreamHandle h = null; try { h = new InputStreamHandle(resource.getInputStream()); } catch (IOException e) { throw new RuntimeException(e); } return format != null ? h.withFormat(format) : h; }
public void writeDocumentUsingInputStreamHandle(DatabaseClient client, String filename, String uri, Transaction transaction, String type) throws FileNotFoundException { // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); // create handle InputStreamHandle contentHandle = new InputStreamHandle(); // get the file InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/" + filename); // set uri String docId = uri + filename; contentHandle.set(inputStream); // write doc docMgr.write(docId, contentHandle, transaction); System.out.println("Write " + docId + " to database"); }
protected void installModule(String path, String localPath) { InputStreamHandle handle = new InputStreamHandle(HubTestBase.class.getClassLoader().getResourceAsStream(localPath)); String ext = FilenameUtils.getExtension(path); DocumentMetadataHandle permissions = new DocumentMetadataHandle() .withPermission(getHubAdminConfig().getHubRoleName(), DocumentMetadataHandle.Capability.EXECUTE, UPDATE, READ); switch(ext) { case "xml": handle.setFormat(Format.XML); break; case "json": handle.setFormat(Format.JSON); break; default: handle.setFormat(Format.TEXT); } modMgr.write(path, permissions, handle); clearFlowCache(); }
static public InputStreamHandle InputStreamToHandle(InputStream value) { return (value == null) ? null : new InputStreamHandle(value); } static public Stream<InputStreamHandle> InputStreamToHandle(Stream<? extends InputStream> values) {
@Test public void testStack20170725() throws IOException { XMLDocumentManager documentManager = Common.client.newXMLDocumentManager(); Transaction transaction = Common.client.openTransaction(); DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle(); documentManager.writeAs("all_well.xml", new FileHandle( new File("../marklogic-client-api-functionaltests/src/test/java/com" + "/marklogic/client/functionaltest/data/all_well.xml"))); InputStreamHandle handle = documentManager.read("all_well.xml", metadataHandle, new InputStreamHandle(), transaction); try { InputStream stream = handle.get(); try { while ( stream.read() > -1 ) {} } finally { stream.close(); } } finally { handle.close(); } // if we ran without throwing any exceptions, then this test passes } }
InputStreamHandle isHandle = new InputStreamHandle(); isHandle.set(inputStream); before = isHandle.toBuffer(); strBefore = new String(before); System.out.println("Before: " + strBefore); isHandle.get(); after = isHandle.toBuffer(); strAfter = new String(after); System.out.println("After: " + strAfter);
@Test public void testWrite_n3_BytesHandle() throws Exception { File file = new File(datasource + "relative7.n3"); FileInputStream fileinputstream = new FileInputStream(file); ByteArrayOutputStream byteoutputstream = new ByteArrayOutputStream(); byte[] buf = new byte[1024]; // Get triples into bytes from file for (int readNum; (readNum = fileinputstream.read(buf)) != -1;) { byteoutputstream.write(buf, 0, readNum); } byte[] bytes = byteoutputstream.toByteArray(); fileinputstream.close(); byteoutputstream.close(); BytesHandle contentHandle = new BytesHandle(); contentHandle.set(bytes); // write triples in bytes to DB gmWriter.write("http://test.n3.com/byte", contentHandle.withMimetype(RDFMimeTypes.N3)); InputStreamHandle read = gmWriter.read("http://test.n3.com/byte", new InputStreamHandle().withMimetype(RDFMimeTypes.N3)); InputStream fileRead = read.get(); String readContent = convertInputStreamToString(fileRead); assertTrue("Did not find expected content after inserting the triples:: Found: " + readContent, readContent.contains("/publications/journals/Journal1/1940")); read.close(); }
@Override public Content adapt(DocumentWriteOperation operation) { String uri = operation.getUri(); ContentCreateOptions options = adaptMetadata(operation.getMetadata()); AbstractWriteHandle handle = operation.getContent(); if (handle instanceof StringHandle) { return ContentFactory.newContent(uri, ((StringHandle) handle).get(), options); } else if (handle instanceof FileHandle) { return ContentFactory.newContent(uri, ((FileHandle) handle).get(), options); } else if (handle instanceof BytesHandle) { return ContentFactory.newContent(uri, ((BytesHandle) handle).get(), options); } else if (handle instanceof InputStreamHandle) { try { return ContentFactory.newContent(uri, ((InputStreamHandle) handle).get(), options); } catch (IOException e) { throw new RuntimeException("Unable to read content input stream: " + e.getMessage(), e); } } else if (handle instanceof DOMHandle) { return ContentFactory.newContent(uri, ((DOMHandle) handle).get(), options); } else throw new IllegalArgumentException("No support yet for content class: " + handle.getClass().getName()); }
assertXMLEqual("InputSource buffering",beforeStr,new String(after)); InputStreamHandle inputStreamH = new InputStreamHandle(); inputStreamH.fromBuffer(before); after = inputStreamH.toBuffer(); assertNotNull("InputStream after",after); assertXMLEqual("InputStream buffering",beforeStr,new String(after));
file1 = new File("src/test/java/com/marklogic/client/functionaltest/data/Sega-4MB.jpg"); fis = new FileInputStream(file1); InputStreamHandle handle1 = new InputStreamHandle(fis); handle1.setFormat(Format.BINARY); docMgr.write("/binary4mbdoc", handle1); String query = "declare variable $myInteger as xs:integer external;" + "(fn:doc()/binary(),$myInteger,xdmp:database-name(xdmp:database()))"; long sizeOfBinary; try ( InputStreamHandle doc =docMgr.read("/binary4mbdoc",new InputStreamHandle()) ) { sizeOfBinary = doc.getByteLength();
/** * Assigns an input stream as the content and returns the handle * as a fluent convenience. * @param content an input stream * @return this handle */ public InputStreamHandle with(InputStream content) { set(content); return this; }
@Override public byte[] toBuffer() { try { if (content == null) return null; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] b = new byte[BUFFER_SIZE]; int len = 0; while ((len = content.read(b)) != -1) { buffer.write(b, 0, len); } content.close(); b = buffer.toByteArray(); fromBuffer(b); return b; } catch (IOException e) { throw new MarkLogicIOException(e); } } /**
/** * Write document using InputStreamHandle * * @param client * @param filename * @param uri * @param type * @throws FileNotFoundException */ public void writeDocumentUsingInputStreamHandle(DatabaseClient client, String filename, String uri, String type) throws FileNotFoundException { // create doc manager DocumentManager docMgr = null; docMgr = documentManagerSelector(client, docMgr, type); // create handle InputStreamHandle contentHandle = new InputStreamHandle(); // get the file InputStream inputStream = new FileInputStream("src/test/java/com/marklogic/client/functionaltest/data/" + filename); // set uri String docId = uri + filename; contentHandle.set(inputStream); // write doc docMgr.write(docId, contentHandle); System.out.println("Write " + docId + " to database"); }
protected void installModules(Map<String, String> modules) { DocumentWriteSet writeSet = modMgr.newWriteSet(); modules.forEach((String path, String localPath) -> { InputStreamHandle handle = new InputStreamHandle(HubTestBase.class.getClassLoader().getResourceAsStream(localPath)); String ext = FilenameUtils.getExtension(path); switch(ext) { case "xml": handle.setFormat(Format.XML); break; case "json": handle.setFormat(Format.JSON); break; default: handle.setFormat(Format.TEXT); } DocumentMetadataHandle permissions = new DocumentMetadataHandle() .withPermission(getHubAdminConfig().getHubRoleName(), DocumentMetadataHandle.Capability.EXECUTE, UPDATE, READ); writeSet.add(path, permissions, handle); }); modMgr.write(writeSet); clearFlowCache(); }
/** * Creates a factory to create an InputStreamHandle instance for an input stream. * @return the factory */ static public ContentHandleFactory newFactory() { return new ContentHandleFactory() { @Override public Class<?>[] getHandledClasses() { return new Class<?>[]{ InputStream.class }; } @Override public boolean isHandled(Class<?> type) { return InputStream.class.isAssignableFrom(type); } @Override public <C> ContentHandle<C> newHandle(Class<C> type) { @SuppressWarnings("unchecked") ContentHandle<C> handle = isHandled(type) ? (ContentHandle<C>) new InputStreamHandle() : null; return handle; } }; }
InputStreamHandle handle = new InputStreamHandle(); Iterator<Triple> triples = null; try { triples = RDFDataMgr.createIteratorTriples(handle.get(), Lang.NTRIPLES, null); } catch (NullPointerException e) { log.info("Got null result from CONSTRUCT, constructing alternate iterator"); ResultSet results = JSONInput.fromJSON(handle.get()); qIter = new QueryIteratorResultSet(results); } else { handle.close(); throw new MarkLogicJenaException("Unrecognized Query Type");