001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 */ 018package org.apache.commons.compress.parallel; 019 020import java.io.File; 021import java.io.FileNotFoundException; 022import java.io.FileInputStream; 023import java.io.FileOutputStream; 024import java.io.IOException; 025import java.io.InputStream; 026 027/** 028 * ScatterGatherBackingStore that is backed by a file. 029 * 030 * @since 1.10 031 */ 032public class FileBasedScatterGatherBackingStore implements ScatterGatherBackingStore { 033 private final File target; 034 private final FileOutputStream os; 035 private boolean closed; 036 037 public FileBasedScatterGatherBackingStore(File target) throws FileNotFoundException { 038 this.target = target; 039 os = new FileOutputStream(target); 040 } 041 042 public InputStream getInputStream() throws IOException { 043 return new FileInputStream(target); 044 } 045 046 @SuppressWarnings("ResultOfMethodCallIgnored") 047 public void closeForWriting() throws IOException { 048 if (!closed) { 049 os.close(); 050 closed = true; 051 } 052 } 053 054 public void writeOut(byte[] data, int offset, int length) throws IOException { 055 os.write(data, offset, length); 056 } 057 058 public void close() throws IOException { 059 closeForWriting(); 060 target.delete(); 061 } 062}