Commit a2563829 authored by Ricki Hirner's avatar Ricki Hirner

Accept Readers instead of InputStreams

parent a9324986
Pipeline #10025104 passed with stage
in 2 minutes and 36 seconds
......@@ -23,6 +23,7 @@ import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.sql.Date;
import java.util.Arrays;
......@@ -94,14 +95,13 @@ public class AndroidContactTest {
@Test
public void testInvalidPREF() throws ContactsStorageException, IOException {
Charset charset = Charsets.UTF_8;
String vCard = "BEGIN:VCARD\r\n" +
"VERSION:4.0\r\n" +
"FN:Test\r\n" +
"TEL;CELL=;PREF=:+12345\r\n" +
"EMAIL;PREF=invalid:test@example.com\r\n" +
"END:VCARD\r\n";
List<Contact> contacts = Contact.fromStream(IOUtils.toInputStream(vCard, charset), charset, null);
List<Contact> contacts = Contact.fromReader(new StringReader(vCard), null);
AndroidContact dbContact = new AndroidContact(addressBook, contacts.get(0), null, null);
dbContact.create();
......
......@@ -20,12 +20,10 @@ import org.apache.commons.lang3.StringUtils
import org.apache.commons.lang3.builder.HashCodeBuilder
import org.apache.commons.lang3.builder.ReflectionToStringBuilder
import java.io.IOException
import java.io.InputStream
import java.io.InputStreamReader
import java.io.OutputStream
import java.io.Reader
import java.net.URI
import java.net.URISyntaxException
import java.nio.charset.Charset
import java.util.*
import java.util.concurrent.atomic.AtomicInteger
import java.util.logging.Level
......@@ -112,21 +110,15 @@ class Contact {
/**
* Parses an InputStream that contains a VCard.
*
* @param stream input stream containing the VCard (any parsable version, i.e. 3 or 4)
* @param charset charset of the input stream or null (will assume UTF-8)
* @param reader reader for the input stream containing the VCard (pay attention to the charset)
* @param downloader will be used to download external resources like contact photos (may be null)
* @return list of filled Event data objects (may have size 0) – doesn't return null
* @throws IOException on I/O errors when reading the stream
*/
@JvmStatic
@Throws(IOException::class)
fun fromStream(stream: InputStream, charset: Charset?, downloader: Downloader?): List<Contact> {
var vcards: List<VCard>? = null
if (charset != null)
InputStreamReader(stream, charset).use { vcards = Ezvcard.parse(it).all() }
else
vcards = Ezvcard.parse(stream).all()
fun fromReader(reader: Reader, downloader: Downloader?): List<Contact> {
val vcards = Ezvcard.parse(reader).all()
val contacts = LinkedList<Contact>()
vcards?.forEach { contacts.add(fromVCard(it, downloader)) }
return contacts
......@@ -515,7 +507,7 @@ class Contact {
interface Downloader {
fun download(url: String, accepts: String): ByteArray
fun download(url: String, accepts: String): ByteArray?
}
}
......@@ -15,6 +15,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Locale;
......@@ -37,6 +38,7 @@ import ezvcard.property.Related;
import ezvcard.property.Telephone;
import ezvcard.property.Url;
import ezvcard.util.PartialDate;
import kotlin.text.Charsets;
import lombok.Cleanup;
import static org.junit.Assert.assertArrayEquals;
......@@ -52,14 +54,14 @@ public class ContactTest {
private Contact parseContact(String fname, Charset charset) throws IOException {
@Cleanup InputStream is = getClass().getClassLoader().getResourceAsStream(fname);
assertNotNull(is);
return Contact.fromStream(is, charset, null).get(0);
return Contact.fromReader(new InputStreamReader(is, charset == null ? Charsets.UTF_8 : charset), null).get(0);
}
private Contact regenerate(Contact c, VCardVersion vCardVersion) throws IOException {
ByteArrayOutputStream os = new ByteArrayOutputStream();
c.write(vCardVersion, GroupMethod.CATEGORIES, os);
Constants.log.log(Level.FINE, "Re-generated VCard", os.toString());
return Contact.fromStream(new ByteArrayInputStream(os.toByteArray()), null, null).get(0);
return Contact.fromReader(new InputStreamReader(new ByteArrayInputStream(os.toByteArray()), Charsets.UTF_8), null).get(0);
}
private String toString(Contact c, GroupMethod groupMethod, VCardVersion vCardVersion) throws IOException {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment