001    /*
002     * Copyright 2010 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.vafer.jdeb.producers;
017    
018    import java.io.File;
019    import java.io.FileInputStream;
020    import java.io.IOException;
021    import java.io.InputStream;
022    
023    import org.apache.tools.tar.TarEntry;
024    import org.vafer.jdeb.DataConsumer;
025    import org.vafer.jdeb.DataProducer;
026    import org.vafer.jdeb.mapping.Mapper;
027    
028    /**
029     * DataProducer representing a single file
030     * For cross-platform permissions and ownerships you probably want to use a Mapper, too. 
031     * 
032     * @author Torsten Curdt <tcurdt@vafer.org>
033     */
034    public final class DataProducerFile extends AbstractDataProducer implements DataProducer {
035    
036            private final File file;
037            
038            public DataProducerFile(final File pFile, String[] pIncludes, String[] pExcludes, Mapper[] pMapper) {
039                    super(pIncludes, pExcludes, pMapper);           
040                    file = pFile;
041            }
042    
043            public void produce( final DataConsumer pReceiver ) throws IOException {
044    
045            TarEntry entry = new TarEntry(file.getName());
046            entry.setUserId(0);
047            entry.setUserName("root");
048            entry.setGroupId(0);
049            entry.setGroupName("root");
050            entry.setMode(TarEntry.DEFAULT_FILE_MODE);
051    
052            entry = map(entry);
053    
054            entry.setSize(file.length());
055    
056            final InputStream inputStream = new FileInputStream(file);
057            try {
058                pReceiver.onEachFile(inputStream, entry.getName(), entry.getLinkName(), entry.getUserName(), entry.getUserId(), entry.getGroupName(), entry.getGroupId(), entry.getMode(), entry.getSize());
059            } finally {
060                inputStream.close();
061            }
062            
063            }
064    
065    }