/** * Create a new Encoder with the state and appropriate API. * * @return an Encoder */ public Encoder enc() { return new Encoder(this); }
@Override void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception { Iterable< ? > collection = (Iterable< ? >) object; app.append("["); String del = ""; for (Object o : collection) { app.append(del); app.encode(o, componentType, visited); del = ","; } app.append("]"); }
@Override public void encode(Encoder app, Object object, Map<Object, Type> visited) throws IOException, Exception { // Byte arrays should not be treated as arrays. We treat them // as hex strings if (object instanceof byte[]) { StringHandler.string(app, Hex.toHexString((byte[]) object)); return; } app.append("["); app.indent(); String del = ""; int l = Array.getLength(object); for (int i = 0; i < l; i++) try { app.append(del); app.encode(Array.get(object, i), componentType, visited); del = ","; } catch (Exception e) { throw new IllegalArgumentException("[" + i + "]", e); } app.undent(); app.append("]"); }
@Override void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception { Map< ? , ? > map = (Map< ? , ? >) object; app.append("{"); String del = ""; for (Map.Entry< ? , ? > e : map.entrySet()) { app.append(del); String key; if (e.getKey() != null && (keyType == String.class || keyType == Object.class)) key = e.getKey().toString(); else { key = app.codec.enc().put(e.getKey()).toString(); } StringHandler.string(app, key); app.append(":"); app.encode(e.getValue(), valueType, visited); del = ","; } app.append("}"); }
public void save() { if (!this.where.getParentFile().isDirectory() && !this.where.getParentFile().mkdirs()) throw new RuntimeException("Cannot create directory in " + this.where.getParent()); try { codec.enc().to(this.where).put(data).flush(); assert this.where.isFile(); } catch (Exception e) { throw new RuntimeException("Cannot write settings file " + this.where, e); } }
void write(Formatter format) throws Exception { Collections.sort(descriptors); date = System.currentTimeMillis(); format.format(// "{\n\"version\" :%s,\n" // + "\"descriptors\" : [\n", version, increment, date); String del = ""; for (ResourceDescriptorImpl rd : descriptors) { format.format(del); format.flush(); codec.enc().to(format.out()).keepOpen().put(rd); del = ",\n"; } format.format("\n]}\n"); format.flush(); } }
public byte[] digest() throws NoSuchAlgorithmException, IOException { if (digest == null) return null; append('\n'); return digest.digest(); }
private void index(HttpServletResponse rsp) throws Exception { Bundle b = context.getBundle(); URL url = cache.internalUrlOf(b, "osgi/enroute/web/index.html"); CacheFile c = cache.getFromBundle(b, url, "osgi/enroute/web/index.html"); if (c == null || c.is404 || c.isNotFound()) { url = cache.internalUrlOf(b, "osgi/enroute/web/local/index.html"); c = cache.getFromBundle(b, url, "osgi/enroute/web/local/index.html"); } String content = IO.collect(c.file); Map<String,String> map = new HashMap<>(); synchronized (index) { map.put("index", new JSONCodec().enc().put(index).indent(" ").toString()); } ReplacerAdapter ra = new ReplacerAdapter(map); content = ra.process(content); IO.store(content, rsp.getOutputStream()); }
public Encoder to(OutputStream out) throws IOException { if (deflate) out = new DeflaterOutputStream(out); return to(new OutputStreamWriter(out, encoding)); }
@Override public Enc pretty() { enc.indent("\t"); return this; }
@Override void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception { // Byte arrays should not be treated as arrays. We treat them // as hex strings if ( object instanceof byte[]) { StringHandler.string(app, Hex.toHexString((byte[]) object)); return; } app.append("["); app.indent(); String del = ""; int l = Array.getLength(object); for (int i = 0; i < l; i++) try { app.append(del); app.encode(Array.get(object, i), componentType, visited); del = ","; } catch( Exception e) { throw new IllegalArgumentException("[" + i + "]", e); } app.undent(); app.append("]"); }
@Override void encode(Encoder app, Object object, Map<Object,Type> visited) throws IOException, Exception { Map< ? , ? > map = (Map< ? , ? >) object; app.append("{"); String del = ""; for (Map.Entry< ? , ? > e : map.entrySet()) try { app.append(del); String key; if (e.getKey() != null && (keyType == String.class || keyType == Object.class)) key = e.getKey().toString(); else { key = app.codec.enc().put(e.getKey()).toString(); } StringHandler.string(app, key); app.append(":"); app.encode(e.getValue(), valueType, visited); del = ","; } catch( Exception ee) { throw new IllegalArgumentException("[\"" + e.getKey() +"\"]", ee); } app.append("}"); }
private void doOutput(Object put, final URLConnection con, HttpRequest<?> rq) throws IOException, Exception { con.setDoOutput(true); try (OutputStream out = con.getOutputStream()) { if (put instanceof InputStream) { logger.debug("out {} input stream {}", rq.verb, rq.url); IO.copy((InputStream) put, out); } else if (put instanceof String) { logger.debug("out {} string {}", rq.verb, rq.url); IO.store(put, out); } else if (put instanceof byte[]) { logger.debug("out {} byte[] {}", rq.verb, rq.url); IO.copy((byte[]) put, out); } else if (put instanceof File) { logger.debug("out {} file {} {}", rq.verb, put, rq.url); IO.copy((File) put, out); } else { logger.debug("out {} JSON {} {}", rq.verb, put, rq.url); codec.enc() .to(out) .put(put) .flush(); } } }
void write(Formatter format) throws Exception { Collections.sort(descriptors); date = System.currentTimeMillis(); format.format(// "{\n\"version\" :%s,\n" // + "\"descriptors\" : [\n", version, increment, date); String del = ""; for (ResourceDescriptorImpl rd : descriptors) { format.format(del); format.flush(); codec.enc().to(format.out()).keepOpen().put(rd); del = ",\n"; } format.format("\n]}\n"); format.flush(); } }