當前位置: 首頁>>代碼示例>>Java>>正文


Java IOUtils.readFully方法代碼示例

本文整理匯總了Java中org.apache.poi.util.IOUtils.readFully方法的典型用法代碼示例。如果您正苦於以下問題:Java IOUtils.readFully方法的具體用法?Java IOUtils.readFully怎麽用?Java IOUtils.readFully使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.poi.util.IOUtils的用法示例。


在下文中一共展示了IOUtils.readFully方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: parse

import org.apache.poi.util.IOUtils; //導入方法依賴的package包/類
public void parse(InputStream stream,
      ContentHandler handler, Metadata metadata,
      ParseContext parseContext) throws IOException, SAXException,
      TikaException 
{
   byte[] initial4 = new byte[4];
   InputStream wrapped;
   // Preserve TikaInputStreams as TikaInputStreams as they require less memory to process
   if (stream.markSupported())
   {
      stream.mark(initial4.length);
      IOUtils.readFully(stream, initial4);
      stream.reset();
      wrapped = stream;
   }
   else
   {
      PushbackInputStream inp = new PushbackInputStream(stream, 4);
      IOUtils.readFully(inp, initial4);
      inp.unread(initial4);
      wrapped = inp;
   }
   
   // Which is it?
   if(initial4[0] == POIFSConstants.OOXML_FILE_HEADER[0] &&
      initial4[1] == POIFSConstants.OOXML_FILE_HEADER[1] &&
      initial4[2] == POIFSConstants.OOXML_FILE_HEADER[2] &&
      initial4[3] == POIFSConstants.OOXML_FILE_HEADER[3])
   {
      ooxmlParser.parse(wrapped, handler, metadata, parseContext);
   }
   else
   {
      ole2Parser.parse(wrapped, handler, metadata, parseContext);
   }
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:37,代碼來源:TikaOfficeDetectParser.java

示例2: ResourceBlock

import org.apache.poi.util.IOUtils; //導入方法依賴的package包/類
private ResourceBlock(InputStream stream) throws IOException, TikaException {
   // Verify the signature
   long sig = EndianUtils.readIntBE(stream);
   if(sig != SIGNATURE) {
      throw new TikaException("Invalid Image Resource Block Signature Found, got " +
            sig + " 0x" + Long.toHexString(sig) + " but the spec defines " + SIGNATURE);
   }
   
   // Read the block
   id = EndianUtils.readUShortBE(stream);
   
   StringBuffer nameB = new StringBuffer();
   int nameLen = 0;
   while(true) {
      int v = stream.read();
      nameLen++;
      
      if(v == 0) {
         // The name length is padded to be even
         if(nameLen % 2 == 1) {
            stream.read();
            nameLen++;
         }
         break;
      } else {
         nameB.append((char)v);
      }
      name = nameB.toString();
   }
   
   int dataLen = EndianUtils.readIntBE(stream);
   if(dataLen %2 == 1) {
       // Data Length is even padded
       dataLen = dataLen + 1;
   }
   totalLength = 4 + 2 + nameLen + 4 + dataLen;
   
   data = new byte[dataLen];
   IOUtils.readFully(stream, data);
}
 
開發者ID:kolbasa,項目名稱:OCRaptor,代碼行數:41,代碼來源:PSDParser.java

示例3: skipToBoundary

import org.apache.poi.util.IOUtils; //導入方法依賴的package包/類
private static void skipToBoundary(int length, InputStream inp) throws IOException {
   // Data is always padded out to a 4 byte boundary
   if(length % 4 != 0) {
      int skip = 4 - (length % 4);
      byte[] padding = new byte[skip];
      IOUtils.readFully(inp, padding);
   }
}
 
開發者ID:rmage,項目名稱:gnvc-ims,代碼行數:9,代碼來源:MAPIAttribute.java

示例4: TNEFAttribute

import org.apache.poi.util.IOUtils; //導入方法依賴的package包/類
/**
 * Constructs a single new attribute from the id, type,
 *  and the contents of the stream
 */
protected TNEFAttribute(int id, int type, InputStream inp) throws IOException {
   this.type = type;
   int length = LittleEndian.readInt(inp);
   
   property = TNEFProperty.getBest(id, type);
   data = new byte[length];
   IOUtils.readFully(inp, data);
   
   checksum = LittleEndian.readUShort(inp);
}
 
開發者ID:rmage,項目名稱:gnvc-ims,代碼行數:15,代碼來源:TNEFAttribute.java

示例5: testGuessMimetypeForFile

import org.apache.poi.util.IOUtils; //導入方法依賴的package包/類
public void testGuessMimetypeForFile() throws Exception
{
    // Correct ones
    assertEquals(
            "application/msword", 
            mimetypeService.guessMimetype("something.doc", openQuickTestFile("quick.doc"))
    );
    assertEquals(
            "application/msword", 
            mimetypeService.guessMimetype("SOMETHING.DOC", openQuickTestFile("quick.doc"))
    );
    
    // Incorrect ones, Tika spots the mistake
    assertEquals(
            "application/msword", 
            mimetypeService.guessMimetype("something.pdf", openQuickTestFile("quick.doc"))
    );
    assertEquals(
            "application/pdf", 
            mimetypeService.guessMimetype("something.doc", openQuickTestFile("quick.pdf"))
    );
    
    // Ones where we use a different mimetype to the canonical one
    assertEquals(
            "image/bmp", // Officially image/x-ms-bmp 
            mimetypeService.guessMimetype("image.bmp", openQuickTestFile("quick.bmp"))
    );
    
    // Ones where we know about the parent, and Tika knows about the details
    assertEquals(
          "application/dita+xml", // Full version:  application/dita+xml;format=concept
          mimetypeService.guessMimetype("concept.dita", openQuickTestFile("quickConcept.dita"))
    );
    
    // Alfresco Specific ones, that Tika doesn't know about
    assertEquals(
          "application/acp", 
          mimetypeService.guessMimetype("something.acp", openQuickTestFile("quick.acp"))
    );

    
    // Where the file is corrupted
    File tmp = File.createTempFile("alfresco", ".tmp");
    ContentReader reader = openQuickTestFile("quick.doc");
    InputStream inp = reader.getContentInputStream();
    byte[] trunc = new byte[512+256];
    IOUtils.readFully(inp, trunc);
    inp.close();
    FileOutputStream out = new FileOutputStream(tmp);
    out.write(trunc);
    out.close();
    ContentReader truncReader = new FileContentReader(tmp);
    
    // Because the file is truncated, Tika won't be able to process the contents
    //  of the OLE2 structure
    // So, it'll fall back to just OLE2, but it won't fail
    assertEquals(
            "application/x-tika-msoffice", 
            mimetypeService.guessMimetype(null, truncReader)
    );
    // But with the filename it'll be able to use the .doc extension
    //  to guess at it being a .Doc file
    assertEquals(
          "application/msword", 
          mimetypeService.guessMimetype("something.doc", truncReader)
    );
    
    // Lotus notes EML files (ALF-16381 / TIKA-1042)
    assertEquals(
          "message/rfc822", 
          mimetypeService.guessMimetype("something.eml", openQuickTestFile("quickLotus.eml"))
    );
}
 
開發者ID:Alfresco,項目名稱:alfresco-repository,代碼行數:74,代碼來源:MimetypeMapContentTest.java

示例6: parse

import org.apache.poi.util.IOUtils; //導入方法依賴的package包/類
public void parse(
        InputStream stream, ContentHandler handler,
        Metadata metadata, ParseContext context)
        throws IOException, SAXException, TikaException {
    // Check for the magic header signature
    byte[] signature = new byte[4];
    IOUtils.readFully(stream, signature);
    if(signature[0] == (byte)'8' && signature[1] == (byte)'B' &&
       signature[2] == (byte)'P' && signature[3] == (byte)'S') {
       // Good, signature found
    } else {
       throw new TikaException("PSD/PSB magic signature invalid");
    }
    
    // Check the version
    int version = EndianUtils.readUShortBE(stream);
    if(version == 1 || version == 2) {
       // Good, we support these two
    } else {
       throw new TikaException("Invalid PSD/PSB version " + version);
    }
    
    // Skip the reserved block
    IOUtils.readFully(stream, new byte[6]);
    
    // Number of channels in the image
    int numChannels = EndianUtils.readUShortBE(stream);
    // TODO Identify a suitable metadata key for this

    // Width and Height
    int height = EndianUtils.readIntBE(stream);
    int width = EndianUtils.readIntBE(stream);
    metadata.set(TIFF.IMAGE_LENGTH, height);
    metadata.set(TIFF.IMAGE_WIDTH, width);
    
    // Depth (bits per channel)
    int depth = EndianUtils.readUShortBE(stream);
    metadata.set(TIFF.BITS_PER_SAMPLE, Integer.toString(depth));
    
    // Colour mode
    // Bitmap = 0; Grayscale = 1; Indexed = 2; RGB = 3; CMYK = 4; Multichannel = 7; Duotone = 8; Lab = 9.
    int colorMode = EndianUtils.readUShortBE(stream);
    // TODO Identify a suitable metadata key for this
    
    // Next is the Color Mode section
    // We don't care about this bit
    long colorModeSectionSize = EndianUtils.readIntBE(stream);
    stream.skip(colorModeSectionSize);

    // Next is the Image Resources section
    // Check for certain interesting keys here
    long imageResourcesSectionSize = EndianUtils.readIntBE(stream);
    long read = 0;
    while(read < imageResourcesSectionSize) {
       ResourceBlock rb = new ResourceBlock(stream);
       read += rb.totalLength;
       
       // Is it one we can do something useful with?
       if(rb.id == ResourceBlock.ID_CAPTION) {
          metadata.add(TikaCoreProperties.DESCRIPTION, rb.getDataAsString()); 
       } else if(rb.id == ResourceBlock.ID_EXIF_1) {
          // TODO Parse the EXIF info
       } else if(rb.id == ResourceBlock.ID_EXIF_3) {
          // TODO Parse the EXIF info
       } else if(rb.id == ResourceBlock.ID_XMP) {
          // TODO Parse the XMP info
       }
    }
    
    // Next is the Layer and Mask Info
    // Finally we have Image Data
    // We can't do anything with these parts
    
    // We don't have any helpful text, sorry...
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    xhtml.endDocument();
}
 
開發者ID:kolbasa,項目名稱:OCRaptor,代碼行數:79,代碼來源:PSDParser.java

示例7: readProperties

import org.apache.poi.util.IOUtils; //導入方法依賴的package包/類
protected void readProperties(InputStream value) throws IOException {
     boolean going = true;
     while (going) {
        try {
           // Read in the header
           int typeID = LittleEndian.readUShort(value);
           int id     = LittleEndian.readUShort(value);
           long flags = LittleEndian.readUInt(value);
           
           // Turn the Type and ID into helper objects
           MAPIType type = Types.getById(typeID);
           MAPIProperty prop = MAPIProperty.get(id);
           if (prop.usualType != type) {
              // Oh dear, something has gone wrong...
              logger.log(POILogger.WARN, "Type mismatch, expected ", type, " but got ", prop.usualType);
              going = false;
              break;
           }
           
           // Work out how long the "data" is
           // This might be the actual data, or just a pointer
           //  to another chunk which holds the data itself
           boolean isPointer = false;
           int length = type.getLength();
           if (! type.isFixedLength()) {
              isPointer = true;
              length = 8;
           }
           
           // Grab the data block
           byte[] data = new byte[length];
           IOUtils.readFully(value, data);
           
           // Skip over any padding
           if (length < 8) {
              byte[] padding = new byte[8-length];
              IOUtils.readFully(value, padding);
           }
           
           // Wrap and store
           PropertyValue propVal = null;
           if (isPointer) {
              // TODO Pointer type which can do lookup
           }
           else if (type == Types.LONG_LONG) {
              propVal = new LongLongPropertyValue(prop, flags, data);
           }
           else if (type == Types.TIME) {
              propVal = new TimePropertyValue(prop, flags, data);
           }
           // TODO Add in the rest of the type
           else {
              propVal = new PropertyValue(prop, flags, data);
           }
           
           if (properties.get(prop) == null) {
              properties.put(prop, new ArrayList<PropertyValue>());
           }
           properties.get(prop).add(propVal);
        } catch (BufferUnderrunException e) {
           // Invalid property, ended short
           going = false;
        }
     }
}
 
開發者ID:rmage,項目名稱:gnvc-ims,代碼行數:66,代碼來源:PropertiesChunk.java


注:本文中的org.apache.poi.util.IOUtils.readFully方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。