1package eu.siacs.conversations.crypto.sasl;
2
3import android.util.Base64;
4
5import java.util.ArrayList;
6import java.util.Arrays;
7import java.util.Iterator;
8import java.util.List;
9import java.util.NoSuchElementException;
10
11/**
12 * A tokenizer for GS2 header strings
13 */
14public final class Tokenizer implements Iterator<String>, Iterable<String> {
15 private final List<String> parts;
16 private int index;
17
18 public Tokenizer(final byte[] challenge) {
19 final String challengeString = new String(challenge);
20 parts = new ArrayList<>(Arrays.asList(challengeString.split(",")));
21 index = 0;
22 }
23
24 /**
25 * Returns true if there is at least one more element, false otherwise.
26 *
27 * @see #next
28 */
29 @Override
30 public boolean hasNext() {
31 return parts.size() != index + 1;
32 }
33
34 /**
35 * Returns the next object and advances the iterator.
36 *
37 * @return the next object.
38 * @throws java.util.NoSuchElementException if there are no more elements.
39 * @see #hasNext
40 */
41 @Override
42 public String next() {
43 if (hasNext()) {
44 return parts.get(index++);
45 } else {
46 throw new NoSuchElementException("No such element. Size is: " + parts.size());
47 }
48 }
49
50 /**
51 * Removes the last object returned by {@code next} from the collection.
52 * This method can only be called once between each call to {@code next}.
53 *
54 * @throws UnsupportedOperationException if removing is not supported by the collection being
55 * iterated.
56 * @throws IllegalStateException if {@code next} has not been called, or {@code remove} has
57 * already been called after the last call to {@code next}.
58 */
59 @Override
60 public void remove() {
61 if(index <= 0) {
62 throw new IllegalStateException("You can't delete an element before first next() method call");
63 }
64 parts.remove(--index);
65 }
66
67 /**
68 * Returns an {@link java.util.Iterator} for the elements in this object.
69 *
70 * @return An {@code Iterator} instance.
71 */
72 @Override
73 public Iterator<String> iterator() {
74 return parts.iterator();
75 }
76}