All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.mobile.io.StringsIO Maven / Gradle / Ivy

There is a newer version: 1.0.9
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * $Id: StringsIO.java,v 70fb0b4b2a54 2010/07/04 21:14:16 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.mobile.io;

import javax.annotation.Nonnull;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import it.tidalwave.util.StringValue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class StringsIO
  {
    public static interface Processor
      {
        @Nonnull
        public T processString (@Nonnull String string);
      }

    private final static String CHARSET = "UTF-8";
    
    @Nonnull
    private final File file;

    public StringsIO (final @Nonnull File file)
      {
        this.file = file;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void store (final @Nonnull Iterable stringValues)
      throws IOException
      {
        PrintWriter pw = null;

        try
          {
            pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), CHARSET));

            for (final StringValue stringValue : stringValues)
              {
                pw.println(stringValue.stringValue());
              }
          }
        finally
          {
            IoUtils.safeClose(pw);
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public  Collection load (final @Nonnull Processor processor)
      throws IOException
      {
        BufferedReader br = null;

        try
          {
            final List result = new ArrayList();
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file), CHARSET));

            for (;;)
              {
                final String string = br.readLine();

                if (string == null)
                  {
                    break;
                  }

                result.add(processor.processString(string));
              }

            return result;
          }
        finally
          {
            IoUtils.safeClose(br);
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy