com.microsoft.reef.io.storage.FramingTupleDeserializer Maven / Gradle / Ivy
/**
* Copyright (C) 2013 Microsoft Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.microsoft.reef.io.storage;
import com.microsoft.reef.exception.evaluator.ServiceException;
import com.microsoft.reef.exception.evaluator.ServiceRuntimeException;
import com.microsoft.reef.io.Tuple;
import com.microsoft.reef.io.serialization.Deserializer;
import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class FramingTupleDeserializer implements
Deserializer, InputStream> {
private final Deserializer keyDeserializer;
private final Deserializer valDeserializer;
public FramingTupleDeserializer(Deserializer keyDeserializer,
Deserializer valDeserializer) {
this.keyDeserializer = keyDeserializer;
this.valDeserializer = valDeserializer;
}
@Override
public Iterable> create(InputStream ins) {
final DataInputStream in = new DataInputStream(ins);
final Iterable keyItbl = keyDeserializer.create(in);
final Iterable valItbl = valDeserializer.create(in);
return new Iterable>() {
@Override
public Iterator> iterator() {
final Iterator keyIt = keyItbl.iterator();
final Iterator valIt = valItbl.iterator();
try {
return new Iterator>() {
private int readFrameLength() throws ServiceException {
try {
return in.readInt();
} catch(IOException e) {
throw new ServiceRuntimeException(e);
}
}
int nextFrameLength = readFrameLength();
@Override
public boolean hasNext() {
return nextFrameLength != -1;
}
@Override
public Tuple next() {
try {
if(nextFrameLength == -1) {
throw new NoSuchElementException();
}
K k = keyIt.next();
readFrameLength();
V v = valIt.next();
nextFrameLength = readFrameLength();
return new Tuple<>(k,v);
} catch(ServiceException e) {
throw new ServiceRuntimeException(e);
}
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
};
} catch(ServiceException e) {
throw new ServiceRuntimeException(e);
}
}
};
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy