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

40
LINES

< > BotCompany Repo | #1005970 // saveGZStructureToFile

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

Libraryless. Click here for Pure Java version (4002L/25K).

1  
static long saveGZStructureToFile(S file, O o) {
2  
  ret saveGZStructureToFile(getProgramFile(file), o);
3  
}
4  
  
5  
// returns number of uncompressed bytes written
6  
static long saveGZStructureToFile(File file, O o, structure_Data data default new) ctex {
7  
  File parentFile = file.getParentFile();
8  
  if (parentFile != null)
9  
    parentFile.mkdirs();
10  
  File tempFile = tempFileFor(file);
11  
  if (tempFile.exists()) try {
12  
    S saveName = tempFile.getPath() + ".saved." + now();
13  
    copyFile(tempFile, new File(saveName));
14  
  } catch (Throwable e) { printStackTrace(e); }
15  
  
16  
  FileOutputStream fileOutputStream = newFileOutputStream(tempFile.getPath());
17  
  CountingOutputStream cos;
18  
  try {
19  
    GZIPOutputStream gos = new GZIPOutputStream(fileOutputStream);
20  
    cos = new CountingOutputStream(gos);
21  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(cos, "UTF-8");
22  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
23  
    structureToPrintWriter(o, printWriter, data);
24  
    printWriter.close();
25  
    gos.close();
26  
    fileOutputStream.close();
27  
  } catch e {
28  
    fileOutputStream.close();
29  
    tempFile.delete();
30  
    throw rethrow(e);
31  
  }
32  
  
33  
  if (file.exists() && !file.delete())
34  
    throw new IOException("Can't delete " + file.getPath());
35  
36  
  if (!tempFile.renameTo(file))
37  
    throw new IOException("Can't rename " + tempFile + " to " + file);
38  
    
39  
  ret cos.getFilePointer();
40  
}

Author comment

Began life as a copy of #1005539

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1005970
Snippet name: saveGZStructureToFile
Eternal ID of this version: #1005970/8
Text MD5: 82913a82f8106e0b24bc5512bc5b9b86
Transpilation MD5: ff7f58b664d28dfb404e2e8a7641ab0a
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-04-14 16:27:27
Source code size: 1426 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 688 / 800
Version history: 7 change(s)
Referenced in: [show references]