blob: 40293bf471b41eab020958d969b5d35bd762b5de [file] [log] [blame]
Paul Duffin7fc0b452015-11-10 17:45:15 +00001/*
2 * Copyright (C) 2010 Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.google.caliper.util;
18
19import com.google.gson.JsonParser;
20
21import java.io.BufferedReader;
22import java.io.Closeable;
23import java.io.IOException;
24import java.io.Reader;
25
26/**
27 * Reads a stream containing inline JSON objects. Each JSON object is prefixed
28 * by a marker string and suffixed by a newline character.
29 */
30public final class InterleavedReader implements Closeable {
31
32 /**
33 * The length of the scratch buffer to search for markers in. Also acts as an
34 * upper bound on the length of returned strings. Not used as an I/O buffer.
35 */
36 private static final int BUFFER_LENGTH = 80;
37
38 private final String marker;
39 private final BufferedReader reader;
40 private final JsonParser jsonParser = new JsonParser();
41
42 public static final String DEFAULT_MARKER = "//ZxJ/";
43
44 public InterleavedReader(Reader reader) {
45 this(DEFAULT_MARKER, reader);
46 }
47
48 public InterleavedReader(String marker, Reader reader) {
49 if (marker.length() > BUFFER_LENGTH) {
50 throw new IllegalArgumentException("marker.length() > BUFFER_LENGTH");
51 }
52 this.marker = marker;
53 this.reader = reader instanceof BufferedReader
54 ? (BufferedReader) reader
55 : new BufferedReader(reader);
56 }
57
58 /**
59 * Returns the next value in the stream: either a String, a JsonElement, or
60 * null to indicate the end of the stream. Callers should use instanceof to
61 * inspect the return type.
62 */
63 public Object read() throws IOException {
64 char[] buffer = new char[BUFFER_LENGTH];
65 reader.mark(BUFFER_LENGTH);
66 int count = 0;
67 int textEnd;
68
69 while (true) {
70 int r = reader.read(buffer, count, buffer.length - count);
71
72 if (r == -1) {
73 // the input is exhausted; return the remaining characters
74 textEnd = count;
75 break;
76 }
77
78 count += r;
79 int possibleMarker = findPossibleMarker(buffer, count);
80
81 if (possibleMarker != 0) {
82 // return the characters that precede the marker
83 textEnd = possibleMarker;
84 break;
85 }
86
87 if (count < marker.length()) {
88 // the buffer contains only the prefix of a marker so we must read more
89 continue;
90 }
91
92 // we've read a marker so return the value that follows
93 reader.reset();
94 String json = reader.readLine().substring(marker.length());
95 return jsonParser.parse(json);
96 }
97
98 if (count == 0) {
99 return null;
100 }
101
102 // return characters
103 reader.reset();
104 count = reader.read(buffer, 0, textEnd);
105 return new String(buffer, 0, count);
106 }
107
108 @Override public void close() throws IOException {
109 reader.close();
110 }
111
112 /**
113 * Returns the index of marker in {@code chars}, stopping at {@code limit}.
114 * Should the chars end with a prefix of marker, the offset of that prefix
115 * is returned.
116 */
117 int findPossibleMarker(char[] chars, int limit) {
118 search:
119 for (int i = 0; true; i++) {
120 for (int m = 0; m < marker.length() && i + m < limit; m++) {
121 if (chars[i + m] != marker.charAt(m)) {
122 continue search;
123 }
124 }
125 return i;
126 }
127 }
128}