EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
olzoBuffer.cc
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file olzoBuffer.cc
1 
2 #include "olzoBuffer.h"
3 #include "BufferConstants.h"
4 
5 #include <lzo/lzoutil.h>
6 #include <cstring>
7 #include <stdlib.h>
8 #include <unistd.h>
9 
11 
12 
13 // the constructor first ----------------
14 #ifndef WIN32
15 olzoBuffer::olzoBuffer (int fdin, PHDWORD * where,
16  const int length,
17  const int irun,
18  const int iseq):
19  ophBuffer(fdin,where,length,irun,iseq)
20 #else
21 olzoBuffer::olzoBuffer (const char *fpp, PHDWORD * where,
22  const int length,
23  int &status,
24  const int irun,
25  const int iseq):
26  ophBuffer(fpp,where,length,status,irun,iseq)
27 #endif
28 {
29  // get a buffer for zlib
30 
31  _broken = 0;
32 
33  if ( ! lzo_initialized )
34  {
35  if (lzo_init() != LZO_E_OK)
36  {
37  COUT << "Could not initialize LZO" << std::endl;
38  _broken = 1;
39  }
40 
41  lzo_initialized = 1;
42  }
43 
44 
45  wrkmem = (lzo_bytep) lzo_malloc(LZO1X_1_12_MEM_COMPRESS);
46  if (wrkmem)
47  {
48  memset(wrkmem, 0, LZO1X_1_12_MEM_COMPRESS);
49  }
50  outputarraylength = (int)(length *1.1) + 2048;
52 
53 }
54 
55 // ----------------------------------------------------------
56 // returns the number of bytes written, including record wasted space.
57 //
59 {
60 
61 
62  if (! dirty) return 0;
63 
64  if (! has_end) addEoB();
65 
66  lzo_uint outputlength_in_bytes = outputarraylength*4-16;
67  lzo_uint in_len = bptr->Length;
68 
69  lzo1x_1_12_compress( (lzo_byte *) bptr,
70  in_len,
71  (lzo_byte *)&outputarray[4],
72  &outputlength_in_bytes,wrkmem);
73 
74 
75  outputarray[0] = outputlength_in_bytes +4*BUFFERHEADERLENGTH;
77  outputarray[2] = bptr->Bufseq;
78  outputarray[3] = bptr->Length;
79 
80  unsigned int ip =0;
81  char *cp = (char *) outputarray;
82 
83  while (ip<outputarray[0])
84  {
85  int n = write ( fd, cp, BUFFERBLOCKSIZE);
86  if ( n != BUFFERBLOCKSIZE)
87  {
88  std::cout << " could not write output, bytes written: " << n << std::endl;
89  return 0;
90  }
91 
92  cp += BUFFERBLOCKSIZE;
93  ip += BUFFERBLOCKSIZE;
94  }
95  dirty = 0;
96  byteswritten += ip;
97  return 0;
98 }
99 
100 
101 // ----------------------------------------------------------
103 {
104  writeout();
105  delete [] outputarray;
106  lzo_free(wrkmem);
107 
108 }
109