001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.wake.storage; 020 021import org.apache.reef.wake.EStage; 022import org.apache.reef.wake.EventHandler; 023 024import java.io.FileInputStream; 025import java.io.IOException; 026 027public class SequentialFileReader implements EStage<ReadRequest> { 028 final EventHandler<ReadResponse> dest = null; 029 final FileHandlePool fdPool = new FileHandlePool(); 030 031 @Override 032 public void onNext(ReadRequest value) { 033 FileInputStream fin = fdPool.get(value.f); 034 int readSoFar = 0; 035 try { 036 synchronized (fin) { 037 fin.reset(); 038 fin.skip(value.offset); 039 while (readSoFar != value.buf.length) { 040 int ret = fin.read(value.buf, readSoFar, value.buf.length); 041 if (ret == -1) { 042 break; 043 } 044 readSoFar += ret; 045 } 046 } 047 } catch (IOException e) { 048 fdPool.release(value.f, fin); 049// err.onNext(null); //new ReadError(e)); 050 } 051 fdPool.release(value.f, fin); 052 dest.onNext(new ReadResponse(value.buf, readSoFar, value.id)); 053 } 054 055 @Override 056 public void close() throws Exception { 057 // TODO Auto-generated method stub 058 059 } 060 061}