disable broken tests on net_4_0
[mcs.git] / class / Npgsql / Npgsql / NpgsqlCopyInStream.cs
blob068fac1d53d6e4df974a803191c6dba2dadd80c2
1 // Npgsql.NpgsqlCopyInStream.cs
2 //
3 // Author:
4 // Kalle Hallivuori <kato@iki.fi>
5 //
6 // Copyright (C) 2007 The Npgsql Development Team
7 // npgsql-general@gborg.postgresql.org
8 // http://gborg.postgresql.org/project/npgsql/projdisplay.php
9 //
10 // Permission to use, copy, modify, and distribute this software and its
11 // documentation for any purpose, without fee, and without a written
12 // agreement is hereby granted, provided that the above copyright notice
13 // and this paragraph and the following two paragraphs appear in all copies.
14 //
15 // IN NO EVENT SHALL THE NPGSQL DEVELOPMENT TEAM BE LIABLE TO ANY PARTY
16 // FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES,
17 // INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
18 // DOCUMENTATION, EVEN IF THE NPGSQL DEVELOPMENT TEAM HAS BEEN ADVISED OF
19 // THE POSSIBILITY OF SUCH DAMAGE.
20 //
21 // THE NPGSQL DEVELOPMENT TEAM SPECIFICALLY DISCLAIMS ANY WARRANTIES,
22 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
23 // AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
24 // ON AN "AS IS" BASIS, AND THE NPGSQL DEVELOPMENT TEAM HAS NO OBLIGATIONS
25 // TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
28 using System;
29 using System.IO;
31 namespace Npgsql
33 /// <summary>
34 /// Stream for writing data to a table on a PostgreSQL version 7.4 or newer database during an active COPY FROM STDIN operation.
35 /// <b>Passes data exactly as is and when given</b>, so see to it that you use server encoding, correct format and reasonably sized writes!
36 /// </summary>
37 internal class NpgsqlCopyInStream : Stream
39 private NpgsqlConnector _context;
40 private long _bytesPassed = 0;
42 /// <summary>
43 /// True while this stream can be used to write copy data to server
44 /// </summary>
45 private bool IsActive
47 get { return _context != null && _context.CurrentState is NpgsqlCopyInState && _context.Mediator.CopyStream == this; }
50 /// <summary>
51 /// Created only by NpgsqlCopyInState.StartCopy()
52 /// </summary>
53 internal NpgsqlCopyInStream(NpgsqlConnector context)
55 _context = context;
58 /// <summary>
59 /// False
60 /// </summary>
61 public override bool CanRead
63 get { return false; }
66 /// <summary>
67 /// True
68 /// </summary>
69 public override bool CanWrite
71 get { return true; }
74 /// <summary>
75 /// False
76 /// </summary>
77 public override bool CanSeek
79 get { return false; }
82 /// <summary>
83 /// Number of bytes written so far
84 /// </summary>
85 public override long Length
87 get { return _bytesPassed; }
90 /// <summary>
91 /// Number of bytes written so far; not settable
92 /// </summary>
93 public override long Position
95 get { return _bytesPassed; }
96 set { throw new NotSupportedException("Tried to set Position of network stream " + this); }
99 /// <summary>
100 /// Successfully completes copying data to server. Returns after operation is finished.
101 /// Does nothing if this stream is not the active copy operation writer.
102 /// </summary>
103 public override void Close()
105 if (_context != null)
107 if (IsActive)
109 _context.CurrentState.SendCopyDone(_context);
111 if (_context.Mediator.CopyStream == this)
113 _context.Mediator.CopyStream = null;
115 _context = null;
119 /// <summary>
120 /// Withdraws an already started copy operation. The operation will fail with given error message.
121 /// Does nothing if this stream is not the active copy operation writer.
122 /// </summary>
123 public void Cancel(string message)
125 if (IsActive)
127 NpgsqlConnector c = _context;
128 _context = null;
129 c.Mediator.CopyStream = null;
130 c.CurrentState.SendCopyFail(_context, message ?? "Cancel Copy");
134 /// <summary>
135 /// Writes given bytes to server.
136 /// Fails if this stream is not the active copy operation writer.
137 /// </summary>
138 public override void Write(byte[] buf, int off, int len)
140 if (! IsActive)
142 throw new ObjectDisposedException("Writing to closed " + this);
144 _context.CurrentState.SendCopyData(_context, buf, off, len);
145 _bytesPassed += len;
148 /// <summary>
149 /// Flushes stream contents to server.
150 /// Fails if this stream is not the active copy operation writer.
151 /// </summary>
152 public override void Flush()
154 if (! IsActive)
156 throw new ObjectDisposedException("Flushing closed " + this);
158 _context.Stream.Flush();
161 /// <summary>
162 /// Not readable
163 /// </summary>
164 public override int Read(byte[] buf, int off, int len)
166 throw new NotSupportedException("Tried to read non-readable " + this);
169 /// <summary>
170 /// Not seekable
171 /// </summary>
172 public override long Seek(long pos, SeekOrigin so)
174 throw new NotSupportedException("Tried to seek non-seekable " + this);
177 /// <summary>
178 /// Not supported
179 /// </summary>
180 public override void SetLength(long len)
182 throw new NotSupportedException("Tried to set length of network stream " + this);