Android FileOutputStream Write WriteArrayOfStringPairs(FileOutputStream stream, ArrayList> pairs)

Here you can find the source of WriteArrayOfStringPairs(FileOutputStream stream, ArrayList> pairs)

Description

Write Array Of String Pairs

License

Open Source License

Declaration

public static void WriteArrayOfStringPairs(FileOutputStream stream,
            ArrayList<Pair<String, String>> pairs) throws IOException 

Method Source Code

/*******************************************************************************
 * Copyright (C) 2010-2011 Dmitriy Nesterov
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version./*w  ww  .  ja  v  a2s.c o  m*/
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class Main{
    public static void WriteArrayOfStringPairs(FileOutputStream stream,
            ArrayList<Pair<String, String>> pairs) throws IOException {
        WriteInt(stream, pairs.size());
        for (Pair<String, String> pair : pairs) {
            WriteString(stream, pair.getFirst());
            WriteString(stream, pair.getSecond());
        }
    }
    public static void WriteInt(FileOutputStream stream, int value)
            throws IOException {
        stream.write(IntToBytes(value));
    }
    public static void WriteString(FileOutputStream stream, String text)
            throws IOException {
        byte[] bytes = text.getBytes();
        WriteInt(stream, bytes.length);
        stream.write(bytes);
    }
    public static byte[] IntToBytes(int value) {
        byte[] result = new byte[4];
        result[0] = (byte) (value >> 24);
        result[1] = (byte) ((value << 8) >> 24);
        result[2] = (byte) ((value << 16) >> 24);
        result[3] = (byte) ((value << 24) >> 24);

        return result;
    }
}

Related

  1. WriteInt(FileOutputStream stream, int value)
  2. WriteString(FileOutputStream stream, String text)
  3. saveFileContent(String file, String content, String encode)
  4. getXMLAsString(File xmlFile, String fileName)