SMBOutputStream.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (C) 2006-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.jlan.client;
  20. import java.io.*;
  21. import org.alfresco.jlan.smb.SMBException;
  22. /**
  23. * SMB output stream class.
  24. *
  25. * <p>
  26. * The SMBOutputStream class provides a standard OutputStream interface to an existing remote file,
  27. * or can be used to create a new remote file.
  28. *
  29. * <p>
  30. * The class may be used with other I/O stream classes such as PrintWriter, DataOutputStream etc.
  31. *
  32. * <p>
  33. * <strong>Note:</strong> It is not necessary to use a BufferedOutputStream or BufferedWriter class
  34. * with the SMBOutputStream as the underlying network connection will usually buffer 4Kb of data, up
  35. * to a maximum of 64Kb.
  36. *
  37. *
  38. * <p>
  39. * Example use of the SMBOutputStream class
  40. *
  41. * <p>
  42. * <code>PCShare shr = new PCShare ( "\\\\TEST\\C\\");<br>
  43. * DiskSession sess = SessionFactory.OpenDisk ( shr);<br>
  44. * SMBOutputStream out = sess.OpenOutputStream ( "DATAFILE.OUT", AccessMode.WriteOnly);<br>
  45. * PrintWriter pWrt = new PrintWriter ( out, false);<br>
  46. * for ( int i = 0; i < 20; i++)<br>
  47. * &nbsp;&nbsp;pWrt.println ( "Record " + i);<br>
  48. * pWrt.flush ();<br>
  49. * out.close ();</code>
  50. *
  51. * @author gkspencer
  52. */
  53. public class SMBOutputStream extends java.io.OutputStream {
  54. // SMB file that this stream is associated with.
  55. private SMBFile m_file;
  56. /**
  57. * Construct an SMB output stream attached to the specified SMB file
  58. *
  59. * @param sfile SMBFile that this output stream is attached to.
  60. */
  61. protected SMBOutputStream(SMBFile sfile) {
  62. m_file = sfile;
  63. }
  64. /**
  65. * Close this output stream and release any system resources associated with the stream.
  66. *
  67. * @exception java.io.IOException If an I/O error occurs.
  68. */
  69. public void close()
  70. throws java.io.IOException {
  71. // Close the remote file
  72. try {
  73. m_file.Close();
  74. }
  75. catch (SMBException ex) {
  76. throw new IOException(ex.getErrorText());
  77. }
  78. }
  79. /**
  80. * Return a reference to the associated SMBFile object.
  81. *
  82. * @return SMBFile associated with this output stream.
  83. */
  84. public final SMBFile File() {
  85. return m_file;
  86. }
  87. /**
  88. * Flush this output stream, force any buffered data to be written out.
  89. *
  90. * @exception java.io.IOException If an I/O error occurs.
  91. */
  92. public void flush()
  93. throws java.io.IOException {
  94. // Flush buffered data on this output stream
  95. try {
  96. m_file.Flush();
  97. }
  98. catch (SMBException ex) {
  99. throw new IOException(ex.getErrorText());
  100. }
  101. }
  102. /**
  103. * Write the specified byte array to the output stream, starting at the specified offset within
  104. * the byte array.
  105. *
  106. * @param buf Byte array containing the data to be output.
  107. * @param off Offset within the buffer that the data starts.
  108. * @param len Length of the data to be output.
  109. * @exception java.io.IOException If an I/O error occurs.
  110. */
  111. public void write(byte buf[], int off, int len)
  112. throws java.io.IOException {
  113. // Write the byte array to the output stream
  114. try {
  115. m_file.Write(buf, len, off);
  116. }
  117. catch (SMBException ex) {
  118. throw new IOException(ex.getErrorText());
  119. }
  120. }
  121. /**
  122. * Write the specified byte to this output stream.
  123. *
  124. * @param byt Byte to be output to this stream.
  125. * @exception java.io.IOException If an I/O error occurs.
  126. */
  127. public void write(int byt)
  128. throws java.io.IOException {
  129. // Create a byte array to hold the single byte
  130. byte[] buf = new byte[2];
  131. buf[0] = (byte) byt;
  132. // Write the byte to the output stream
  133. try {
  134. m_file.Write(buf, 1, 0);
  135. }
  136. catch (SMBException ex) {
  137. throw new IOException(ex.getErrorText());
  138. }
  139. }
  140. }