private byte[] readLocation() { if ( pathname == null ) return null; return IOX.readAll(pathname) ; }
public PatchStoreFile(Path location, PatchStoreProvider provider) { super(provider); IOX.ensureDirectory(location); this.serverRoot = location; }
/** Write a file safely - the change happens (the function returns true) or * somthing went wrong (the function throws a runtime exception) and the file is not changed. * Note that the tempfile must be in the same direct as the actual file so an OS-atomic rename can be done. */ public static boolean safeWrite(Path file, Path tmpFile, IOConsumer<OutputStream> writerAction) { try { try(OutputStream out = new BufferedOutputStream(Files.newOutputStream(tmpFile)) ) { writerAction.actionEx(out); } move(tmpFile, file); return true; } catch(IOException ex) { throw IOX.exception(ex); } }
/** Read the whole of a file */ public static byte[] readAll(Path pathname) { try { return Files.readAllBytes(pathname); } catch (IOException ex) { throw IOX.exception(ex); } }
private Id getIdOrNull(JsonObject obj, String field) { String s = JSONX.getStrOrNull(obj, field); if ( s == null ) return null; return Id.fromString(s); }
/** Access a field of a JSON object, return an {@code int} or a default value. */ public static int getInt(JsonObject obj, String field, int dftValue) { JsonValue jv = obj.get(field); if ( jv == null ) return dftValue; return getInt(jv, dftValue); }
private void writeLocation() { if ( pathname == null ) return; // Does not need synchronizing. IOX.safeWrite(pathname, out->out.write(value)); } }
public static boolean exactlyOneNotNull(Object ... objects) { return countNonNulls(objects) == 1; }
/** Create and set a new zNode: the zNode must not exist before this operation. */ public static void zkCreateSetJson(CuratorFramework client, String statePath, JsonObject x) { byte[] bytes = JSONX.asBytes(x); zkCreateSet(client, statePath, bytes); }
public static boolean exactNumNull(int N, Object... objs) { return N == countNull(objs); }
public static <T> ListIterator<T> reverse(ListIterator<T> iter) { return new ListIteratorReverse<T>(iter) ; }
public static RuntimeException wrap(Throwable th) { return new WrappedException(th); } private static class WrappedException extends RuntimeException {
public static byte[] asBytes(JsonValue jsonValue) { ByteArrayOutputStream out = new ByteArrayOutputStream(); writeFlat(out, jsonValue); return out.toByteArray(); }
public static JsonObject zkFetchJson(CuratorFramework client, Watcher watcher, String path) { byte[] x = zkFetch(client, watcher, path); if ( x == null ) return null; if ( x.length == 0 ) return null; return JSONX.fromBytes(x); }
/** Create a safe copy of a {@link JsonValue}. * <p> * If the JsonValue is a structure (object or array), copy the structure recursively. * <p> * If the JsonValue is a primitive (string, number, boolean or null), * it is immutable so return the same object. */ public static JsonValue copy(JsonValue arg) { JsonBuilder builder = builder(arg) ; return builder==null ? arg : builder.build() ; }
/** * Return a temporary filename path. * <p> * This operation is thread-safe. */ public static Path createTempFile(Path dir, String prefix, String suffix, FileAttribute<? >... attrs) { try { return Files.createTempFile(dir, prefix, suffix, attrs); } catch (IOException ex) { throw IOX.exception(ex); } }
/** Atomically write the file */ public void write(IOConsumer<OutputStream> action) { if ( haveWritten ) throw new RuntimeIOException("FileEntry has already been written: "+datafile); IOX.safeWrite(datafile, tmpfile, action); haveWritten = true; }
public static boolean allNonNull(Object ... objects) { return countNonNulls(objects) == objects.length; }
/** Set an existing zNode to the the bytes for the JSON object */ public static void zkSetJson(CuratorFramework client, String statePath, JsonObject x) { byte[] bytes = JSONX.asBytes(x); zkSet(client, statePath, bytes); }