Advertisement
SforzandoCF

no

Mar 8th, 2024
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. public class JSONWriter extends Writer {
  2.     private JSONObject json = null;
  3.     private boolean closed = true;
  4.    
  5.     public JSONWriter () {
  6.         closed = false;
  7.     }
  8.    
  9.     public JSONWriter (String text) throws JSONFormatException {
  10.         json = new JSONObject(text);
  11.         closed = false;
  12.     }
  13.    
  14.     public JSONWriter (File file) throws JSONFormatException {
  15.         String text = "";
  16.         for (String s : Files.readAllLines(file.toPath())) text = text.concat(s);
  17.         json = new JSONObject(text);
  18.         closed = false;
  19.     }
  20.    
  21.     public JSONWriter (File file, boolean overwrite) throws JSONFormatException {
  22.         if (!overwrite) {
  23.             String text = "";
  24.             for (String s : Files.readAllLines(file.toPath())) text = text.concat(s);
  25.         }
  26.         json = new JSONObject(text);
  27.         closed = false;
  28.     }
  29.    
  30.     public void flush () {
  31.         json = null;
  32.     }
  33.    
  34.     public void close () {
  35.         this.flush();
  36.         closed = true;
  37.     }
  38.    
  39.     public void write (char[] buffer) {
  40.         this.write(new String(buffer));
  41.     }
  42.    
  43.     public void write (char[] buffer, int offset, int length) {
  44.         if (offset + length > buffer.length) throw new IllegalArgumentException("Offset and length combined cannot be greater than buffer length.");
  45.         char[] intermediate = new char[length];
  46.         System.arraycopy(buffer, offset, intermediate, 0, length);
  47.         this.write(new String(intermediate));
  48.     }
  49.    
  50.     public void write (int i) {
  51.         this.write(new char[1]{(char) i});
  52.     }
  53.    
  54.     public void write (String buffer) {
  55.         this.write(buffer, 0, buffer.length());
  56.     }
  57.    
  58.     public void write (String buffer, int offset, int length) {
  59.         if (json == null) json = new JSONObject();
  60.         if (offset + length > buffer.length()) throw new IllegalArgumentException("Offset and length combined cannot be greater than buffer length.");
  61.         json.add(buffer.substring(offset, offset + length));
  62.     }
  63.    
  64.     public void addEntry (String key, Object value) {
  65.        
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement