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.impl; 020 021import javax.inject.Named; 022import javax.inject.Singleton; 023 024import java.io.*; 025import java.nio.ByteBuffer; 026import java.nio.charset.StandardCharsets; 027import java.nio.file.Files; 028import java.nio.file.Path; 029import java.nio.file.StandardCopyOption; 030 031import org.eclipse.aether.spi.io.PathProcessor; 032import org.eclipse.aether.util.FileUtils; 033 034/** 035 * A utility class helping with file-based operations. 036 */ 037@Singleton 038@Named 039public class DefaultPathProcessor implements PathProcessor { 040 @Override 041 public void write(Path target, String data) throws IOException { 042 FileUtils.writeFile(target, p -> Files.write(p, data.getBytes(StandardCharsets.UTF_8))); 043 } 044 045 @Override 046 public void write(Path target, InputStream source) throws IOException { 047 FileUtils.writeFile(target, p -> Files.copy(source, p, StandardCopyOption.REPLACE_EXISTING)); 048 } 049 050 @Override 051 public void copy(Path source, Path target) throws IOException { 052 copy(source, target, null); 053 } 054 055 @Override 056 public long copy(Path source, Path target, ProgressListener listener) throws IOException { 057 try (InputStream in = new BufferedInputStream(Files.newInputStream(source)); 058 FileUtils.CollocatedTempFile tempTarget = FileUtils.newTempFile(target); 059 OutputStream out = new BufferedOutputStream(Files.newOutputStream(tempTarget.getPath()))) { 060 long result = copy(out, in, listener); 061 tempTarget.move(); 062 return result; 063 } 064 } 065 066 private long copy(OutputStream os, InputStream is, ProgressListener listener) throws IOException { 067 long total = 0L; 068 byte[] buffer = new byte[1024 * 32]; 069 while (true) { 070 int bytes = is.read(buffer); 071 if (bytes < 0) { 072 break; 073 } 074 075 os.write(buffer, 0, bytes); 076 077 total += bytes; 078 079 if (listener != null && bytes > 0) { 080 try { 081 listener.progressed(ByteBuffer.wrap(buffer, 0, bytes)); 082 } catch (Exception e) { 083 // too bad 084 } 085 } 086 } 087 088 return total; 089 } 090 091 @Override 092 public void move(Path source, Path target) throws IOException { 093 Files.move(source, target, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.COPY_ATTRIBUTES); 094 } 095}