blob: 1b58c0f10752a73948b3a923734f28b7df30f99e [file] [log] [blame]
Bharat saraswal97459962016-02-20 21:57:16 +05301/*
Brian O'Connor0f7908b2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Bharat saraswal97459962016-02-20 21:57:16 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.utils.io.impl;
18
19import java.io.BufferedReader;
20import java.io.File;
21import java.io.FileReader;
22import java.io.FileWriter;
23import java.io.IOException;
24import java.io.PrintWriter;
Bharat saraswal715d3fc2016-05-17 19:59:16 +053025import java.util.ArrayList;
26import java.util.List;
Bharat saraswal97459962016-02-20 21:57:16 +053027
Bharat saraswal84366c52016-03-23 19:40:35 +053028import static org.onosproject.yangutils.utils.UtilConstants.EIGHT_SPACE_INDENTATION;
29import static org.onosproject.yangutils.utils.UtilConstants.EMPTY_STRING;
30import static org.onosproject.yangutils.utils.UtilConstants.FOUR_SPACE_INDENTATION;
31import static org.onosproject.yangutils.utils.UtilConstants.MULTIPLE_NEW_LINE;
32import static org.onosproject.yangutils.utils.UtilConstants.NEW_LINE;
Bharat saraswal84366c52016-03-23 19:40:35 +053033import static org.onosproject.yangutils.utils.UtilConstants.SPACE;
Bharat saraswal97459962016-02-20 21:57:16 +053034
35/**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053036 * Represents utility to handle file system operations.
Bharat saraswal97459962016-02-20 21:57:16 +053037 */
38public final class FileSystemUtil {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053039
Bharat saraswal97459962016-02-20 21:57:16 +053040 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +053041 * Creates an instance of file system util.
Bharat saraswal97459962016-02-20 21:57:16 +053042 */
43 private FileSystemUtil() {
44 }
45
46 /**
Bharat saraswal8beac342016-08-04 02:00:03 +053047 * Reads the contents from source file and append its contents to append file.
Bharat saraswal97459962016-02-20 21:57:16 +053048 *
Bharat saraswal8beac342016-08-04 02:00:03 +053049 * @param toAppend destination file in which the contents of source file is appended
50 * @param srcFile source file from which data is read and added to to append file
Vinod Kumar S08710982016-03-03 19:55:30 +053051 * @throws IOException any IO errors
Bharat saraswal97459962016-02-20 21:57:16 +053052 */
Bharat saraswal8beac342016-08-04 02:00:03 +053053 static void appendFileContents(File toAppend, File srcFile)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053054 throws IOException {
Bharat saraswal84366c52016-03-23 19:40:35 +053055 updateFileHandle(srcFile, NEW_LINE + readAppendFile(toAppend.toString(), FOUR_SPACE_INDENTATION), false);
Bharat saraswal97459962016-02-20 21:57:16 +053056 }
57
58 /**
59 * Reads file and convert it to string.
60 *
Vinod Kumar S08710982016-03-03 19:55:30 +053061 * @param toAppend file to be converted
Gaurav Agrawalbfce9342016-06-15 13:58:01 +053062 * @param spaces spaces to be appended
Vinod Kumar S08710982016-03-03 19:55:30 +053063 * @return string of file
Bharat saraswal97459962016-02-20 21:57:16 +053064 * @throws IOException when fails to convert to string
65 */
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +053066 public static String readAppendFile(String toAppend, String spaces)
67 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +053068
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053069 FileReader fileReader = new FileReader(toAppend);
70 BufferedReader bufferReader = new BufferedReader(fileReader);
Bharat saraswal97459962016-02-20 21:57:16 +053071 try {
72 StringBuilder stringBuilder = new StringBuilder();
73 String line = bufferReader.readLine();
74
75 while (line != null) {
Bharat saraswal8beac342016-08-04 02:00:03 +053076 switch (line) {
77 case SPACE:
78 case EMPTY_STRING:
79 case EIGHT_SPACE_INDENTATION:
80 case MULTIPLE_NEW_LINE:
81 stringBuilder.append(NEW_LINE);
82 break;
83 case FOUR_SPACE_INDENTATION:
84 stringBuilder.append(EMPTY_STRING);
85 break;
86 default:
87 String append = spaces + line;
88 stringBuilder.append(append);
89 stringBuilder.append(NEW_LINE);
90 break;
Bharat saraswal5600f0f2016-03-09 18:34:56 +053091 }
Bharat saraswal97459962016-02-20 21:57:16 +053092 line = bufferReader.readLine();
93 }
94 return stringBuilder.toString();
95 } finally {
Vinod Kumar S9f26ae52016-03-23 15:30:27 +053096 fileReader.close();
Bharat saraswal97459962016-02-20 21:57:16 +053097 bufferReader.close();
98 }
99 }
100
101 /**
Bharat saraswal63f26fb2016-04-05 15:13:44 +0530102 * Updates the generated file handle.
Bharat saraswal97459962016-02-20 21:57:16 +0530103 *
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530104 * @param inputFile input file
Vinod Kumar S08710982016-03-03 19:55:30 +0530105 * @param contentTobeAdded content to be appended to the file
Gaurav Agrawalbfce9342016-06-15 13:58:01 +0530106 * @param isClose when close of file is called.
Bharat saraswal8beac342016-08-04 02:00:03 +0530107 * @throws IOException if the named file exists but is a directory rather than a regular file, does not exist but
108 * cannot be created, or cannot be opened for any other reason
Bharat saraswal97459962016-02-20 21:57:16 +0530109 */
Bharat saraswal8beac342016-08-04 02:00:03 +0530110 static void updateFileHandle(File inputFile, String contentTobeAdded, boolean isClose)
VinodKumarS-Huawei6266db32016-05-10 17:58:57 +0530111 throws IOException {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530112
113 List<FileWriter> fileWriterStore = new ArrayList<>();
114
Bharat saraswal97459962016-02-20 21:57:16 +0530115 FileWriter fileWriter = new FileWriter(inputFile, true);
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530116 fileWriterStore.add(fileWriter);
Vinod Kumar S9f26ae52016-03-23 15:30:27 +0530117 PrintWriter outputPrintWriter = new PrintWriter(fileWriter, true);
Bharat saraswal022dae92016-03-04 20:08:09 +0530118 if (!isClose) {
119 outputPrintWriter.write(contentTobeAdded);
120 outputPrintWriter.flush();
121 outputPrintWriter.close();
122 } else {
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530123 for (FileWriter curWriter : fileWriterStore) {
124 curWriter.flush();
125 curWriter.close();
Bharat saraswal715d3fc2016-05-17 19:59:16 +0530126 }
Bharat saraswal022dae92016-03-04 20:08:09 +0530127 }
Bharat saraswal97459962016-02-20 21:57:16 +0530128 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530129
130 /**
131 * Closes the file handle for temporary file.
132 *
133 * @param file file to be closed
134 * @param toBeDeleted flag to indicate if file needs to be deleted
135 * @throws IOException when failed to close the file handle
136 */
137 public static void closeFile(File file, boolean toBeDeleted)
138 throws IOException {
139
140 if (file != null) {
141 updateFileHandle(file, null, true);
142 if (toBeDeleted) {
Bharat saraswal8beac342016-08-04 02:00:03 +0530143 boolean deleted = file.delete();
144 if (!deleted) {
145 throw new IOException("Failed to delete temporary file " + file.getName());
146 }
Bharat saraswald14cbe82016-07-14 13:26:18 +0530147 }
148 }
149 }
Bharat saraswal97459962016-02-20 21:57:16 +0530150}