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).

srecord noeq PicturesByMD5(File dir) {
  settable S extension = ".jpeg";
  transient Lock lock = lock();
  
  // current count of images (calculated lazily)
  transient new VarWithNotify<Int> imageCount;
  
  File imageFile(S md5) {
    ret newFile(dir, assertMD5(md5) + extension);
  }
  
  bool has(S md5) { ret isFile(imageFile(md5)); }
  
  BufferedImage get(S md5) {
    ret decodeImage(imageFile(md5));
  }
  
  swappable BufferedImage decodeImage(File f) {
    ret loadImage2(f);
  }
  
  bool put(S md5, byte[] imageData) {
    // double-checked locking, yay!
    if (has(md5)) false;
    lock lock;
    if (has(md5)) false;
    saveBinaryFile(imageFile(md5), imageData);
    if (imageCount.has()) imageCount.set(imageCount()+1);
    true;
  }
  
  // -actually scans the directory
  // -only sees files with the correct extension
  int freshImageCount() {
    lock lock;
    ret setAndReturn(imageCount, l(freshImageFiles()));
  }
  
  L<File> freshImageFiles() {
    ret listFilesWithExtension(dir, extension);
  }
  
  // get current image count in an efficient way
  // -does not notice when other objects/processes
  //  add or remove images in the directory
  int imageCount() {
    lock lock;
    if (!imageCount.has())
      freshImageCount();
    ret imageCount!;
  }
  
  IVarWithNotify varImageCount() { ret imageCount; }
}

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: 162 / 271
Version history: 11 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)