1

I am currently following https://developers.google.com/drive/v3/web/quickstart/java tutorial to get the list of files from the doogle drive. In am getting the following exception

Caused by:

java.io.EOFException
    at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
    at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
    at java.io.ObjectInputStream.<init>(Unknown Source)
    at com.google.api.client.util.IOUtils.deserialize(IOUtils.java:171)
    at com.google.api.client.util.store.FileDataStoreFactory$FileDataStore.<init>(FileDataStoreFactory.java:102)
    at com.google.api.client.util.store.FileDataStoreFactory.createDataStore(FileDataStoreFactory.java:73)
    at com.google.api.client.util.store.AbstractDataStoreFactory.getDataStore(AbstractDataStoreFactory.java:55)
    at com.google.api.client.auth.oauth2.StoredCredential.getDefaultDataStore(StoredCredential.java:171)
    at com.google.api.client.auth.oauth2.AuthorizationCodeFlow$Builder.setDataStoreFactory(AuthorizationCodeFlow.java:736)
    at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder.setDataStoreFactory(GoogleAuthorizationCodeFlow.java:209)
    at com.demo.gapps.server.FetchNewFilesCron.authorize(FetchNewFilesCron.java:233)
    at com.demo.gapps.server.FetchNewFilesCron.getDriveService(FetchNewFilesCron.java:244)
    at com.demo.gapps.server.FetchNewFilesCron.doGet(FetchNewFilesCron.java:97)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:128)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:50)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
    at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:98)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:511)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

The code from the tutorial that I am using to authorize that generated teh potential exception is as follows:

* @return an authorized Credential object.
         * @throws IOException
         */
        public static Credential authorize() throws IOException {

            System.out.println("file path is "+DATA_STORE_DIR.getPath());

            // Load client secrets.
            InputStream in =
                FetchNewFilesCron.class.getResourceAsStream("/client_secrets.json");
            GoogleClientSecrets clientSecrets =
                GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));



            // Build flow and trigger user authorization request.
            GoogleAuthorizationCodeFlow flow =
                    new GoogleAuthorizationCodeFlow.Builder(
                            HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                    .setDataStoreFactory(DATA_STORE_FACTORY)
                    .setAccessType("offline")
                    .build();
            Credential credential = new AuthorizationCodeInstalledApp(
                flow, new LocalServerReceiver()).authorize("user");
            System.out.println(
                    "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath());
            return credential;
        }

The error is probably at .setDataStoreFactory(DATA_STORE_FACTORY)

Can anybody please guide me with this error.

Thanks,

Furqan Ahmed
  • 159
  • 1
  • 3
  • 17

2 Answers2

0

The exception indicates the the end of file (EOF), or the end of stream has been reached unexpectedly. Also, this exception is mainly used by DataInputStreams, in order to signal the end of stream. However, notice that other input operations may return a special value upon the end of a stream, instead of throwing an EOFException.

The EOFException class extends the IOException class, which is the general class of exceptions produced by failed or interrupted I/O operations. Moreover, it implements the Serializable interface. Also, it is defined as a checked exception and thus, it must be declared in a method or a constructor’s throws clause.

DataInputStreams provide methods that can read primitive Java data types from an underlying input stream in a machine-independent way. An application writes data, by using the methods provided by the OutputStream class or the DataOutputStream class.

For more information, check this related SO ticket which discuss about EOFException and DataInputStream: java.io.EOFException when try to read from a socket

Community
  • 1
  • 1
Android Enthusiast
  • 4,826
  • 2
  • 15
  • 30
  • Actually this exception I was getting on my local environment while on Google App Engine development server I am getting following exception. Can you please guide me with that – Furqan Ahmed Jul 30 '16 at 14:33
  • java.security.AccessControlException: access denied ("java.io.FilePermission" "/" "read") at java.security.AccessControlContext.checkPermission(AccessControlContext.java:484) at java.security.AccessController.checkPermission(AccessController.java:698) at java.lang.SecurityManager.checkPermission(SecurityManager.java:549) at com.google.apphosting.runtime.security.CustomSecurityManager.checkPermission(CustomSecurityManager.java:55) at java.lang.SecurityManager.checkRead(SecurityManager.java:888) at java.io.File.exists(File.java:877) – Furqan Ahmed Jul 30 '16 at 14:34
  • it seems google app engine is not allowing me to create and read a file. what could be the possible issue?? – Furqan Ahmed Jul 30 '16 at 14:34
0

I know that this is an old post, but I had almost the exact same stacktrace and the only answer wasn't very helpful. So, I'm posting in hopes that it will help those that come after me.

The issue appears to be related to file/directory permissions. As soon as I changed the filepath being used by the FileDataStoreFactory to a directory that my application had read/write permissions to, OAuth2 worked correctly. So, make sure that you are using a non-restricted filepath and that all of the directories have the correct ownership and permissions.

Snake14
  • 416
  • 2
  • 7