/// <summary>read</summary> /// <exception cref="System.Exception"/> public virtual void Read(File file, PDXReaderListener pdxReaderListener) { try { /* * start */ pdxReaderListener.Start(); /* * set up streams */ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream (file)); LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream (bufferedInputStream); try { /* * mark and read the headers */ bufferedInputStream.Mark(MaxHeaderSize); ReadHeaders(littleEndianDataInputStream); /* * call the api */ pdxReaderListener.Header(dbTableHeader); /* * read the block data */ bufferedInputStream.Reset(); ReadBlocks(bufferedInputStream, pdxReaderListener); /* * done */ pdxReaderListener.Finish(); } finally { littleEndianDataInputStream.Close(); bufferedInputStream.Close(); } } catch (Exception e) { throw new Exception("Exception in read", e); } }
public static FileType?DetectFileType([NotNull] BufferedInputStream inputStream) { int maxByteCount = _root.GetMaxDepth(); inputStream.Mark(maxByteCount); sbyte[] bytes = new sbyte[maxByteCount]; int bytesRead = inputStream.Read(bytes); if (bytesRead == -1) { throw new IOException("Stream ended before file's magic number could be determined."); } inputStream.Reset(); //noinspection ConstantConditions return(_root.Find(bytes)); }
//import eu.europa.ec.markt.dss.validation.asic.ASiCXMLDocumentValidator; //import eu.europa.ec.markt.dss.validation.pades.PDFDocumentValidator; //import eu.europa.ec.markt.dss.validation.xades.XMLDocumentValidator; /// <summary>Guess the document format and return an appropriate document</summary> /// <param name="document"></param> /// <returns></returns> /// <exception cref="System.IO.IOException"></exception> public static SignedDocumentValidator FromDocument(Document document) { InputStream input = null; try { input = new BufferedInputStream(document.OpenStream()); input.Mark(5); byte[] preamble = new byte[5]; int read = input.Read(preamble); input.Reset(); if (read < 5) { throw new RuntimeException("Not a signed document"); } string preambleString = Sharpen.Runtime.GetStringForBytes(preamble); if (Sharpen.Runtime.GetBytesForString(preambleString)[0] == unchecked ((int)(0x30) )) { try { return(new CMSDocumentValidator(document)); } catch (CmsException) { throw new IOException("Not a valid CAdES file"); } } else { throw new RuntimeException("Document format not recognized/handled"); } } finally { if (input != null) { try { input.Close(); } catch (IOException) { } } } }
/// <exception cref="System.IO.IOException"/> private BufferedInputStream ReadStreamHeader() { // We are flexible enough to allow the compressed stream not to // start with the header of BZ. So it works fine either we have // the header or not. if (base.@in != null) { bufferedIn.Mark(HeaderLen); byte[] headerBytes = new byte[HeaderLen]; int actualRead = bufferedIn.Read(headerBytes, 0, HeaderLen); if (actualRead != -1) { string header = new string(headerBytes, Charsets.Utf8); if (string.CompareOrdinal(header, Header) != 0) { bufferedIn.Reset(); } else { this.isHeaderStripped = true; // In case of BYBLOCK mode, we also want to strip off // remaining two character of the header. if (this.readMode == SplittableCompressionCodec.READ_MODE.Byblock) { actualRead = bufferedIn.Read(headerBytes, 0, SubHeaderLen); if (actualRead != -1) { this.isSubHeaderStripped = true; } } } } } if (bufferedIn == null) { throw new IOException("Failed to read bzip2 stream."); } return(bufferedIn); }
/// <summary>read</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> public virtual void Read(File file) { try { /* * set up streams */ BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream (file)); LittleEndianDataInputStream littleEndianDataInputStream = new LittleEndianDataInputStream (bufferedInputStream); try { /* * mark and read the headers */ bufferedInputStream.Mark(MaxHeaderSize); ReadHeaders(littleEndianDataInputStream); /* * read the block data */ bufferedInputStream.Reset(); ReadBlocks(bufferedInputStream); } finally { littleEndianDataInputStream.Close(); bufferedInputStream.Close(); } } catch (Exception e) { throw new PDXReaderException("Exception in read", e); } }
/// <summary>read block data</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> private void ReadBlocks(BufferedInputStream bufferedInputStream, PDXReaderListener pdxReaderListener) { try { /* * init the array */ blocks = new Hashtable <int, DBTableBlock>(); /* * skip to the first block */ int nSkip = dbTableHeader.GetBlockSize().GetValue() * 1024; if (nSkip == bufferedInputStream.Skip(nSkip)) { /* * walk blocks */ int blocksInUse = dbTableHeader.GetBlocksInUse(); for (int i = 0; i < blocksInUse; i++) { /* * block */ DBTableBlock pdxTableBlock = new DBTableBlock(i + 1, dbTableHeader.CalculateRecordsPerBlock (), dbTableHeader.GetFields()); /* * mark at the start of the block */ bufferedInputStream.Mark(MaxBlockSize); /* * read the block data */ pdxTableBlock.Read(pdxReaderListener, bufferedInputStream); /* * store it. blocks are numbered from 1, not from 0. */ blocks[pdxTableBlock.GetBlockNumber()] = pdxTableBlock; /* * reset to the start of the block */ bufferedInputStream.Reset(); /* * skip ahead to next block */ bufferedInputStream.Skip(dbTableHeader.GetBlockSize().GetValue() * 1024); } } else { throw new PDXReaderException("File format exception"); } } catch (Exception e) { throw new PDXReaderException("Exception in readBlocks", e); } }
public static string getCharset(string fileName) { BufferedInputStream bis = null; string charset = "GBK"; byte[] first3Bytes = new byte[3]; try { bool check = false; bis = new BufferedInputStream(new System.IO.FileStream(fileName, System.IO.FileMode.OpenOrCreate)); bis.Mark(0); int read = bis.Read(first3Bytes, 0, 3); if (read == -1) { return(charset); } if (first3Bytes[0] == (byte)0xFF && first3Bytes[1] == (byte)0xFE) { charset = "UTF-16LE"; check = true; } else if (first3Bytes[0] == (byte)0xFE && first3Bytes[1] == (byte)0xFF) { charset = "UTF-16BE"; check = true; } else if (first3Bytes[0] == (byte)0xEF && first3Bytes[1] == (byte)0xBB && first3Bytes[2] == (byte)0xBF) { charset = "UTF-8"; check = true; } bis.Mark(0); if (!check) { while ((read = bis.Read()) != -1) { if (read >= 0xF0) { break; } if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK { break; } if (0xC0 <= read && read <= 0xDF) { read = bis.Read(); if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF) // (0x80 - 0xBF),也可能在GB编码内 { continue; } else { break; } } else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小 read = bis.Read(); if (0x80 <= read && read <= 0xBF) { read = bis.Read(); if (0x80 <= read && read <= 0xBF) { charset = "UTF-8"; break; } else { break; } } else { break; } } } } } catch (Exception e) { e.PrintStackTrace(); } finally { if (bis != null) { try { bis.Close(); } catch (IOException e) { e.PrintStackTrace(); } } } return(charset); }
/// <exception cref="System.IO.IOException"></exception> /// <exception cref="NGit.Errors.CorruptObjectException"></exception> private void ReadFrom(InputStream inStream) { BufferedInputStream @in = new BufferedInputStream(inStream); MessageDigest md = Constants.NewMessageDigest(); // Read the index header and verify we understand it. // byte[] hdr = new byte[20]; IOUtil.ReadFully(@in, hdr, 0, 12); md.Update(hdr, 0, 12); if (!Is_DIRC(hdr)) { throw new CorruptObjectException(JGitText.Get().notADIRCFile); } int ver = NB.DecodeInt32(hdr, 4); bool extended = false; if (ver == 3) { extended = true; } else { if (ver != 2) { throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().unknownDIRCVersion , ver)); } } entryCnt = NB.DecodeInt32(hdr, 8); if (entryCnt < 0) { throw new CorruptObjectException(JGitText.Get().DIRCHasTooManyEntries); } // Load the individual file entries. // int infoLength = DirCacheEntry.GetMaximumInfoLength(extended); byte[] infos = new byte[infoLength * entryCnt]; sortedEntries = new DirCacheEntry[entryCnt]; MutableInteger infoAt = new MutableInteger(); for (int i = 0; i < entryCnt; i++) { sortedEntries[i] = new DirCacheEntry(infos, infoAt, @in, md); } snapshot = FileSnapshot.Save(liveFile); // After the file entries are index extensions, and then a footer. // for (; ;) { @in.Mark(21); IOUtil.ReadFully(@in, hdr, 0, 20); if (@in.Read() < 0) { // No extensions present; the file ended where we expected. // break; } @in.Reset(); md.Update(hdr, 0, 8); IOUtil.SkipFully(@in, 8); long sz = NB.DecodeUInt32(hdr, 4); switch (NB.DecodeInt32(hdr, 0)) { case EXT_TREE: { if (int.MaxValue < sz) { throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().DIRCExtensionIsTooLargeAt , FormatExtensionName(hdr), sz)); } byte[] raw = new byte[(int)sz]; IOUtil.ReadFully(@in, raw, 0, raw.Length); md.Update(raw, 0, raw.Length); tree = new DirCacheTree(raw, new MutableInteger(), null); break; } default: { if (hdr[0] >= 'A' && ((sbyte)hdr[0]) <= 'Z') { // The extension is optional and is here only as // a performance optimization. Since we do not // understand it, we can safely skip past it, after // we include its data in our checksum. // SkipOptionalExtension(@in, md, hdr, sz); } else { // The extension is not an optimization and is // _required_ to understand this index format. // Since we did not trap it above we must abort. // throw new CorruptObjectException(MessageFormat.Format(JGitText.Get().DIRCExtensionNotSupportedByThisVersion , FormatExtensionName(hdr))); } break; } } } byte[] exp = md.Digest(); if (!Arrays.Equals(exp, hdr)) { throw new CorruptObjectException(JGitText.Get().DIRCChecksumMismatch); } }
/// <summary>read block data</summary> /// <exception cref="Com.Khubla.Pdxreader.Api.PDXReaderException"/> private void ReadBlocks(BufferedInputStream bufferedInputStream) { try { /* * init the array */ blocks = new List <PXIndexBlock>(); /* * skip to the first index block */ int nSkip = pxFileHeader.GetBlockSize().GetValue() * 1024; if (nSkip == bufferedInputStream.Skip(nSkip)) { /* * walk index blocks */ int blocksInUse = pxFileHeader.GetBlocksInUse(); for (int i = 0; i < blocksInUse; i++) { /* * block */ PXIndexBlock pxIndexBlock = new PXIndexBlock(); /* * mark at the start of the block */ bufferedInputStream.Mark(MaxBlockSize); /* * read the block data */ pxIndexBlock.Read(bufferedInputStream); /* * store it. blocks are numbered from 1, not from 0. */ blocks.Add(pxIndexBlock); /* * reset to the start of the block */ bufferedInputStream.Reset(); /* * skip ahead to next block */ bufferedInputStream.Skip(pxFileHeader.GetBlockSize().GetValue() * 1024); } } else { throw new PDXReaderException("File format exception"); } } catch (Exception e) { throw new PDXReaderException("Exception in readBlocks", e); } }