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

54
LINES

< > BotCompany Repo | #1035889 // PicturesByMD5 (on disk)

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (11648L/66K).

1  
srecord noeq PicturesByMD5(File dir) {
2  
  settable S extension = ".jpeg";
3  
  transient Lock lock = lock();
4  
  
5  
  // current count of images (calculated lazily)
6  
  transient new VarWithNotify<Int> imageCount;
7  
  
8  
  File imageFile(S md5) {
9  
    ret newFile(dir, assertMD5(md5) + extension);
10  
  }
11  
  
12  
  bool has(S md5) { ret isFile(imageFile(md5)); }
13  
  
14  
  BufferedImage get(S md5) {
15  
    ret decodeImage(imageFile(md5));
16  
  }
17  
  
18  
  swappable BufferedImage decodeImage(File f) {
19  
    ret loadImage2(f);
20  
  }
21  
  
22  
  bool put(S md5, byte[] imageData) {
23  
    // double-checked locking, yay!
24  
    if (has(md5)) false;
25  
    lock lock;
26  
    if (has(md5)) false;
27  
    saveBinaryFile(imageFile(md5), imageData);
28  
    if (imageCount.has()) imageCount.set(imageCount()+1);
29  
    true;
30  
  }
31  
  
32  
  // -actually scans the directory
33  
  // -only sees files with the correct extension
34  
  int freshImageCount() {
35  
    lock lock;
36  
    ret setAndReturn(imageCount, l(freshImageFiles()));
37  
  }
38  
  
39  
  L<File> freshImageFiles() {
40  
    ret listFilesWithExtension(dir, extension);
41  
  }
42  
  
43  
  // get current image count in an efficient way
44  
  // -does not notice when other objects/processes
45  
  //  add or remove images in the directory
46  
  int imageCount() {
47  
    lock lock;
48  
    if (!imageCount.has())
49  
      freshImageCount();
50  
    ret imageCount!;
51  
  }
52  
  
53  
  IVarWithNotify varImageCount() { ret imageCount; }
54  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1035889
Snippet name: PicturesByMD5 (on disk)
Eternal ID of this version: #1035889/12
Text MD5: 906886b1dbae687b778b2344d85180e0
Transpilation MD5: a595f91d01e85eb214c8a21fa8b0eccc
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-21 07:27:06
Source code size: 1395 bytes / 54 lines
Pitched / IR pitched: No / No
Views / Downloads: 163 / 272
Version history: 11 change(s)
Referenced in: [show references]