use of java.util.ArrayDeque in project okhttp by square.
the class BasicCertificateChainCleaner method clean.
/**
* Returns a cleaned chain for {@code chain}.
*
* <p>This method throws if the complete chain to a trusted CA certificate cannot be constructed.
* This is unexpected unless the trust root index in this class has a different trust manager than
* what was used to establish {@code chain}.
*/
@Override
public List<Certificate> clean(List<Certificate> chain, String hostname) throws SSLPeerUnverifiedException {
Deque<Certificate> queue = new ArrayDeque<>(chain);
List<Certificate> result = new ArrayList<>();
result.add(queue.removeFirst());
boolean foundTrustedCertificate = false;
followIssuerChain: for (int c = 0; c < MAX_SIGNERS; c++) {
X509Certificate toVerify = (X509Certificate) result.get(result.size() - 1);
// If this cert has been signed by a trusted cert, use that. Add the trusted certificate to
// the end of the chain unless it's already present. (That would happen if the first
// certificate in the chain is itself a self-signed and trusted CA certificate.)
X509Certificate trustedCert = trustRootIndex.findByIssuerAndSignature(toVerify);
if (trustedCert != null) {
if (result.size() > 1 || !toVerify.equals(trustedCert)) {
result.add(trustedCert);
}
if (verifySignature(trustedCert, trustedCert)) {
// The self-signed cert is a root CA. We're done.
return result;
}
foundTrustedCertificate = true;
continue;
}
// the next element in the chain, but it could be any element.
for (Iterator<Certificate> i = queue.iterator(); i.hasNext(); ) {
X509Certificate signingCert = (X509Certificate) i.next();
if (verifySignature(toVerify, signingCert)) {
i.remove();
result.add(signingCert);
continue followIssuerChain;
}
}
// We've reached the end of the chain. If any cert in the chain is trusted, we're done.
if (foundTrustedCertificate) {
return result;
}
// The last link isn't trusted. Fail.
throw new SSLPeerUnverifiedException("Failed to find a trusted cert that signed " + toVerify);
}
throw new SSLPeerUnverifiedException("Certificate chain too long: " + result);
}
use of java.util.ArrayDeque in project rest.li by linkedin.
the class URIMaskUtil method decodeMaskUriFormat.
/**
* Return a MaskTree decoded from the URI-formatted String input.
*
* @param toparse StringBuilder containing a URI-formatted String
* representation of an encoded MaskTree
* @return a MaskTree
* @throws IllegalMaskException if syntax in the input is malformed
*/
public static MaskTree decodeMaskUriFormat(StringBuilder toparse) throws IllegalMaskException {
ParseState state = ParseState.PARSE_FIELDS;
DataMap result = new DataMap();
Deque<DataMap> stack = new ArrayDeque<DataMap>();
stack.addLast(result);
while (toparse.length() > 0) {
switch(state) {
case TRAVERSE:
if (toparse.indexOf(",") != 0) {
throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while traversing");
}
toparse.delete(0, 1);
state = ParseState.PARSE_FIELDS;
break;
case DESCEND:
if (toparse.indexOf(":(") != 0) {
throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while descending");
}
toparse.delete(0, 2);
state = ParseState.PARSE_FIELDS;
break;
case PARSE_FIELDS:
Integer maskValue = null;
if (toparse.charAt(0) == '-') {
maskValue = MaskOperation.NEGATIVE_MASK_OP.getRepresentation();
toparse.delete(0, 1);
} else {
maskValue = MaskOperation.POSITIVE_MASK_OP.getRepresentation();
}
int nextToken = -1;
StringBuilder field = new StringBuilder();
for (int ii = 0; ii < toparse.length(); ++ii) {
char c = toparse.charAt(ii);
switch(c) {
case ',':
state = ParseState.TRAVERSE;
nextToken = ii;
break;
case ':':
if (toparse.charAt(ii + 1) != '(') {
throw new IllegalMaskException("Malformed mask syntax: expected '(' token");
}
state = ParseState.DESCEND;
nextToken = ii;
break;
case ')':
state = ParseState.ASCEND;
nextToken = ii;
break;
default:
field.append(c);
break;
}
if (nextToken != -1) {
break;
}
}
if (toparse.length() != field.length()) {
if (nextToken == -1) {
throw new IllegalMaskException("Malformed mask syntax: expected closing token");
}
toparse.delete(0, nextToken);
} else {
toparse.delete(0, toparse.length());
}
if (state == ParseState.DESCEND) {
if (field.length() == 0) {
throw new IllegalMaskException("Malformed mask syntax: empty parent field name");
}
DataMap subTree = new DataMap();
stack.peekLast().put(field.toString().trim(), subTree);
stack.addLast(subTree);
} else if (field.length() != 0) {
stack.peekLast().put(field.toString().trim(), maskValue);
}
break;
case ASCEND:
if (toparse.indexOf(")") != 0) {
throw new IllegalStateException("Internal Error parsing mask: unexpected parse buffer '" + toparse + "' while ascending");
}
if (stack.isEmpty()) {
throw new IllegalMaskException("Malformed mask syntax: unexpected ')' token");
}
toparse.delete(0, 1);
stack.removeLast();
state = ParseState.PARSE_FIELDS;
break;
}
}
if (stack.size() != 1) {
throw new IllegalMaskException("Malformed mask syntax: unmatched nesting");
}
result = stack.removeLast();
return new MaskTree(result);
}
use of java.util.ArrayDeque in project wire by square.
the class ParsingTester method main.
public static void main(String... args) {
int total = 0;
int failed = 0;
Deque<File> fileQueue = new ArrayDeque<>();
fileQueue.add(ROOT);
while (!fileQueue.isEmpty()) {
File file = fileQueue.removeFirst();
if (file.isDirectory()) {
Collections.addAll(fileQueue, file.listFiles());
} else if (file.getName().endsWith(".proto")) {
System.out.println("Parsing " + file.getPath());
total += 1;
try (BufferedSource in = Okio.buffer(Okio.source(file))) {
String data = in.readUtf8();
ProtoParser.parse(Location.get(file.getPath()), data);
} catch (Exception e) {
e.printStackTrace();
failed += 1;
}
}
}
System.out.println("\nTotal: " + total + " Failed: " + failed);
}
use of java.util.ArrayDeque in project hazelcast by hazelcast.
the class WriteBehindStateHolder method readData.
@Override
public void readData(ObjectDataInput in) throws IOException {
int size = in.readInt();
delayedEntries = new HashMap<String, List<DelayedEntry>>(size);
for (int i = 0; i < size; i++) {
String mapName = in.readUTF();
int listSize = in.readInt();
List<DelayedEntry> delayedEntriesList = new ArrayList<DelayedEntry>(listSize);
for (int j = 0; j < listSize; j++) {
Data key = in.readData();
Data value = in.readData();
long storeTime = in.readLong();
int partitionId = in.readInt();
long sequence = in.readLong();
DelayedEntry<Data, Data> entry = DelayedEntries.createDefault(key, value, storeTime, partitionId);
entry.setSequence(sequence);
delayedEntriesList.add(entry);
}
delayedEntries.put(mapName, delayedEntriesList);
}
int expectedSize = in.readInt();
flushSequences = new HashMap<String, Queue<WriteBehindStore.Sequence>>(expectedSize);
for (int i = 0; i < expectedSize; i++) {
String mapName = in.readUTF();
int setSize = in.readInt();
Queue<WriteBehindStore.Sequence> queue = new ArrayDeque<WriteBehindStore.Sequence>(setSize);
for (int j = 0; j < setSize; j++) {
queue.add(new WriteBehindStore.Sequence(in.readLong(), in.readBoolean()));
}
flushSequences.put(mapName, queue);
}
}
use of java.util.ArrayDeque in project flink by apache.
the class SpanningRecordSerializationTest method test.
/**
* Iterates over the provided records and tests whether {@link SpanningRecordSerializer} and {@link AdaptiveSpanningRecordDeserializer}
* interact as expected.
* <p>
* Only a single {@link MemorySegment} will be allocated.
*
* @param records records to test
* @param segmentSize size for the {@link MemorySegment}
*/
private void test(Util.MockRecords records, int segmentSize, RecordSerializer<SerializationTestType> serializer, RecordDeserializer<SerializationTestType> deserializer) throws Exception {
// length encoding
final int SERIALIZATION_OVERHEAD = 4;
final Buffer buffer = new Buffer(MemorySegmentFactory.allocateUnpooledSegment(segmentSize), mock(BufferRecycler.class));
final ArrayDeque<SerializationTestType> serializedRecords = new ArrayDeque<SerializationTestType>();
// -------------------------------------------------------------------------------------------------------------
serializer.setNextBuffer(buffer);
int numBytes = 0;
int numRecords = 0;
for (SerializationTestType record : records) {
serializedRecords.add(record);
numRecords++;
numBytes += record.length() + SERIALIZATION_OVERHEAD;
// serialize record
if (serializer.addRecord(record).isFullBuffer()) {
// buffer is full => start deserializing
deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), segmentSize);
while (!serializedRecords.isEmpty()) {
SerializationTestType expected = serializedRecords.poll();
SerializationTestType actual = expected.getClass().newInstance();
if (deserializer.getNextRecord(actual).isFullRecord()) {
Assert.assertEquals(expected, actual);
numRecords--;
} else {
serializedRecords.addFirst(expected);
break;
}
}
while (serializer.setNextBuffer(buffer).isFullBuffer()) {
deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), segmentSize);
}
}
}
// deserialize left over records
deserializer.setNextMemorySegment(serializer.getCurrentBuffer().getMemorySegment(), (numBytes % segmentSize));
serializer.clear();
while (!serializedRecords.isEmpty()) {
SerializationTestType expected = serializedRecords.poll();
SerializationTestType actual = expected.getClass().newInstance();
RecordDeserializer.DeserializationResult result = deserializer.getNextRecord(actual);
Assert.assertTrue(result.isFullRecord());
Assert.assertEquals(expected, actual);
numRecords--;
}
// assert that all records have been serialized and deserialized
Assert.assertEquals(0, numRecords);
Assert.assertFalse(serializer.hasData());
Assert.assertFalse(deserializer.hasUnfinishedData());
}
Aggregations