001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.eclipse.aether.internal.test.util;
020
021import java.io.BufferedOutputStream;
022import java.io.File;
023import java.io.FileInputStream;
024import java.io.FileOutputStream;
025import java.io.IOException;
026import java.io.InputStream;
027import java.io.OutputStream;
028import java.nio.Buffer;
029import java.nio.ByteBuffer;
030import java.nio.charset.StandardCharsets;
031import java.nio.file.Files;
032
033/**
034 * A simple file processor implementation to help satisfy component requirements during tests. It implements the
035 * deprecated {@code FileProcessor} SPI on purpose, referenced by its fully qualified name as javac reports an import
036 * of a deprecated type with {@code --release 8}.
037 */
038@SuppressWarnings("deprecation")
039public class TestFileProcessor implements org.eclipse.aether.spi.io.FileProcessor {
040
041    public boolean mkdirs(File directory) {
042        if (directory == null) {
043            return false;
044        }
045
046        if (directory.exists()) {
047            return false;
048        }
049        if (directory.mkdir()) {
050            return true;
051        }
052
053        File canonDir = null;
054        try {
055            canonDir = directory.getCanonicalFile();
056        } catch (IOException e) {
057            return false;
058        }
059
060        File parentDir = canonDir.getParentFile();
061        return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
062    }
063
064    public void write(File file, String data) throws IOException {
065        mkdirs(file.getParentFile());
066
067        try (FileOutputStream fos = new FileOutputStream(file)) {
068            if (data != null) {
069                fos.write(data.getBytes(StandardCharsets.UTF_8));
070            }
071        }
072    }
073
074    public void write(File target, InputStream source) throws IOException {
075        mkdirs(target.getAbsoluteFile().getParentFile());
076
077        try (OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
078            copy(fos, source, null);
079        }
080    }
081
082    public void copy(File source, File target) throws IOException {
083        copy(source, target, null);
084    }
085
086    public long copy(File source, File target, ProgressListener listener) throws IOException {
087        long total = 0;
088
089        mkdirs(target.getAbsoluteFile().getParentFile());
090
091        try (InputStream fis = new FileInputStream(source);
092                OutputStream fos = new BufferedOutputStream(new FileOutputStream(target))) {
093            total = copy(fos, fis, listener);
094        }
095
096        return total;
097    }
098
099    private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException {
100        long total = 0L;
101
102        ByteBuffer buffer = ByteBuffer.allocate(1024 * 32);
103        byte[] array = buffer.array();
104
105        while (true) {
106            int bytes = is.read(array);
107            if (bytes < 0) {
108                break;
109            }
110
111            os.write(array, 0, bytes);
112
113            total += bytes;
114
115            if (listener != null && bytes > 0) {
116                try {
117                    ((Buffer) buffer).rewind();
118                    ((Buffer) buffer).limit(bytes);
119                    listener.progressed(buffer);
120                } catch (Exception e) {
121                    // too bad
122                }
123            }
124        }
125
126        return total;
127    }
128
129    public void move(File source, File target) throws IOException {
130        target.delete();
131
132        if (!source.renameTo(target)) {
133            copy(source, target);
134
135            target.setLastModified(source.lastModified());
136
137            source.delete();
138        }
139    }
140
141    @Override
142    public String readChecksum(final File checksumFile) throws IOException {
143        return new String(Files.readAllBytes(checksumFile.toPath()), StandardCharsets.UTF_8);
144    }
145
146    @Override
147    public void writeChecksum(final File checksumFile, final String checksum) throws IOException {
148        write(checksumFile, checksum);
149    }
150}