org.apache.jena.atlas.io.IO Java Examples

The following examples show how to use org.apache.jena.atlas.io.IO. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DeltaClientLib.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
/** Connect to a destination for changes */
public static RDFChanges destination(String dest) {
    // TODO text vs binary
    if ( dest.startsWith("file:") ) {
        OutputStream out = IO.openOutputFile(dest) ;
        TokenWriter tokenWriter = new TokenWriterText(out) ;
        RDFChanges sc = new RDFChangesWriter(tokenWriter) ;
        return sc ;
    }

    if ( dest.startsWith("delta:") ) { // TCP connection delta:HOST:PORT
        throw new NotImplemented(dest) ;
    }

    if ( dest.startsWith("http:") || dest.startsWith("https:") ) {
        // Triggered on each transaction.
        return new RDFChangesHTTP(dest, dest) ;
    }
    throw new IllegalArgumentException("Not understood: "+dest) ;
}
 
Example #2
Source File: OutputManagedFile.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
private void nextFile() {
        try {
            currentFilename = roller.nextFilename();
            FmtLog.debug(LOG, "Setup: %s", currentFilename);
            // Must be a FileOutputStream so that getFD().sync is available.
            fileOutput = new FileOutputStream(currentFilename.toString(), true);
            output = new BufferedOutputStream(fileOutput);
            //[gz]
        } catch (FileNotFoundException ex) {
            IO.exception(ex);
            return;
//        } catch (IOException ex) {
//            IO.exception(ex);
//            return;
        }
    }
 
Example #3
Source File: TestRDFChangesGraph.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
private Graph replay() {
    IO.close(bout);
    final boolean DEBUG = false;

    if ( DEBUG ) {
        System.out.println("== Graph ==");
        RDFDataMgr.write(System.out, baseGraph, Lang.NQ);
        System.out.println("== Replay ==");
        String x = StrUtils.fromUTF8bytes(bout.toByteArray());
        System.out.print(x);
        System.out.println("== ==");
    }

    // A completely separate graph (different dataset)
    Graph graph2 = txnGraph();

    try(ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray())) {
        RDFPatchOps.applyChange(graph2, bin);
        if ( DEBUG ) {
            System.out.println("== Graph outcome ==");
            RDFDataMgr.write(System.out, graph2, Lang.NT);
            System.out.println("== ==");
        }
        return graph2;
    } catch (IOException ex) { IO.exception(ex); return null; }
}
 
Example #4
Source File: CmdPatch.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Override
protected void exec() {
    execStart();
    try {
        // Patches
        if ( getPositional().isEmpty() )
            execOne("Stdin", System.in);
        else {
            getPositional().forEach(fn->{
                try ( InputStream in = IO.openFile(fn) ) {
                    execOne(fn, in);
                } catch (IOException ex) { IO.exception(ex); return; }
            });
        }
    } finally { execFinish(); }
}
 
Example #5
Source File: patch2update.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Override
protected void exec() {
    AWriter out = IO.wrapUTF8(System.out);

    // Generalize to abstract class for any "apply" - patch2rdf
    RDFChanges c =  new RDFChangesWriteUpdate(out);

    // Patches
    if ( getPositional().isEmpty() )
        execOne(System.in);

    getPositional().forEach(fn->{
        // Check patch threading?
        RDFPatch patch = RDFPatchOps.read(fn);
        if ( isVerbose() ) {
            System.err.printf("# Patch id=%s", Id.str(patch.getId()));
            if ( patch.getPrevious() != null )
                System.err.printf(" prev=%s", Id.str(patch.getPrevious()));
            System.err.println();
        }
        patch.apply(c);
    });
    out.flush();

}
 
Example #6
Source File: TestFileStore.java    From rdf-delta with Apache License 2.0 6 votes vote down vote up
@Test
public void fs_write_01() throws IOException {
    FileStore fs = FileStore.attach(STORE, "FILE");
    assertEquals(0, fs.getCurrentIndex());
    FileEntry entry = fs.writeNewFile(out->{
        try(AWriter aw = IO .wrapUTF8(out)) {
          aw.write("abc");
        }
    }) ;
    assertNotNull(entry);
    assertNotNull(entry.datafile);
    int idx = checkFilename(entry.datafile);
    assertEquals(1, idx);
    // Read it back in again.
    String s = FileUtils.readWholeFileAsUTF8(entry.getDatafileName());
    assertEquals("abc", s);
}
 
Example #7
Source File: PersistentCounter.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
/** Write a string to a file as UTF-8. The file is closed after the operation.
 * @param filename
 * @param content String to be writtem
 * @throws IOException
 */

private /*public*/ static void writeStringAsUTF8(String filename, String content) throws IOException {
    try ( OutputStream out = IO.openOutputFileEx(filename) ) {
        writeStringAsUTF8(out, content) ;
        out.flush() ;
    }
}
 
Example #8
Source File: OutputManagedFile.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
private void closeOutput() {
    if ( output == null )
        return;
    flushOutput();
    IO.close(output);
    //[gz] Compress currentFilename.
    output = null;
    fileOutput = null;
    // keep: currentFilename
    currentOutput = null;
}
 
Example #9
Source File: OutputFixed.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
private void finish() {
    try {
        outputStream.flush();
        currentOutput = null;
    }
    catch (IOException e) {
        IO.exception(e); return;
    }
    if ( sema.availablePermits() == 0 )
        sema.release();
}
 
Example #10
Source File: TestRDFChangesDataset.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
private DatasetGraph replay() {
    IO.close(bout);
    try(ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray())) {
        DatasetGraph dsg2 = DatasetGraphFactory.createTxnMem();
        RDFPatchOps.applyChange(dsg2, bin);
        return dsg2;
    } catch (IOException ex) { IO.exception(ex); return null; }
}
 
Example #11
Source File: TestRotate.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
/** "touch" - without metadata */
private static void touch(String filename) {
    Path p = DIR.resolve(filename);
    if ( ! Files.exists(p) ) {
        try ( OutputStream out = new FileOutputStream(p.toString()) ) {}
        catch (IOException ex) {
            IO.exception(ex);
        }
    }
}
 
Example #12
Source File: LibX.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
/** Copy the contents of an {@link InputStream} so it can be closed. */
public static InputStream copy(InputStream inputStream) {
    if ( inputStream == null )
        return null ;
    byte[] b = IO.readWholeFile(inputStream) ;
    InputStream x = new ByteArrayInputStream(b) ;
    return x ;
}
 
Example #13
Source File: Setup.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
/** Set the HttpClient - close the old one if appropriate */
/*package*/ static void setHttpClient(HttpClient newHttpClient) {
    HttpClient hc = HttpOp.getDefaultHttpClient() ;
    if ( hc instanceof CloseableHttpClient )
        IO.close((CloseableHttpClient)hc) ;
    HttpOp.setDefaultHttpClient(newHttpClient) ;

}
 
Example #14
Source File: TestDeltaFusekiGood.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void afterClass() {
    if ( server1 != null )
        server1.stop();
    if ( server2 != null )
        server2.stop();
    deltaServer.stop();
    IO.close( ((CloseableHttpClient)HttpOp.getDefaultHttpClient()) );
    HttpOp.setDefaultHttpClient(dftStdHttpClient);
}
 
Example #15
Source File: RDFChangesHTTP.java    From rdf-delta with Apache License 2.0 5 votes vote down vote up
private static String readResponse(HttpResponse resp) {
    HttpEntity e = resp.getEntity();
    if ( e != null ) {
        try ( InputStream ins = e.getContent() ) {
            return IO.readWholeFileAsUTF8(ins);
        } catch (IOException ex) { ex.printStackTrace(); }
    }
    return null;
}
 
Example #16
Source File: LocatorFileAccept.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
/**
 * Open anything that looks a bit like a file name
 */
@Override
public TypedInputStream open(LookUpRequest request) {
    String filenameIRI = request.getFilenameOrURI();
    if(filenameIRI.startsWith("http") || filenameIRI.startsWith("coap")) {
        return null;
    }
    String fn = toFileName(filenameIRI);
    if (fn == null) {
        log.debug("Cannot find a proper filename: " + filenameIRI);
        return null;
    }

    try {
        if (!exists$(fn)) {
            return null;
        }
    } catch (AccessControlException e) {
        log.debug("Security problem testing for file", e);
        return null;
    }

    try {
        InputStream in = IO.openFileEx(fn);
        try {
            String ct = Files.probeContentType(Paths.get(new File(filenameIRI).getName()));
            return new TypedInputStream(in, ContentType.create(ct), filenameIRI);
        } catch (Exception ex) {
            return new TypedInputStream(in, (ContentType) null, filenameIRI);
        } 
    } catch (IOException ioEx) {
        // Includes FileNotFoundException
        // We already tested whether the file exists or not.
        log.debug("File unreadable (but exists): " + fn + " Exception: " + ioEx.getMessage());
        return null;
    }
}
 
Example #17
Source File: TokenWriterText.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
public TokenWriterText(String label, NodeFormatter formatter, Writer out) {
    this(label, formatter, IO.wrap(out));
}
 
Example #18
Source File: RDFChangesWriterBinary.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
public static void write(RDFPatch patch, String filename) {
    try ( OutputStream out = IO.openOutputFile(filename) ) {
        write(patch, out);
    } catch (IOException ex) { IO.exception(ex); }
}
 
Example #19
Source File: RDFChangesManagedOutput.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
private void finishOutput() {
    super.tok.flush();
    IO.close(currentStream);
    currentStream = null;
    super.tok = null;
}
 
Example #20
Source File: TokenWriterText.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    if ( inTuple ) {}
    IO.close(out);
}
 
Example #21
Source File: TokenizerText.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IO.close(reader);
}
 
Example #22
Source File: TokenWriterText.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
private static Writer writer(OutputStream out) {
    // IO.wrap(out) -- need buffering version,
    Writer w1 = IO.asBufferedUTF8(out);
    Writer w2 = new BufferingWriter(w1, 1024 * 1024);
    return w2;
}
 
Example #23
Source File: RDFPatchOps.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
/** Read an {@link RDFPatch} from a file. */
public static RDFPatch readBinary(String filename) {
    try ( InputStream input = IO.openFile(filename) ) {
        return readBinary(input);
    } catch (IOException ex) { IO.exception(ex); return null; }
}
 
Example #24
Source File: RDFPatchOps.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
/** Read an {@link RDFPatch} from a file. */
public static RDFPatch read(String filename) {
    try ( InputStream input = IO.openFile(filename) ) {
        return read(input);
    } catch (IOException ex) { IO.exception(ex); return null; }
}
 
Example #25
Source File: RDFPatchOps.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
/** Read an {@link RDFPatch} from a file. */
public static RDFPatch fileToPatch(String filename) {
    InputStream in = IO.openFile(filename);
    return read(in);
}
 
Example #26
Source File: TestDeltaFusekiBad.java    From rdf-delta with Apache License 2.0 4 votes vote down vote up
@After
public void after() {
    IO.close( ((CloseableHttpClient)HttpOp.getDefaultHttpClient()) );
}
 
Example #27
Source File: SinkTripleStarOutput.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Override
public void close() {
    IO.flush(writer);
}
 
Example #28
Source File: SinkTripleStarOutput.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
@Override
public void flush() {
    IO.flush(writer);
}
 
Example #29
Source File: SinkTripleStarOutput.java    From RDFstarTools with Apache License 2.0 4 votes vote down vote up
public SinkTripleStarOutput(OutputStream out, String baseIRI, PrefixMap prefixMap, NodeToLabel labels) {
	writer = IO.wrapUTF8(out);
    labelPolicy = labels;        
    nodeFmt = new NodeFormatterTurtleStarExtImpl(baseIRI, prefixMap, labels);
}
 
Example #30
Source File: FileEntry.java    From rdf-delta with Apache License 2.0 3 votes vote down vote up
/**
 * Complete the write process: closes the OutputStream allocated by
 * {@link #openForWrite} thenn
 *
 * <p>
 * Note that {@code Outstream.close} is idempotent - it is safe
 * for the application to close the {@code Outstream}.
 * <p>
 * The application must flush any buffered output prior to calling {@code completeWrite}.
 */
public void completeWrite() {
    IO.close(out);
    IOX.move(tmpfile, datafile);
    haveWritten = true;
    out = null ;
}