Home | History | Annotate | Download | only in gltrace
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      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 
     17 package com.android.ide.eclipse.gltrace;
     18 
     19 import com.android.ide.eclipse.gltrace.GLProtoBuf.GLMessage;
     20 import com.google.protobuf.InvalidProtocolBufferException;
     21 
     22 import java.io.EOFException;
     23 import java.io.IOException;
     24 import java.io.RandomAccessFile;
     25 
     26 public class TraceFileReader {
     27     /** Maximum size for a protocol buffer message.
     28      * The message size is dominated by the size of the compressed framebuffer.
     29      * Currently, we assume that the maximum is for a 1080p display. Since the buffers compress
     30      * well, we should probably never get close to this.
     31      */
     32     private static final int MAX_PROTOBUF_SIZE = 1920 * 1080 * 100;
     33 
     34     /**
     35      * Obtain the next protobuf message in this file.
     36      * @param file file to read from
     37      * @param offset offset to start reading from
     38      * @return protobuf message at given offset
     39      * @throws IOException in case of file I/O errors
     40      * @throws InvalidProtocolBufferException if protobuf is not well formed
     41      */
     42     public GLMessage getMessageAtOffset(RandomAccessFile file, long offset) throws IOException {
     43         int len;
     44         byte[] b;
     45         try {
     46             if (offset != -1) {
     47                 file.seek(offset);
     48             }
     49 
     50             len = file.readInt();
     51             if (len > MAX_PROTOBUF_SIZE) {
     52                 String msg = String.format(
     53                         "Unexpectedly large (%d bytes) protocol buffer message encountered.",
     54                         len);
     55                 throw new InvalidProtocolBufferException(msg);
     56             }
     57 
     58             b = new byte[len];
     59             file.readFully(b);
     60         } catch (EOFException e) {
     61             return null;
     62         }
     63 
     64         return GLMessage.parseFrom(b);
     65     }
     66 }
     67