Search in sources :

Example 61 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project commons by twitter.

the class TokenStreamSerializerTest method testVInt.

@Test
public void testVInt() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TokenStreamSerializer.AttributeOutputStream out = new TokenStreamSerializer.AttributeOutputStream(baos);
    final int N = 10000;
    final int[] expected = new int[N];
    Random rnd = new Random();
    for (int i = 0; i < N; i++) {
        expected[i] = rnd.nextInt();
        out.writeVInt(expected[i]);
    }
    out.flush();
    byte[] data = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(data);
    TokenStreamSerializer.AttributeInputStream in = new TokenStreamSerializer.AttributeInputStream(bais);
    for (int i = 0; i < N; i++) {
        assertEquals(expected[i], in.readVInt());
    }
}
Also used : Random(java.util.Random) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 62 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project trie4j by takawitter.

the class LongsTailLOUDSTrieWithConcatTailArrayTest method test_save_load.

@Test
public void test_save_load() throws Exception {
    String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
    TailLOUDSTrie lt = trieWithWords(words);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    lt.writeExternal(oos);
    oos.flush();
    lt = new TailLOUDSTrie();
    lt.readExternal(new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())));
    for (String w : words) {
        Assert.assertTrue(lt.contains(w));
    }
    Assert.assertFalse(lt.contains("おやすみなさい"));
    StringBuilder b = new StringBuilder();
    Node[] children = lt.getRoot().getChildren();
    for (Node n : children) {
        char[] letters = n.getLetters();
        b.append(letters[0]);
    }
    Assert.assertEquals("おこさ", b.toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.trie4j.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Example 63 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project trie4j by takawitter.

the class TailLOUDSTrieWithConcatTailArrayTest method test_save_load.

@Test
public void test_save_load() throws Exception {
    String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
    TailLOUDSTrie lt = trieWithWords(words);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    lt.writeExternal(oos);
    oos.flush();
    lt = new TailLOUDSTrie();
    lt.readExternal(new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())));
    for (String w : words) {
        Assert.assertTrue(lt.contains(w));
    }
    Assert.assertFalse(lt.contains("おやすみなさい"));
    StringBuilder b = new StringBuilder();
    Node[] children = lt.getRoot().getChildren();
    for (Node n : children) {
        char[] letters = n.getLetters();
        b.append(letters[0]);
    }
    Assert.assertEquals("おこさ", b.toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.trie4j.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Example 64 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project trie4j by takawitter.

the class TailLOUDSTrieWithSuffixTrieTailArrayTest method test_save_load2.

@Test
public void test_save_load2() throws Exception {
    String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
    Trie trie = new PatriciaTrie();
    for (String w : words) trie.insert(w);
    TailLOUDSTrie lt = new TailLOUDSTrie(trie);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(lt);
    oos.flush();
    lt = (TailLOUDSTrie) new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())).readObject();
    for (String w : words) {
        Assert.assertTrue(lt.contains(w));
    }
    Assert.assertFalse(lt.contains("おやすみなさい"));
    StringBuilder b = new StringBuilder();
    Node[] children = lt.getRoot().getChildren();
    for (Node n : children) {
        char[] letters = n.getLetters();
        b.append(letters[0]);
    }
    Assert.assertEquals("おこさ", b.toString());
}
Also used : PatriciaTrie(org.trie4j.patricia.PatriciaTrie) Node(org.trie4j.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) Trie(org.trie4j.Trie) PatriciaTrie(org.trie4j.patricia.PatriciaTrie) ObjectInputStream(java.io.ObjectInputStream) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Example 65 with ByteArrayInputStream

use of java.io.ByteArrayInputStream in project trie4j by takawitter.

the class TailLOUDSTrieWithSuffixTrieTailArrayTest method test_save_load.

@Test
public void test_save_load() throws Exception {
    String[] words = { "こんにちは", "さようなら", "おはよう", "おおきなかぶ", "おおやまざき" };
    TailLOUDSTrie lt = trieWithWords(words);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    lt.writeExternal(oos);
    oos.flush();
    lt = new TailLOUDSTrie();
    lt.readExternal(new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray())));
    for (String w : words) {
        Assert.assertTrue(lt.contains(w));
    }
    Assert.assertFalse(lt.contains("おやすみなさい"));
    StringBuilder b = new StringBuilder();
    Node[] children = lt.getRoot().getChildren();
    for (Node n : children) {
        char[] letters = n.getLetters();
        b.append(letters[0]);
    }
    Assert.assertEquals("おこさ", b.toString());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Node(org.trie4j.Node) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ObjectOutputStream(java.io.ObjectOutputStream) ObjectInputStream(java.io.ObjectInputStream) AbstractTermIdTrieTest(org.trie4j.AbstractTermIdTrieTest) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)8004 Test (org.junit.Test)2746 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2096 InputStream (java.io.InputStream)1831 IOException (java.io.IOException)1612 ObjectInputStream (java.io.ObjectInputStream)672 DataInputStream (java.io.DataInputStream)653 X509Certificate (java.security.cert.X509Certificate)412 ObjectOutputStream (java.io.ObjectOutputStream)376 CertificateFactory (java.security.cert.CertificateFactory)369 ArrayList (java.util.ArrayList)354 File (java.io.File)315 HashMap (java.util.HashMap)279 Certificate (java.security.cert.Certificate)239 DataOutputStream (java.io.DataOutputStream)226 InputStreamReader (java.io.InputStreamReader)216 FileInputStream (java.io.FileInputStream)211 Document (org.w3c.dom.Document)186 Test (org.testng.annotations.Test)181 Map (java.util.Map)162