AnsweredAssumed Answered

Error in ContentUtils.convertToByteArray(inputStream)

Question asked by nimind on May 16, 2007
Latest reply on Dec 20, 2007 by robertoroberto
Only transform the first 8192 bytes, if you have a file greater than 8k, it won't work.
Code in Alfresco:

byte[] result = null;
       
        if (inputStream.available() > 0)
        {
            result = new byte[inputStream.available()];       
            inputStream.read(result);;
        }    
       
        return result;

it must be a while, not if. inputStream.available() only returns max 8192 bytes. If you file have more than 8k:


                 bytes = new byte[z];
            in = wdr.getMethodData();   
            int x=0;
            while(in.available() > 0)
            {
               in.read(bytes, x*8192, in.available());
               x++;
            }
You must know the file length to create the array. I'm using WEBDAV. My code:


public byte[] getKBDocument(String sDocumentDMId, String sSolutionDMId)
   {
      Store store = null; //almacen
      Reference reference = null; //reference
       String[] sDirs = null; //path directories
       String sPath = null; //Alfresco path
       String sFinalPath = ""; //webdav path
       byte[] bytes = null;
       Node node = null;
       InputStream in = null;

      
       store = new Store(Constants.WORKSPACE_STORE, STORE);
      try
       {
         AuthenticationUtils.startSession(USER, PASS); 
          logger.info("getKBDocument");
         
          node = searchNode(store, sDocumentDMId);
          reference = node.getReference();

                     //Get path
         sPath = reference.getPath();
         
         logger.info("getKBDocument: Alfresco Path: "+sPath);
         sPath = sPath.replace("cm:", "");
         sPath = sPath.replace("_x0020_", " ");
         sPath = sPath.replace("_x003", "");
         sPath = sPath.replace("_", "");
         sDirs = sPath.split("/");
         
         for (int i=3; i < sDirs.length; i++)
            sFinalPath = sFinalPath+"/"+sDirs[i];
         
         HttpURL hrl = new HttpURL(WEBDAV+ROOT_NODE+sFinalPath);
         logger.info("getKBDocument: WEBDAV Path: "+WEBDAV+ROOT_NODE+sFinalPath);
          
         hrl.setUserinfo("admin","admin");
           WebdavResource wdr = new WebdavResource(hrl);
           int z = (int)wdr.getGetContentLength();
           bytes = new byte[z];
            in = wdr.getMethodData();   
            int x=0;
            while(in.available() > 0)
            {
               in.read(bytes, x*8192, in.available());
               x++;
            }
                 wdr.close();
               }
       catch(MalformedURLException mue)
       {
          System.out.println(mue.getMessage());
          //mue.printStackTrace();
          //throw new DocumentManagementExceotion();
       }
       catch(HttpException he)
       {
          System.out.println(he.getMessage());
          //he.printStackTrace();
       }
       catch(IOException ioe)
       {
          System.out.println(ioe.getMessage());
          //ioe.printStackTrace();
       }
       catch(Exception e)
       {
          System.out.println(e.getMessage());
       }
      return bytes;
      
      
   }


private Node searchNode(Store store, String uuid) throws Exception{
      Node[] nodes = null; //all nodes
        Node myNode = null; //node found
        Reference reference = null; //reference
        Predicate predicate = null; //predicateç
        RepositoryServiceSoapBindingStub repositoryService = null; //repository
       
        logger.info("searchNode");
      
        repositoryService = WebServiceFactory.getRepositoryService();
      reference = new Reference(store, uuid, null);
        predicate = new Predicate(new Reference[]{reference}, null, null);
       

        try {
           nodes = repositoryService.get(predicate);
           // Obtenemos el nodo.
           myNode = nodes[0];
           //System.out.println("hola:"+myNode.getReference().getUuid());     
        } catch (RepositoryFault e) {
           System.out.println(ERROR_MANAGER+ " Data not found");
        }
        logger.info("searchNode node"+myNode.toString());
        logger.info("searchNode exit");
      return myNode;
   }

Outcomes