Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

76
LINES

< > BotCompany Repo | #1004128 // fileOpBot

JavaX fragment (include)

1  
static int fileOpBot_maxReadSize = 500000;
2  
static int fileOpBot_maxWriteSize = 500000;
3  
4  
static S fileOpBot(S s) ctex {
5  
  new Matches m;
6  
  
7  
  if "get user home"
8  
    ret ok(userHome());
9  
  
10  
  if "list directory *"
11  
    ret structure(listDirAwarenessStyle(new File(assertAbsolutePath(m.unq(0)))));
12  
    
13  
  if "mkdir *" {
14  
    S dir = assertAbsolutePath(m.unq(0));
15  
    mkdir(new File(dir));
16  
    ret new File(dir).exists() ? "ok" : "problem";
17  
  }
18  
  
19  
  if (matchOneOf(s, m, "rmdir *", "delete file *")) {
20  
    S dir = assertAbsolutePath(m.unq(0));
21  
    new File(dir).delete();
22  
    ret !new File(dir).exists() ? "ok" : "problem";
23  
  }
24  
  
25  
  if "load file * bin from * l *" {
26  
    File f = new File(assertAbsolutePath(m.unq(0)));
27  
    long from = parseLong(m.unq(1));
28  
    int l = parseInt(m.unq(2));
29  
    if (l > fileOpBot_maxReadSize)
30  
      fail(format("Can't read more than * bytes per call", fileOpBot_maxReadSize));
31  
    byte[] buf = new byte[l];
32  
    RandomAccessFile raf = new RandomAccessFile(f, "r");
33  
    try {
34  
      raf.seek(from);
35  
      raf.readFully(buf);
36  
      ret "OK " + bytesToHex(buf);
37  
    } finally {
38  
      raf.close();
39  
    }
40  
  }
41  
  
42  
  if "get file * length" {
43  
    File f = new File(assertAbsolutePath(m.unq(0)));
44  
    ret str(f.length());
45  
  }
46  
  
47  
  if "set file * length to *" {
48  
    File f = new File(assertAbsolutePath(m.unq(0)));
49  
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
50  
    try {
51  
      raf.setLength(parseLong(m.unq(1)));
52  
      ret "OK";
53  
    } finally {
54  
      raf.close();
55  
    }
56  
  }
57  
    
58  
  if "write file * bin from * data *" {
59  
    print(shorten(s, 200));
60  
    File f = new File(assertAbsolutePath(m.unq(0)));
61  
    long from = parseLong(m.unq(1));
62  
    byte[] data = hexToBytes(m.unq(2));
63  
    if (l(data) > fileOpBot_maxWriteSize)
64  
      fail(format("Can't write more than * bytes per call", fileOpBot_maxWriteSize));
65  
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
66  
    try {
67  
      raf.seek(from);
68  
      raf.write(data);
69  
      ret "OK";
70  
    } finally {
71  
      raf.close();
72  
    }
73  
  }
74  
  
75  
  null;
76  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1004128
Snippet name: fileOpBot
Eternal ID of this version: #1004128/1
Text MD5: 98e354fa07f89a6f4c9fbe0cd8198c5c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-01-12 03:17:51
Source code size: 2089 bytes / 76 lines
Pitched / IR pitched: No / No
Views / Downloads: 583 / 906
Referenced in: [show references]