/** * Stores the specified source to the specified file. * * @param in input source * @param file target file * @throws IOException I/O exception */ public static void store(final InputSource in, final IOFile file) throws IOException { // add directory if it does not exist anyway file.dir().md(); final PrintOutput po = new PrintOutput(file.path()); try { final Reader r = in.getCharacterStream(); final InputStream is = in.getByteStream(); final String id = in.getSystemId(); if (r != null) { for (int c; (c = r.read()) != -1; ) po.utf8(c); } else if (is != null) { for (int b; (b = is.read()) != -1; ) po.write(b); } else if (id != null) { final BufferInput bi = new BufferInput(IO.get(id)); try { for (int b; (b = bi.read()) != -1; ) po.write(b); } finally { bi.close(); } } } finally { po.close(); } }
@Override protected boolean run() throws IOException { final String path = MetaData.normPath(args[0]); if (path == null) return error(NAME_INVALID_X, args[0]); final IOFile bin = context.data().meta.binary(path); if (bin == null || !bin.exists() || bin.isDir()) return error(RES_NOT_FOUND_X, path); try { final BufferInput bi = new BufferInput(bin); try { for (int b; (b = bi.read()) != -1; ) out.write(b); } finally { bi.close(); } return info(QUERY_EXECUTED_X, perf); } catch (final IOException ex) { return error(FILE_NOT_STORED_X, ex); } }
/** * Checks the next success flag. * * @param bi buffer input * @return value of check * @throws IOException I/O exception */ protected static boolean ok(final BufferInput bi) throws IOException { return bi.read() == 0; }