If cachedBytes are not null, returns a Reader created from the cachedBytes.
Otherwise, returns a Reader from the underlying source.
425576
public Reader createReader(byte[] cachedBytes) throws IOException {
	if (cachedBytes != null) {
	    return new InputStreamReader(new ByteArrayInputStream(cachedBytes));
	}
        if (inputStream != null) {
            return new InputStreamReader(inputStream, charset);
        }
        if (reader != null) {
            return reader;
        }
        return new InputStreamReader(new FileInputStream(file), charset);
    }
955821
protected void processReader(IModule module, ModuleBuildReader reader) throws IOException {
		// Add the cache key generator list to the result list
		List<ICacheKeyGenerator> keyGenList = reader.getCacheKeyGenerators();
		if (keyGenList != null) {
			keyGens.addAll(keyGenList);
		}
		if (smGen != null) {
			// If we're generating a source map, then merge the source map for the module
			// into the layer source map.
			SourceMap moduleSourceMap = reader.getSourceMap();
			if (moduleSourceMap != null && writer instanceof LineCountingStringWriter) {
				sectionCount++;
				LineCountingStringWriter lcWriter = (LineCountingStringWriter)writer;
				try {
					smGen.mergeMapSection(lcWriter.getLine(), lcWriter.getColumn(), moduleSourceMap.map);
				} catch (SourceMapParseException e) {
					throw new IOException(e);
				}
				// Save the sources content, indexed by the module name, so that we can
				// add the 'sourcesContent' property to the layer map after the map
				// has been generated.  We need to do this because SourceMapGeneratorV3
				// does not merge the sourcesContent properties from module source maps into
				// the merged layer source map.
				sourcesMap.put(moduleSourceMap.name, moduleSourceMap.source);
			}
		}
		// Add the reader contents to the result
		try {
			IOUtils.copy(reader, writer);
		} finally {
			IOUtils.closeQuietly(reader);
		}
		if (reader.isError()) {
			errorMessages.add(reader.getErrorMessage());
		}
	}
598077
@Override
	public Reader getReader() throws IOException {
		// Return an Aggregation reader for all the resources in this
		// aggregated resource
		List<Reader> readers = new ArrayList<Reader>(resources.size());
		for (IResource resource : resources) {
			readers.add(resource.getReader());
		}
		return new AggregationReader(readers);
	}
1252941
private <T> T getInstance(DatastoreType datastoreType, TransactionalCache.Mode cacheMode) {
        DatastoreId id = getDatastoreId(datastoreType);
        Object instance = cache.get(id, cacheMode);
        if (instance == null) {
            DynamicType<?> types = getTypes(datastoreType);
            instance = newInstance(id, datastoreType, types, cacheMode);
            if (TransactionalCache.Mode.READ.equals(cacheMode)) {
                instanceListenerService.postLoad(instance);
            }
        }
        return (T) instance;
    }
1367323
private static final TreeReader getReader(String format, boolean throwException) {
		TreeReader reader;
		if (format == null) {
			reader = cachedJsonReader;
		} else {
			String key = format.toLowerCase();
			if (JSON.equals(key)) {
				reader = cachedJsonReader;
			} else {
				reader = readers.get(key);
			}
		}
		if (reader != null) {
			return reader;
		}

		// Search reader class by system property:
		//
		// -Ddatatree.json.reader=io.datatree.adapters.JsonBoon
		// -Ddatatree.xml.reader=io.datatree.adapters.XmlJackson
		// -Ddatatree.yaml.reader=your.yaml.reader.ClassName
		// -Ddatatree.csv.reader=your.csv.reader.ClassName
		// -Ddatatree.properties.reader=your.properties.reader.ClassName
		//
		// ...or to use your "custom" format:
		//
		// -Ddatatree.custom.reader=your.custom.reader.ClassName
		// Tree node = new Tree(source, "custom");
		//
		String propertyName = "datatree." + format + ".reader";
		try {
			String className = System.getProperty(propertyName);
			if (className == null || className.isEmpty()) {
				className = PackageScanner.findByFormat(format, true);
			}
			if (className == null || className.isEmpty()) {
				throw new IllegalArgumentException("System Property \"" + propertyName + "\" not found! "
						+ "This property defines a custom reader class for the \"" + format + "\" format.");
			}
			reader = (TreeReader) Class.forName(className).newInstance();
			readers.put(format, reader);
			return reader;
		} catch (Throwable cause) {
			if (throwException) {
				suggestDependency(format);
				cause.printStackTrace();
				throw new IllegalArgumentException("Unable to create reader for format \"" + format + "\"! Set the -D"
						+ propertyName + "=package.ReaderClass initial parameter to specify the proper reader class.",
						cause);
			}
		}
		return new JsonBuiltin();
	}
290074
@Override
    protected T readCacheDataFromFile(File file) throws CacheLoadingException {
        try {
            String resultJson = null;
            synchronized (file.getAbsolutePath().intern()) {
                resultJson = FileUtils.readFileToString(file, CharEncoding.UTF_8);
            }
            if (!StringUtils.isEmpty(resultJson)) {
                T result = deserializeData(resultJson);
                return result;
            }
            throw new CacheLoadingException("Unable to restore cache content : cache file is empty");
        } catch (FileNotFoundException e) {
            // Should not occur (we test before if file exists)
            // Do not throw, file is not cached
            Ln.w("file " + file.getAbsolutePath() + " does not exists", e);
            return null;
        } catch (CacheLoadingException e) {
            throw e;
        } catch (Exception e) {
            throw new CacheLoadingException(e);
        }
    }
336047
function Reader(buf, filePath) {
  // Allow instantiating without `new`
  if (!(this instanceof Reader)) {
    return new Reader(buf);
  }

  // Allow passing either a path to a file or a raw buffer
  if (typeof buf === 'string') {
    filePath = buf;
    buf = fs.readFileSync(buf);
  }

  // LRU cache that'll hold objects we read from the data section.
  // 5000 seems to be roughly the sweet spot between mem vs hit-rate
  this._pointerCache = new LRU(LRU_SIZE);

  this._buf = buf;

  // Stash the file path for if we want to reload it later
  this.filePath = filePath;

  this.setup();
}
755337
public Result readCacheEntry(Object id, boolean calledFromRemove) {
        final String methodName = "readCacheEntry()";
        Result result = getFromResultPool();
        CacheEntry answer = null;
        if (this.invalidationBuffer.contains(id)) {
            return result;
        }
        HashtableEntry htEntry = null;
        boolean checkExpired = true;
        if (cache.isEnableListener() && cache.getEventSource().getInvalidationListenerCount() > 0 && this.cod.ignoreValueInInvalidationEvent == false) {
            checkExpired = false;
        }
        Object obj = null;
        try {
            rwLock.readLock().lock();
            //if (this.testException) {
            //    throw new FileManagerException("*** Generate filemanager exception");
            //}
            htEntry = object_cache.getHashTableEntry(id, checkExpired); // retrieve from HTOD
        } catch (FileManagerException ex) {
            this.diskCacheException = ex;
            result.diskException = ex;
            result.returnCode = DISK_EXCEPTION;
        } catch (HashtableOnDiskException ex) {
            this.diskCacheException = ex;
            result.diskException = ex;
            result.returnCode = DISK_EXCEPTION;
        } catch (IOException ex) {
            this.diskCacheException = ex;
            result.diskException = ex;
            result.returnCode = DISK_EXCEPTION;
        } catch (Exception ex) {
            result.returnCode = OTHER_EXCEPTION;
            result.diskException = ex;
        } finally {
            rwLock.readLock().unlock();
            if (result.returnCode != NO_EXCEPTION) {
                if (tc.isDebugEnabled())
                    Tr.debug(tc, methodName, "cacheName=" + this.cacheName + "\n Exception: " + ExceptionUtility.getStackTrace(result.diskException));
            }
            if (result.returnCode == DISK_EXCEPTION || result.returnCode == OTHER_EXCEPTION) {
                com.ibm.ws.ffdc.FFDCFilter.processException(result.diskException, "com.ibm.ws.cache.HTODDynacache.readCacheEntry", "916", this);
                return result;
            }
        }

        boolean expired = false;
        boolean alias = false;
        int dataSize = 0;
        Object origId = id;
        if (htEntry != null) {
            if (htEntry.isAliasId()) {
                alias = true;
                obj = htEntry.getValue();
                origId = obj;
                if (tc.isDebugEnabled())
                    Tr.debug(tc, methodName, "cacheName=" + this.cacheName + " found alias=" + id + " cacheId=" + origId);
                returnToHashtableEntryPool(htEntry);
                try {
                    rwLock.readLock().lock();
                    if (this.invalidationBuffer.contains(origId)) {
                        return result;
                    }
                    htEntry = object_cache.getHashTableEntry(origId, checkExpired); // retrieve from HTOD
                } catch (FileManagerException ex) {
                    this.diskCacheException = ex;
                    result.diskException = ex;
                    result.returnCode = DISK_EXCEPTION;
                } catch (HashtableOnDiskException ex) {
                    this.diskCacheException = ex;
                    result.diskException = ex;
                    result.returnCode = DISK_EXCEPTION;
                } catch (IOException ex) {
                    this.diskCacheException = ex;
                    result.diskException = ex;
                    result.returnCode = DISK_EXCEPTION;
                } catch (Exception ex) {
                    result.returnCode = OTHER_EXCEPTION;
                    result.diskException = ex;
                } finally {
                    rwLock.readLock().unlock();
                    if (result.returnCode != NO_EXCEPTION) {
                        if (tc.isDebugEnabled())
                            Tr.debug(tc, methodName, "cacheName=" + this.cacheName + "\n Exception: " + ExceptionUtility.getStackTrace(result.diskException));
                    }
                    if (result.returnCode == DISK_EXCEPTION || result.returnCode == OTHER_EXCEPTION) {
                        com.ibm.ws.ffdc.FFDCFilter.processException(result.diskException, "com.ibm.ws.cache.HTODDynacache.readCacheEntry", "916", this);
                        return result;
                    }
                }
            }
            if (htEntry != null) {
                if (htEntry.isExpired()) {
                    expired = true;
                    if (checkExpired) {
                        if (cache.isEnableListener() && cache.getEventSource().getInvalidationListenerCount() > 0) {
                            // In here - ignoreValueInInvalidationEvent = true
                            InvalidationEvent ie = new InvalidationEvent(origId, null, InvalidationEvent.DISK_TIMEOUT, InvalidationEvent.LOCAL, this.cache.getCacheName());
                            if (!this.invalidationBuffer.contains(origId)) {
                                this.cache.getEventSource().fireEvent(ie);
                            }
                        }
                        this.invalidationBuffer.add(origId, HTODInvalidationBuffer.EXPLICIT_BUFFER, CachePerf.TIMEOUT, CachePerf.LOCAL, !Cache.FROM_DEPID_TEMPLATE_INVALIDATION,
                                                    !HTODInvalidationBuffer.FIRE_EVENT, !HTODInvalidationBuffer.ALIAS_ID);
                        if (alias) {
                            this.invalidationBuffer.add(id, HTODInvalidationBuffer.EXPLICIT_BUFFER, CachePerf.TIMEOUT, CachePerf.LOCAL, !Cache.FROM_DEPID_TEMPLATE_INVALIDATION,
                                                        !HTODInvalidationBuffer.FIRE_EVENT, HTODInvalidationBuffer.ALIAS_ID);
                        }
                        if (this.cod.diskCachePerformanceLevel != CacheConfig.HIGH && !calledFromRemove) {
                            this.cache.internalInvalidateByDepId(origId, CachePerf.TIMEOUT, CachePerf.LOCAL, Cache.FIRE_INVALIDATION_LISTENER);
                        }
                        returnToHashtableEntryPool(htEntry);
                        return result;
                    }
                }
                // Retrieve CE and put data back in CE
                obj = htEntry.getValue();
                dataSize = htEntry.size();
                if (obj != null) {
                    try {
                        answer = (CacheEntry) SerializationUtility.deserialize((byte[]) obj, cacheName);
                        answer.setValidatorExpirationTime(htEntry.validatorExpirationTime());
                        byte[] serializedCacheValue = htEntry.getSerializedCacheValue();
                        if (serializedCacheValue != null) {
                            answer.serializedValue = serializedCacheValue;
                        }
                    } catch (Exception ex) {
                        result.returnCode = SERIALIZATION_EXCEPTION;
                        result.diskException = ex;
                        com.ibm.ws.ffdc.FFDCFilter.processException(result.diskException, "com.ibm.ws.cache.HTODDynacache.readCacheEntry", "953", this);
                        if (tc.isDebugEnabled())
                            Tr.debug(tc, methodName, "cacheName=" + this.cacheName + "\n Exception: " + ExceptionUtility.getStackTrace(result.diskException));
                    } finally {
                        returnToHashtableEntryPool(htEntry);
                    }
                }
            }
        }
        if (answer != null) {
            if (expired) {
                if (this.cache.isEnableListener() && cache.getEventSource().getInvalidationListenerCount() > 0) {
                    // In here - ignoreValueInInvalidationEvent = false
                    InvalidationEvent ie = new InvalidationEvent(answer.id, answer.serializedValue, InvalidationEvent.DISK_TIMEOUT, InvalidationEvent.LOCAL, this.cache.getCacheName());
                    if (!this.invalidationBuffer.contains(answer.id)) {
                        this.cache.getEventSource().fireEvent(ie);
                    }
                }
                this.invalidationBuffer.add(answer.id, HTODInvalidationBuffer.EXPLICIT_BUFFER, CachePerf.TIMEOUT, CachePerf.LOCAL, !Cache.FROM_DEPID_TEMPLATE_INVALIDATION,
                                            !HTODInvalidationBuffer.FIRE_EVENT, !HTODInvalidationBuffer.ALIAS_ID);
                for (int i = 0; i < answer.aliasList.length; i++) {
                    this.invalidationBuffer.add(answer.aliasList[i], HTODInvalidationBuffer.EXPLICIT_BUFFER, CachePerf.TIMEOUT, CachePerf.LOCAL,
                                                !Cache.FROM_DEPID_TEMPLATE_INVALIDATION, !HTODInvalidationBuffer.FIRE_EVENT, HTODInvalidationBuffer.ALIAS_ID);;
                }
                if (this.cod.diskCachePerformanceLevel != CacheConfig.HIGH && !calledFromRemove) {
                    //if (tc.isDebugEnabled()) Tr.debug(tc, methodName, "cacheName=" + this.cacheName + " id=" + answer.id + " check id as dep id");
                    this.cache.internalInvalidateByDepId(answer.id, CachePerf.TIMEOUT, CachePerf.LOCAL, Cache.FIRE_INVALIDATION_LISTENER);
                }
                answer = null;
            } else {
                if (!calledFromRemove && answer.serializedValue != null) {
                    this.cache.getCacheStatisticsListener().readEntryFromDisk(answer.id, dataSize);
                }
            }
        }
        result.data = answer;
        if (tc.isDebugEnabled())
            Tr.debug(tc, methodName, "cacheName=" + this.cacheName + " id=" + id + " cacheSizeInBytes=" + this.cod.currentCacheSizeInBytes);
        return result;
    }
972271
def read(self, identifier, path=None):
        
        if self.CACHE_FULL_TEI is True:
            o = self.cache.get(_cache_key(self.texts_parsed_cache_key, identifier))
            if o is not None:
                return o
            else:
                with open(path) as f:
                    o = Text(urn=identifier, resource=self.xmlparse(f))
                    self.cache.set(_cache_key(self.texts_parsed_cache_key, identifier), o)
        else:
            with open(path) as f:
                o = Text(urn=identifier, resource=self.xmlparse(f))
        return o
707119
@Override
	protected Reader getContentReader(String mid, IResource resource, HttpServletRequest request,
			MutableObject<List<ICacheKeyGenerator>> keyGensRef) throws IOException {

		final String sourceMethod = "getContentReader"; //$NON-NLS-1$
		final boolean isTraceLogging = log.isLoggable(Level.FINER);
		if (isTraceLogging) {
			log.entering(sourceClass, sourceMethod, new Object[]{mid, resource, request, keyGensRef});
		}

		// Call super class implementation and determine if we need to update the cache key generators
		Reader result = super.getContentReader(mid, resource, request, keyGensRef);
		if (isFeatureDependent) {
			List<ICacheKeyGenerator> keyGens = keyGensRef.getValue();
			if (resource.getPath().toLowerCase().endsWith(LESS_SUFFIX)) {
				Set<String> dependentFeatures = threadLocalDependentFeatures.get();
				if (keyGens == null) {
					keyGens = getCacheKeyGenerators(getAggregator());
				}
				FeatureSetCacheKeyGenerator fsKeyGen = (FeatureSetCacheKeyGenerator)keyGens.get(2);
				// Cache key generators need to be updated if existing one is provisional or if there are new dependent features
				if (fsKeyGen.isProvisional() || !fsKeyGen.getFeatureSet().containsAll(dependentFeatures)) {
					List<ICacheKeyGenerator> newKeyGens = new ArrayList<ICacheKeyGenerator>();
					newKeyGens.add(keyGens.get(0));
					newKeyGens.add(keyGens.get(1));
					FeatureSetCacheKeyGenerator newFsKeyGen = new FeatureSetCacheKeyGenerator(dependentFeatures, false);
					newKeyGens.add(newFsKeyGen.combine(fsKeyGen));
					keyGensRef.setValue(newKeyGens);
					if (isTraceLogging) {
						log.logp(Level.FINER, sourceClass, sourceMethod, "Key generators updated: " + KeyGenUtil.toString(newKeyGens)); //$NON-NLS-1$
					}
				}
			} else {
				// We're processing a CSS file.  See if we need to provide key gens
				if (keyGens == null) {
					keyGensRef.setValue(super.getCacheKeyGenerators(getAggregator()));
				}
			}
		}
		if (isTraceLogging) {
			log.exiting(sourceClass, sourceMethod, result);
		}
		return result;
	}
424046
private ItemDataProvider connect(boolean sync, InputReader in) {
		//read app's consumer key and secret
		String[] consumer;
		try {
			consumer = readConsumer();
		} catch (Exception e) {
			//should never happen
			throw new RuntimeException("Could not read OAuth consumer key and secret");
		}
		
		//use previously stored authentication
		File authStoreFile = new File(CSLToolContext.current().getConfigDir(),
				getAuthStoreFileName());
		AuthenticationStore authStore;
		try {
			authStore = new FileAuthenticationStore(authStoreFile);
		} catch (IOException e) {
			error("could not read user's authentication store: " + authStoreFile.getPath());
			return null;
		}
		
		//connect to remote server
		RemoteConnector dmc = createRemoteConnector(consumer[0], consumer[1]);
		dmc = new AuthenticatedRemoteConnector(dmc, authStore);
		
		//enable cache
		File cacheFile = new File(CSLToolContext.current().getConfigDir(),
				getCacheFileName());
		CachingRemoteConnector mc = new CachingRemoteConnector(dmc, cacheFile);
		
		//clear cache if necessary
		if (sync) {
			mc.clear();
		}

		List<CSLItemData> itemDataList;
		int retries = 1;
		while (true) {
			try {
				//download list of item IDs
				boolean cacheempty = false;
				if (!mc.hasItemList()) {
					System.out.print("Retrieving items ...");
					cacheempty = true;
				}
				List<String> items = mc.getItemIDs();
				if (cacheempty) {
					System.out.println();
				}
				
				//download all items
				itemDataList = new ArrayList<>(items.size());
				int s = 0;
				int printed = 0;
				int bulk = mc.getMaxBulkItems();
				if (bulk == 1) {
					mc.beginTransaction();
				}
				
				while (s < items.size()) {
					int n = 0;
					List<String> itemsToRetrieve = new ArrayList<>(bulk);
					while (s < items.size() && n < bulk) {
						String did = items.get(s);
						if (!mc.containsItemId(did)) {
							String msg = String.format("\rSynchronizing (%d/%d) ...",
									s + 1, items.size());
							System.out.print(msg);
							++printed;
							++n;
						}
						itemsToRetrieve.add(did);
						++s;
					}
					Map<String, CSLItemData> itemData = mc.getItems(itemsToRetrieve);
					itemDataList.addAll(itemData.values());
					
					if (bulk == 1 && s % 10 == 0) {
						mc.commitTransaction();
					}
				}
				
				if (bulk == 1) {
					mc.commitTransaction();
					mc.endTransaction();
				}
				
				if (printed > 0) {
					System.out.println();
				}
			} catch (UnauthorizedException e) {
				if (retries == 0) {
					error("failed to authorize.");
					return null;
				}
				--retries;
				
				//app is not authenticated yet
				System.out.print("\r"); //overwrite 'Retrieving items' message
				if (!authorize(mc, in)) {
					return null;
				}
				
				continue;
			} catch (RequestException e) {
				error(e.getMessage());
				return null;
			} catch (IOException e) {
				error("could not get list of items from remote server.");
				return null;
			}
			
			break;
		}
		
		//return provider that contains all items from the server
		CSLItemData[] itemDataArr = itemDataList.toArray(new CSLItemData[itemDataList.size()]);
		return createItemDataProvider(itemDataArr);
	}
1354677
public PersistedNodeData read(ObjectReader in) throws UnknownClassIdException, IOException
   {
      // read id
      int key;
      if ((key = in.readInt()) != SerializationConstants.PERSISTED_NODE_DATA)
      {
         throw new UnknownClassIdException("There is unexpected class [" + key + "]");
      }

      QPath qpath;
      try
      {
         String sQPath = in.readString();
         qpath = QPath.parse(sQPath);
      }
      catch (final IllegalPathException e)
      {
         throw new IOException("Deserialization error. " + e)
         {

            /**
             * {@inheritDoc}
             */
            @Override
            public Throwable getCause()
            {
               return e;
            }
         };
      }

      String identifier = in.readString();

      String parentIdentifier = null;
      if (in.readByte() == SerializationConstants.NOT_NULL_DATA)
      {
         parentIdentifier = in.readString();
      }

      int persistedVersion = in.readInt();
      // --------------

      int orderNum = in.readInt();

      // primary type
      InternalQName primaryTypeName;
      try
      {
         primaryTypeName = InternalQName.parse(in.readString());
      }
      catch (final IllegalNameException e)
      {
         throw new IOException(e.getMessage())
         {
            private static final long serialVersionUID = 3489809179234435267L;

            /**
             * {@inheritDoc}
             */
            @Override
            public Throwable getCause()
            {
               return e;
            }
         };
      }

      // mixins
      InternalQName[] mixinTypeNames = null;
      if (in.readByte() == SerializationConstants.NOT_NULL_DATA)
      {
         int count = in.readInt();
         mixinTypeNames = new InternalQName[count];
         for (int i = 0; i < count; i++)
         {
            try
            {
               mixinTypeNames[i] = InternalQName.parse(in.readString());
            }
            catch (final IllegalNameException e)
            {
               throw new IOException(e.getMessage())
               {
                  private static final long serialVersionUID = 3489809179234435268L; // eclipse

                  // gen

                  /**
                   * {@inheritDoc}
                   */
                  @Override
                  public Throwable getCause()
                  {
                     return e;
                  }
               };
            }
         }
      }

      // acl
      AccessControlList acl = null;
      if (in.readByte() == SerializationConstants.NOT_NULL_DATA)
      {
         ACLReader rdr = new ACLReader();
         acl = rdr.read(in);
      }

      return new PersistedNodeData(identifier, qpath, parentIdentifier, persistedVersion, orderNum, primaryTypeName,
         mixinTypeNames, acl);
   }
1298060
def get_cached_data(datatable, **kwargs):
    
    cache_key = '%s%s' % (CACHE_PREFIX, datatable.get_cache_key(**kwargs))
    data = cache.get(cache_key)
    log.debug("Reading data from cache at %r: %r", cache_key, data)
    return data
482311
@Override
    public BufferedReader reader() throws IOException {
        byte[] raw = request.getRawBody();
        if (raw != null) {
            return IOUtils.toBufferedReader(new InputStreamReader(new ByteArrayInputStream(raw)));
        }
        return null;
    }
576738
public InputStream getAsStream() throws CacheException {
		try {
			if(file.exists() && file.isFile()) {
				logger.debug("retrieving file as a stream");
				return new FileInputStream(file);			
			}
		} catch(IOException e) {
			logger.error("file '{}' does not exist or is not a file", file.getAbsolutePath());
			throw new CacheException("File " + file.getAbsolutePath() + " does not exist or is not a regular file", e);
		}		
		return null;
	}
245393
private void readObject(java.io.ObjectInputStream in)
        throws IOException, ClassNotFoundException
    {
        in.defaultReadObject();
        _cache=new HashMap();
    }
284713
public static Reader retrieve(String reader) throws StripeException {
    return retrieve(reader, (Map<String, Object>) null, (RequestOptions) null);
  }