Direct-BT v3.3.0-1-gc2d430c
Direct-BT - Direct Bluetooth Programming.
DBTUtils.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
25package trial.org.direct_bt;
26
27import java.io.File;
28import java.nio.file.Files;
29
30import org.direct_bt.BTFactory;
31import org.jau.io.PrintUtil;
32
33public class DBTUtils {
34 public static final boolean mkdirKeyFolder() {
36 boolean res = true;
37 {
38 final File file = new File(DBTConstants.CLIENT_KEY_PATH);
39 try {
40 if( !file.isDirectory() ) {
41 final boolean res2 = file.mkdirs();
42 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': mkdir: "+res2);
43 res = res && res2;
44 } else {
45 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': already exists");
46 }
47 } catch(final Throwable t) {
48 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': Caught "+t.getMessage());
49 res = false;
50 }
51 }
52 if( res ) {
53 final File file = new File(DBTConstants.SERVER_KEY_PATH);
54 try {
55 if( !file.isDirectory() ) {
56 final boolean res2 = file.mkdirs();
57 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': mkdir: "+res2);
58 res = res && res2;
59 } else {
60 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': already exists");
61 }
62 } catch(final Throwable t) {
63 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': Caught "+t.getMessage());
64 res = false;
65 }
66 }
67 return res;
68 }
69
70 /**
71 *
72 * @param file
73 * @param recursive
74 * @return true only if the file or the directory with content has been deleted, otherwise false
75 */
76 private static boolean delete(final File file, final boolean recursive) {
77 boolean rm_parent = true;
78 final File[] contents = file.listFiles();
79 if (contents != null) {
80 for (final File f : contents) {
81 if( f.isDirectory() && !Files.isSymbolicLink( f.toPath() ) ) {
82 if( recursive ) {
83 rm_parent = delete(f, true) && rm_parent;
84 } else {
85 // can't empty contents -> can't rm 'file'
86 rm_parent = false;
87 }
88 } else {
89 try {
90 rm_parent = f.delete() && rm_parent;
91 } catch( final Exception e ) {
92 e.printStackTrace();
93 rm_parent = false;
94 }
95 }
96 }
97 }
98 if( rm_parent ) {
99 try {
100 return file.delete();
101 } catch( final Exception e ) { e.printStackTrace(); }
102 }
103 return false;
104 }
105
106 public static final boolean rmKeyFolder() {
108 boolean res = true;
109 {
110 final File file = new File(DBTConstants.CLIENT_KEY_PATH);
111 try {
112 if( file.isDirectory() ) {
113 final boolean res2 = delete(file, false /* recursive */);
114 res = res2 && res;
115 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': delete: "+res2);
116 }
117 } catch(final Throwable t) {
118 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': Caught "+t.getMessage());
119 res = false;
120 }
121 }
122 if( res ) {
123 final File file = new File(DBTConstants.SERVER_KEY_PATH);
124 try {
125 if( file.isDirectory() ) {
126 final boolean res2 = delete(file, false /* recursive */);
127 res = res2 && res;
128 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': delete: "+res2);
129 }
130 } catch(final Throwable t) {
131 PrintUtil.println(System.err, "****** KEY_PATH '"+file.toString()+"': Caught "+t.getMessage());
132 res = false;
133 }
134 }
135 return res;
136 }
137}
One stop BTManager API entry point.
Definition: BTFactory.java:52
static synchronized void initDirectBTLibrary()
Preloads the DirectBT native library w/o instantiating BTManager.
Definition: BTFactory.java:482
static final String SERVER_KEY_PATH
static final String CLIENT_KEY_PATH
static final boolean mkdirKeyFolder()
Definition: DBTUtils.java:34
static final boolean rmKeyFolder()
Definition: DBTUtils.java:106