View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.eclipse.aether.internal.test.util;
20  
21  import java.io.BufferedOutputStream;
22  import java.io.File;
23  import java.io.FileInputStream;
24  import java.io.FileOutputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  import java.io.OutputStream;
28  import java.nio.Buffer;
29  import java.nio.ByteBuffer;
30  import java.nio.charset.StandardCharsets;
31  import java.nio.file.Files;
32  
33  import org.eclipse.aether.spi.io.FileProcessor;
34  
35  /**
36   * A simple file processor implementation to help satisfy component requirements during tests.
37   */
38  public class TestFileProcessor implements FileProcessor {
39  
40      public boolean mkdirs(File directory) {
41          if (directory == null) {
42              return false;
43          }
44  
45          if (directory.exists()) {
46              return false;
47          }
48          if (directory.mkdir()) {
49              return true;
50          }
51  
52          File canonDir = null;
53          try {
54              canonDir = directory.getCanonicalFile();
55          } catch (IOException e) {
56              return false;
57          }
58  
59          File parentDir = canonDir.getParentFile();
60          return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
61      }
62  
63      public void write(File file, String data) throws IOException {
64          mkdirs(file.getParentFile());
65  
66          try (FileOutputStream fos = new FileOutputStream(file)) {
67              if (data != null) {
68                  fos.write(data.getBytes(StandardCharsets.UTF_8));
69              }
70          }
71      }
72  
73      public void write(File target, InputStream source) throws IOException {
74          mkdirs(target.getAbsoluteFile().getParentFile());
75  
76          try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
77              copy(fos, source, null);
78          }
79      }
80  
81      public void copy(File source, File target) throws IOException {
82          copy(source, target, null);
83      }
84  
85      public long copy(File source, File target, ProgressListener listener) throws IOException {
86          long total = 0;
87  
88          mkdirs(target.getAbsoluteFile().getParentFile());
89  
90          try (InputStream fis = new FileInputStream(source);
91                  OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
92              total = copy(fos, fis, listener);
93          }
94  
95          return total;
96      }
97  
98      private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
99          long total = 0L;
100 
101         ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
102         byte[] array = buffer.array();
103 
104         while (true) {
105             int bytes = is.read(array);
106             if (bytes < 0) {
107                 break;
108             }
109 
110             os.write(array, 0, bytes);
111 
112             total += bytes;
113 
114             if (listener != null && bytes > 0) {
115                 try {
116                     ((Buffer) buffer).rewind();
117                     ((Buffer) buffer).limit(bytes);
118                     listener.progressed(buffer);
119                 } catch (Exception e) {
120                     // too bad
121                 }
122             }
123         }
124 
125         return total;
126     }
127 
128     public void move(File source, File target) throws IOException {
129         target.delete();
130 
131         if (!source.renameTo(target)) {
132             copy(source, target);
133 
134             target.setLastModified(source.lastModified());
135 
136             source.delete();
137         }
138     }
139 
140     @Override
141     public String readChecksum(final File checksumFile) throws IOException {
142         return new String(Files.readAllBytes(checksumFile.toPath()), StandardCharsets.UTF_8);
143     }
144 
145     @Override
146     public void writeChecksum(final File checksumFile, final String checksum) throws IOException {
147         write(checksumFile, checksum);
148     }
149 }