Java InputStream Copy to File copyStreamsToFile(String path, Map streamMap)

Here you can find the source of copyStreamsToFile(String path, Map streamMap)

Description

copy Streams To File

License

Open Source License

Declaration

public static void copyStreamsToFile(String path, Map<String, InputStream> streamMap) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2014 OpenLegacy Inc./*www.  j av a  2s .  co  m*/
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     OpenLegacy Inc. - initial API and implementation
 *******************************************************************************/

import java.io.BufferedReader;

import java.io.File;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.util.Map;

public class Main {
    private static String LINE_START = "      ";
    private static int LINE_MIN_LENGTH = 6;
    private static String newLine = System.getProperty("line.separator");

    public static void copyStreamsToFile(String path, Map<String, InputStream> streamMap) throws IOException {

        for (String copyBookName : streamMap.keySet()) {
            File copyBookFile = new File(path + copyBookName);
            OutputStream copyBookStream = new FileOutputStream(copyBookFile);
            OutputStreamWriter copyBookStreamWriter = new OutputStreamWriter(copyBookStream);

            copyBookFile.deleteOnExit();

            BufferedReader input = new BufferedReader(new InputStreamReader(streamMap.get(copyBookName)));
            String line;
            while ((line = input.readLine()) != null) {
                if ((line = preProcessLine(line)) != null) {
                    copyBookStreamWriter.write(line);
                }

            }
            // IOUtils.copy(streamMap.get(copyBookName), copyBookStream);
            copyBookStreamWriter.close();
            copyBookStream.close();
        }
    }

    public static String preProcessLine(String line) {
        if (line.length() > LINE_MIN_LENGTH) {
            line = LINE_START + line.substring(6);
            if (isComment(line) == false) {
                return line + newLine;

            }
        }
        return null;
    }

    private static boolean isComment(String line) {

        for (int i = 6; i < line.length(); i++) {
            if (line.charAt(i) != ' ') {
                if (line.charAt(i) == '*') {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }
}

Related

  1. copyStream(final InputStream is, final File destinationFile)
  2. copyStream(InputStream copyFrom, File copyTo)
  3. copyStream(InputStream in, File dest)
  4. copyStream(InputStream in, File dest)
  5. copyStreamIntoFile(File outFile, InputStream is)
  6. copyStreamsToFolder(Iterator> streams, File folder)
  7. copyStreamToFile(final File to, final InputStream from)
  8. copyStreamToFile(InputStream from, File to)
  9. copyStreamToFile(InputStream in, File destination)