public OutStream writeNext(Uri path, DateTime modifyTime) { if (m_zipOut == null) { throw UnsupportedErr.make("Zip not opened for writing").val; } if (path.frag() != null) { throw ArgErr.make("Path must not contain fragment: " + path).val; } if (path.queryStr() != null) { throw ArgErr.make("Path must not contain query: " + path).val; } try { string zipPath = path.ToString(); if (zipPath.StartsWith("/")) { zipPath = zipPath.Substring(1); } ZipEntry entry = new ZipEntry(zipPath); entry.DateTime = new System.DateTime(modifyTime.dotnet()); m_zipOut.PutNextEntry(entry); return(new ZipSysOutStream(m_zipOut)); } catch (System.IO.IOException e) { throw IOErr.make(e).val; } }
public static Buf fromHex(string s) { int slen = s.Length; byte[] buf = new byte[slen / 2]; int[] hexInv = Buf.hexInv; int size = 0; for (int i = 0; i < slen; ++i) { int c0 = s[i]; int n0 = c0 < 128 ? hexInv[c0] : -1; if (n0 < 0) { continue; } int n1 = -1; if (++i < slen) { int c1 = s[i]; n1 = c1 < 128 ? hexInv[c1] : -1; } if (n1 < 0) { throw IOErr.make("Invalid hex str").val; } buf[size++] = (byte)((n0 << 4) | n1); } return(new MemBuf(buf, size)); }
public override InStream @in(Long bufSize) { try { System.IO.Stream ins; if (m_parent is Zip) { // never buffer if using ZipInputStream ins = new ZipEntryInputStream((m_parent as Zip).m_zipIn); } else { ins = (m_parent as ZipFile).GetInputStream(m_entry); // buffer if specified if (bufSize != null && bufSize.longValue() != 0) { ins = new System.IO.BufferedStream(ins, bufSize.intValue()); } } // return as fan stream return(new SysInStream(ins)); } catch (System.IO.IOException e) { throw IOErr.make(e).val; } }
public override Buf mmap(string mode, long pos, Long size) { try { // map mode /* * MmapBuf.FileRights mode; * if (mode.val.equals("r")) { mode = MmapBuff.FileRights.Read; } * else if (mode.val.equals("rw")) { mode = MmapBuff.FileRights.ReadWrite; } * else if (mode.val.equals("p")) throw ArgErr.make("Private mode not supported.").val; * else throw ArgErr.make("Invalid mode: " + mode).val; * * // verify mode is 'r' or 'rw' * if (mode.val.equals("p")) throw ArgErr.make("Private mode not supported.").val; * if (!mode.val.equals("r") || !mode.val.equals("rw")) throw ArgErr.make("Invalid mode: " + mode).val; * * // if size is null, use file size * if (size == null) size = size(); * * // traverse the various Java APIs * //RandomAccessFile fp = new RandomAccessFile(file, rw); * //FileChannel chan = fp.getChannel(); * //MappedByteBuffer mmap = chan.map(mm, pos.val, size.val); */ return(new MmapBuf(this, mode, pos, size)); } catch (System.IO.IOException e) { throw IOErr.make(e).val; } }
////////////////////////////////////////////////////////////////////////// // IO ////////////////////////////////////////////////////////////////////////// public override Buf open(string mode) { try { System.IO.FileMode fm; System.IO.FileAccess fa; string s = mode; if (s == "r") { fm = System.IO.FileMode.Open; fa = System.IO.FileAccess.Read; } else if (s == "w") { fm = System.IO.FileMode.OpenOrCreate; fa = System.IO.FileAccess.Write; } else if (s == "rw") { fm = System.IO.FileMode.OpenOrCreate; fa = System.IO.FileAccess.ReadWrite; } else { throw new System.IO.IOException("Unsupported mode: " + mode); } return(new FileBuf(this, (m_file as FileInfo).Open(fm, fa))); } catch (System.IO.IOException e) { throw IOErr.make(e).val; } }
public override void delete() { if (!exists()) { return; } if (m_file is DirectoryInfo) { List kids = list(); for (int i = 0; i < kids.sz(); i++) { (kids.get(i) as File).delete(); } } try { m_file.Delete(); m_file.Refresh(); } catch (System.Exception e) { throw IOErr.make("Cannot delete: " + m_file, e).val; } }
public override sealed string toHex() { try { long oldPos = getPos(); int size = (int)getSize(); byte[] temp = this.temp(); char[] hexChars = Buf.hexChars; StringBuilder s = new StringBuilder(size * 2); setPos(0); int total = 0; while (total < size) { int n = m_stream.Read(temp, 0, Math.Min(temp.Length, size - total)); for (int i = 0; i < n; ++i) { int b = temp[i] & 0xFF; s.Append(hexChars[b >> 4]).Append(hexChars[b & 0xf]); } total += n; } setPos(oldPos); return(s.ToString()); } catch (IOException e) { throw IOErr.make(e).val; } }
public virtual long readS8() { long c1 = r(); long c2 = r(); long c3 = r(); long c4 = r(); long c5 = r(); long c6 = r(); long c7 = r(); long c8 = r(); if ((c1 | c2 | c3 | c4 | c5 | c6 | c7 | c8) < 0) { throw IOErr.make("Unexpected end of stream").val; } if (m_bigEndian) { return((c1 << 56) + (c2 << 48) + (c3 << 40) + (c4 << 32) + (c5 << 24) + (c6 << 16) + (c7 << 8) + c8); } else { return((c8 << 56) + (c7 << 48) + (c6 << 40) + (c5 << 32) + (c4 << 24) + (c3 << 16) + (c2 << 8) + c1); } }
public override void encode(char c, OutStream @out) { if (c > 0xFF) { throw IOErr.make("Invalid ISO-8859-1 char").val; } @out.w((c >> 0) & 0xFF); }
internal override void pipeTo(byte[] dst, int dstPos, int len) { if (m_pos + len > m_size) { throw IOErr.make("Not enough bytes to write").val; } System.Array.Copy(m_buf, m_pos, dst, dstPos, len); m_pos += len; }
public virtual bool readBool() { int n = r(); if (n < 0) { throw IOErr.make("Unexpected end of stream").val; } return(n == 0 ? false : true); }
public override sealed OutStream w(int v) { try { p.m_stream.WriteByte((byte)v); return(this); } catch (IOException e) { throw IOErr.make(e).val; } catch (System.NotSupportedException e) { throw IOErr.make(e.Message, e).val; } }
public override OutStream writeBuf(Buf other, long n) { try { other.pipeTo(p.m_stream, n); return(this); } catch (IOException e) { throw IOErr.make(e).val; } catch (System.NotSupportedException e) { throw IOErr.make(e.Message, e).val; } }
public virtual long readS1() { int c = r(); if (c < 0) { throw IOErr.make("Unexpected end of stream").val; } return((sbyte)c); }
private OutStream writeUtfString(string s) { int slen = s.Length; int utflen = 0; // first we have to figure m_out the utf Length for (int i = 0; i < slen; ++i) { int c = s[i]; if (c <= 0x007F) { utflen += 1; } else if (c > 0x07FF) { utflen += 3; } else { utflen += 2; } } // sanity check if (utflen > 65536) { throw IOErr.make("String too big").val; } // write Length as 2 byte value w((utflen >> 8) & 0xFF); w((utflen >> 0) & 0xFF); // write characters for (int i = 0; i < slen; ++i) { int c = s[i]; if (c <= 0x007F) { w(c); } else if (c > 0x07FF) { w(0xE0 | ((c >> 12) & 0x0F)); w(0x80 | ((c >> 6) & 0x3F)); w(0x80 | ((c >> 0) & 0x3F)); } else { w(0xC0 | ((c >> 6) & 0x1F)); w(0x80 | ((c >> 0) & 0x3F)); } } return(this); }
internal override void setSize(long x) { try { m_stream.SetLength(x); } catch (IOException e) { throw IOErr.make(e).val; } }
internal override long getPos() { try { return(m_stream.Position); } catch (IOException e) { throw IOErr.make(e).val; } }
////////////////////////////////////////////////////////////////////////// // Buf Support ////////////////////////////////////////////////////////////////////////// internal override long getSize() { try { return(m_stream.Length); } catch (IOException e) { throw IOErr.make(e).val; } }
internal override void pipeTo(Stream dst, long lenLong) { int len = (int)lenLong; if (m_pos + len > m_size) { throw IOErr.make("Not enough bytes to write").val; } dst.Write(m_buf, m_pos, len); m_pos += len; }
/* * internal override void pipeTo(ByteBuffer dst, int len) * { * try * { * byte[] temp = temp(); * int total = 0; * while (total < len) * { * int n = fp.read(temp, 0, Math.min(temp.length, len-total)); * dst.put(temp, 0, n); * total += n; * } * } * catch (IOException e) * { * throw IOErr.make(e).val; * } * } */ internal override void pipeFrom(byte[] src, int srcPos, int len) { try { m_stream.Write(src, srcPos, len); } catch (IOException e) { throw IOErr.make(e).val; } }
internal override void setPos(long x) { try { m_stream.Position = x; } catch (IOException e) { throw IOErr.make(e).val; } }
internal override void pipeTo(byte[] dst, int dstPos, int len) { try { m_stream.Read(dst, dstPos, len); } catch (IOException e) { throw IOErr.make(e).val; } }
public override int r() { try { return(inStream.ReadByte()); } catch (IOException e) { throw IOErr.make(e).val; } }
public override InStream @in(Long bufSize) { try { System.IO.Stream stream = (m_file as FileInfo).OpenRead(); return(SysInStream.make(stream, bufSize)); } catch (System.IO.IOException e) { throw IOErr.make(e).val; } }
public override OutStream w(int v) { try { outStream.WriteByte((byte)v); return(this); } catch (IOException e) { throw IOErr.make(e).val; } }
internal override void getBytes(long pos, byte[] dst, int off, int len) { try { m_stream.Position = pos; m_stream.Read(dst, off, len); } catch (IOException e) { throw IOErr.make(e).val; } }
public override OutStream writeBuf(Buf buf, long n) { try { buf.pipeTo(outStream, n); return(this); } catch (IOException e) { throw IOErr.make(e).val; } }
public override OutStream sync() { try { flush(); /*fd.sync();*/ return(this); } catch (System.IO.IOException e) { throw IOErr.make(e).val; } }
/* * internal override int pipeFrom(ByteBuffer src, int len) * { * try * { * byte[] temp = temp(); * int total = 0; * while (total < len) * { * int n = Math.min(temp.length, len-total); * src.get(temp, 0, n); * fp.write(temp, 0, n); * total += n; * } * return total; * } * catch (IOException e) * { * throw IOErr.make(e).val; * } * } */ ////////////////////////////////////////////////////////////////////////// // Buf API ////////////////////////////////////////////////////////////////////////// public override sealed Buf flush() { try { //fp.getFD().sync(); m_stream.Flush(); return(this); } catch (IOException e) { throw IOErr.make(e).val; } }
public override Long peek() { try { long pos = p.getPos(); int n = p.m_stream.ReadByte(); p.setPos(pos); return(n < 0 ? null : Long.valueOf(n)); } catch (IOException e) { throw IOErr.make(e).val; } }